Margins and collapsing
Overview
Put a heading with margin-bottom: 30px above a paragraph with margin-top: 20px, and you might expect 50px between them. You get 30px. This is margin collapsing — a deliberate feature of normal flow that surprises almost every CSS learner. Once you know its three rules, it stops being mysterious.
Why collapsing exists
Normal flow was designed for documents. If every paragraph has margin-block: 1em, you want 1em between paragraphs, not 2em — and you want the first paragraph to sit the same distance from the heading regardless of how margins are distributed. Collapsing gives text a consistent rhythm without extra rules.
Rule 1: adjacent siblings
When two block boxes are stacked, the bottom margin of the first and the top margin of the second collapse into a single margin equal to the larger of the two:
h2 { margin-bottom: 30px; }
p { margin-top: 20px; }
/* gap between them: 30px */With negative margins, the result is the largest positive margin plus the most negative one: 30px and -10px give 20px.
Rule 2: parent and first/last child
If there’s nothing separating a parent’s top edge from its first child’s top margin — no border, no padding, no inline content, and no new formatting context — the child’s margin collapses with the parent’s. The child’s margin effectively moves outside the parent:
<section class="hero">
<h1>Welcome</h1>
</section>.hero { background: #eaf0fc; }
.hero h1 { margin-top: 3rem; }
/* the 3rem appears ABOVE the blue section, not inside it */The same happens at the bottom with the last child’s margin-bottom. This is the most common margin surprise: a background that doesn’t extend as far as you expect, or a whole section pushed down by its child’s margin.
Rule 3: empty elements
An element with no height, padding, border or content lets its own top and bottom margins collapse together — and with the margins around it. An empty <div style="margin: 20px 0"> adds 20px, not 40px.
When margins don’t collapse
Margin collapsing only happens between block-direction margins in normal flow. Margins don’t collapse:
- horizontally (left and right margins always add up),
- between flex items or grid items, or between a flex/grid container and its items,
- on floated or absolutely positioned elements,
- across a border or padding,
- through an element that creates a new block formatting context —
display: flow-root,overflow: hidden/auto,display: inline-block, and others.
Preventing collapse
Choose based on what you need:
/* 1. Padding instead of the child's margin */
.hero { padding-block: 3rem; }
.hero h1 { margin-top: 0; }
/* 2. A new block formatting context on the parent */
.hero { display: flow-root; }
/* 3. Flex or grid with gap — margins never collapse there */
.card { display: flex; flex-direction: column; gap: 1rem; }Option 3 is increasingly popular for components: set child margins to zero and let gap handle the spacing. It’s predictable and only adds space between children.
A spacing strategy
Margins on both sides of elements cause more collapsing confusion than necessary. Common, predictable approaches:
Space in one direction
Pick one direction — usually the block-start (top) — and only add margins there:
.flow > * + * {
margin-block-start: 1em;
}The * + * selector (“lobotomised owl”) targets every child that follows another child — so there’s space between elements and none before the first. Adjusting the space after a heading becomes simple:
.flow > h2 + * { margin-block-start: 0.5em; }Gap for components
For components, prefer flexbox or grid with gap:
.stack {
display: flex;
flex-direction: column;
gap: var(--space, 1rem);
}You’ll meet this “stack” primitive again in Organizing layout CSS.
Reset default margins in components
Browsers give headings, paragraphs, lists and <figure> default margins. Many resets remove them so components start from zero:
h1, h2, h3, p, figure, ul, ol { margin: 0; }Then add spacing deliberately — in prose with .flow, in components with gap.
Debugging margins
Hover an element in DevTools: its margin is highlighted in orange, even where it extends outside its parent. If a margin appears where you didn’t expect it, look for a child whose margin has collapsed through its parent.
Mental model: in normal flow, vertical margins are minimum distances, not additions. “At least 30px between me and the next thing” — and if the next thing asks for 20px, 30px satisfies both.
Fix the escaping margin
A .card has a blue background, and its first child h2 has margin-top: 2rem. Instead of creating space inside the card, the margin appears *above* the card, outside its background. Explain why, and fix it in two different ways: (1) without changing the h2, using one property on the card; (2) by replacing the margins inside the card with a spacing approach that can’t collapse.
<div class="card">
<h2>Margins</h2>
<p>They collapse in surprising ways.</p>
</div>
<style>
.card { background: #eaf0fc; }
.card h2 { margin-top: 2rem; }
/* your code here */
</style><div class="card fix-1">
<h2>Margins</h2>
<p>They collapse in surprising ways.</p>
</div>
<div class="card fix-2">
<h2>Margins</h2>
<p>They collapse in surprising ways.</p>
</div>
<style>
.card { background: #eaf0fc; }
.card h2 { margin-top: 2rem; }
/*
The card has no border, padding or content above the h2, so the h2's
top margin collapses with the card's top margin and ends up outside it.
*/
/* Fix 1: a new block formatting context stops child margins escaping */
.fix-1 { display: flow-root; }
/* Fix 2: padding for the inside, gap between children — neither collapses */
.fix-2 {
display: flex;
flex-direction: column;
gap: 0.75rem;
padding: 2rem 1rem 1rem;
}
.card.fix-2 > * { margin: 0; } /* two classes, so it beats .card h2 */
</style>Summary
- Adjacent vertical margins in normal flow collapse into one: the larger margin wins.
- A first or last child’s margin can collapse through its parent if the parent has no border, padding or content in between.
- Margins of empty elements collapse through them.
- Margins never collapse horizontally, or inside flex and grid containers.
- Prevent collapsing with padding, a border,
display: flow-root, or by spacing children with flex/gridgap.