Chapter 9 of 17 · CSS for Beginners — Complete Free Tutorial
Display & Margin Collapsing
Before Flexbox and Grid, layout starts with the display property — it decides whether an element behaves like a block, like text, or like nothing at all. This chapter also explains margin collapsing, the classic source of "why is my gap twice as big?" confusion.
The main display values
| Value | Behaviour | Typical elements |
|---|---|---|
block | Full width, starts on a new line, accepts width/height | div, p, h1, section |
inline | Flows inside text, ignores width/height | span, a, strong |
inline-block | Inline flow but accepts width/height and margins | Badges, small widgets |
flex | Turns the element into a flex container | Navbars, toolbars, rows |
grid | Turns the element into a grid container | Page layouts, card grids |
none | Removed from layout entirely (not rendered, not read by some AT) | Hidden panels |
block vs inline, demonstrated
.tag-inline {
display: inline;
background: #eef2ff;
padding: 4px 8px; /* padding renders but does NOT affect line height */
width: 200px; /* ignored for inline elements */
}
.tag-block {
display: inline-block;
background: #eef2ff;
padding: 4px 8px;
width: 200px; /* now it works */
vertical-align: top;
}
Expected output: the inline tag's background hugs the text and does not push other lines; the inline-block version is exactly 200 px wide and can sit next to other elements.
none vs visibility: hidden
.gone { display: none; } /* no space, no render */
.invisible { visibility: hidden; } /* space kept, nothing painted */
display: none— element leaves the flow completely (screen readers skip it).visibility: hidden— reserves its space; useful for keeping layout stable while content loads.
Margin collapsing
In normal (block) flow, vertical margins between block siblings merge into a single margin — the larger of the two. Horizontal margins never collapse.
.a { margin-bottom: 30px; }
.b { margin-top: 10px; }
/* The visible gap between .a and .b is 30px, not 40px. */
The three collapsing cases
- Sibling siblings — adjacent block elements' bottom + top margins merge (shown above).
- Parent and first/last child — a child's top margin can escape the parent if the parent has no border/padding/flow context at that edge. Fix: put a padding or
overflow: autoon the parent, or usegapinstead. - Empty blocks — an element with no content collapses its own top and bottom margins together.
.panel {
padding-top: 1px; /* classic trick to stop child margin escape… */
/* …but the real modern fix: use flex/grid + gap, or padding on parent */
}
.stack > * + * { margin-top: 1.25rem; } /* predictable vertical rhythm */
Expected output: every stacked child sits exactly 20 px below the previous one, no matter how big individual margins get — because only one margin is ever in play.
Modern spacing: gap
Flex and grid containers support gap, which never collapses and never needs first/last-child hacks:
.row { display: flex; gap: 1rem; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.5rem; }
.stack { display: flex; flex-direction: column; gap: 1.25rem; }
Real-world example: vertical rhythm for a page
main {
display: flex;
flex-direction: column;
gap: 2.5rem;
max-width: 720px;
margin-inline: auto;
}
main section { display: flex; flex-direction: column; gap: 1rem; }
Expected output: sections are spaced 40 px apart and their inner blocks 16 px apart — with zero margin rules and no collapsing surprises.
Common mistakes
- Fighting margin collapse with
!important— restructure with flex/grid + gap instead. - Setting width on inline elements — switch to
inline-blockor (better) flex/grid children. - Using
display: nonefor off-screen widgets — considervisibilityor transform-based hiding when the space should remain. - Margins on flex/grid children — usually replace them with the container's
gap.
Practice checklist
- Create two paragraphs with
margin-bottom: 40pxandmargin-top: 10px— confirm dev tools show a 40 px gap. - Convert a stacked list to flex column with
gapand remove every child margin. - Toggle
display: nonevsvisibility: hiddenon an element and watch the layout shift (or not).
With display understood, containers become powerful — Flexbox: The Complete Guide is next.
Frequently Asked Questions
CSS (Cascading Style Sheets) is the language that controls how HTML content looks — colours, fonts, spacing, layout, responsiveness and animation. HTML provides structure and meaning; CSS provides presentation. One stylesheet can restyle an entire website at once.
The fundamentals (selectors, box model, Flexbox, media queries) take about 2–3 weeks of daily practice. Grid, animations and architecture come with a little more practice. This 17-chapter series is designed for 3–4 weeks at a relaxed pace — faster if you type out every example.
Yes. HTML first, then CSS, then JavaScript. CSS and HTML together let you build complete, good-looking static pages, and almost every JavaScript framework assumes you already know CSS layout and selectors.
No — CSS is a styling language with selectors and property-value declarations, not variables, loops or conditionals. You can get far without any programming logic, and JavaScript adds the behaviour later.
Flexbox distributes space along one axis (a row or a column) and is ideal for components — toolbars, navbars, card rows. Grid controls rows and columns together and is ideal for page structure and card grids. They are complementary: many layouts use a grid page shell with flex components inside.
Usually one of three reasons: the selector does not match (check class names), a more specific rule wins (use the browser inspector to see computed styles and specificity), or the file is not linked/loads in the wrong order. The specificity chapter explains how the cascade decides.
Every element is a box of four layers: content, padding (inner space), border, and margin (outer space). With box-sizing: border-box, the width you set includes padding and border — the universal reset every project uses.
Use relative units, flexible grids like repeat(auto-fit, minmax(260px, 1fr)), images with max-width: 100%, and mobile-first media queries. The goal is zero horizontal scrolling from 320px up — the responsive chapters show the full workflow.
Not to learn. Frameworks are productivity tools built on the same CSS you are learning here; understanding the fundamentals makes any framework easier to use (and to debug when it fights you). Learn vanilla CSS first, then evaluate frameworks with an informed eye.
JavaScript for interactivity, then a build tool and version control (Git). With HTML, CSS and JavaScript you can build complete websites; after that, frameworks like React or server-side tools like Laravel Blade build on these fundamentals.