You set a bottom margin on one paragraph and a top margin on the next, and the gap between them is not the sum of the two.
It is the larger of them. This behaviour has a name, and it is called collapsing margins. It is why a gap you expected to be 40 pixels turns out to be 20.
The previous lesson in this module covered "Why Do Vertical Margins Collapse Together?", and the next lesson in this module is about "How Do Borders, Shadows and Radius Polish a Box?".
What collapses, and what does not?
The vertical margins between block-level elements collapse. The space between them becomes the larger of the two margins and not their sum, and the result is called a collapsed margin.
.first { margin-bottom: 30px; }
.second { margin-top: 20px; }
/* the gap between them is 30px, not 50px */There are three situations that are involved.
Two adjacent siblings with no border, padding, or clearance between them collapse.
A parent's top margin collapses with its first child's top margin when nothing separates them.
And a parent's bottom margin collapses with its last child's bottom margin for the same reason.
<div class="parent">
<p class="first">First paragraph</p>
</div>
<div class="next">Next block</div>.parent { margin-bottom: 40px; }
.first { margin-top: 20px; }Here the child's 20 pixel top margin collapses out through the parent, and the gap between the two blocks is 40 pixels and not 60.
The child appears to have moved up out of its parent, and the background of the parent does not stretch to cover that space.
What prevents collapsing?
Anything that separates the margins vertically.
A border, a padding, or a clearance on the parent stops the parent's margin from collapsing with its children's.
A flex or a grid container stops the collapsing between its children entirely.
/* padding on the parent keeps the child's margin inside */
.parent { padding: 1px; }
/* or a flex container: no collapsing between flex items */
.parent { display: flex; flex-direction: column; }
/* or overflow other than visible */
.parent { overflow: auto; }Flex and grid are the modern answer, because they solve the problem structurally. In a flex column, the space between two items is their margins added together, exactly as you would expect.
That is one practical reason people use flexbox for vertical stacks instead of plain block layout.
Why does it work this way at all?
Because the model is built around the idea of a single gap between two boxes.
If every pair of margins added up, then a stack of paragraphs with a 20 pixel top and bottom margin each would have 40 pixels between every pair, and the space at the top and the bottom of the document would be missing.
Collapsing gives you one consistent gap, and it removes the outer gaps. That is why the top margin of the first element often appears to move the whole page down instead of adding space above the text.
The behaviour has a second effect that surprises people. A block with a margin-top larger than the height of the viewport pushes the page down, and the space above it cannot be scrolled to.
That is the "margin at the top of the page" problem, and the fix is to put the spacing on the parent instead.
/* instead of a top margin on the first element */
h1 { margin-top: 2rem; }
/* put the space on the container */
.page { padding-top: 2rem; }How do I work out where a gap came from?
Turn on the box model overlay, and hover over the elements.
The overlay draws the margin areas, and when the two margin areas overlap you can see the collapse happening. The two translucent bands sit on top of each other rather than stacking.
sibling margins
30px └──┐
├── one collapsed gap of 30px
20px ┌──┘
parent and child
40px └──┐
20px ┌──┘ the child's margin has escaped the parentThere are three fixes that cover nearly every case.
Use flex or grid for the container, which removes the collapsing between the items.
Add a small padding or a border to the parent when you need a child's margin to stay inside it.
Or use the gap property in a flex or a grid container, which is explicit about the space and never collapses.
/* the clearest way to space a stack */
.stack {
display: flex;
flex-direction: column;
gap: 1rem;
}How do you use collapsing margins deliberately?
Collapsing is usually something you fight, but it is also a feature you can lean on.
Because two adjoining vertical margins collapse into one, a stack of elements with margin-bottom: 1rem each is separated by exactly 1rem and not 2rem.
That gives you an even rhythm without a wrapper element or a gap property to maintain.
<section class="article">
<h2>Heading</h2>
<p>First paragraph.</p>
<p>Second paragraph.</p>
</section>.article h2 {
margin-block: 0 0.5rem;
}
.article p {
margin-block: 0 1rem;
}
/* the gap between two paragraphs is 1rem, not 2rem */
.article p + p {
margin-block-start: 1rem;
}The collapsing rule only applies to adjoining margins. That means margins with no padding, border, content, or clearance between them.
The margins on the same element always add up, and margins that are separated by anything else stay separate.
So the collapse happens between the bottom margin of one paragraph and the top margin of the next, and never between two margins of the same element.
There are two things that prevent a collapse, and both are useful to know on purpose.
A parent with no padding, no border, no height, and no content of its own lets the top margin of its first child and the bottom margin of its last child escape out through it, so the parent ends up in the wrong place.
Adding overflow: hidden, a border, or display: flow-root on the parent stops the escape. The flow-root is the cleanest of the three, because it says exactly what it means. It makes that element a block formatting context.
Quick reference
- Adjacent vertical margins collapse. The gap is the larger of the two and not the sum.
- A parent's margin can collapse with the first and the last child's.
- Padding, a border, or a flex or grid container stops the collapsing.
- In flex and grid the margins add normally, and the gap is usually clearer.
- A top margin on the first element moves the page down instead of adding space above it. Pad the container instead.
Try it yourself: put two paragraphs in a column with a 30 pixel bottom margin on the first and a 20 pixel top margin on the second. Measure the gap. Then change the container to display: flex; flex-direction: column and measure again. The gap should now be 50 pixels, because flex items do not collapse.