Sub topic 6
When vertical margins collapse
This chapter assumes you know the CSS box model, the difference between padding and margin, and rem units. Margin collapsing is a rule that surprises people when they space groups of content. Once you know it, you can predict it.
The rule
When the vertical margins of two block boxes touch, the browser merges them into one margin instead of adding them. This is margin collapsing. It applies only to vertical margins (top and bottom) in normal flow. Horizontal margins never collapse.
.box-a { margin-bottom: 2rem; }
.box-b { margin-top: 1rem; }If .box-a sits directly above .box-b as a block sibling, the gap between them is 2rem, not 3rem. The larger margin wins and the smaller one is absorbed.
The size of the merged margin depends on the signs:
| Margins that touch | Resulting space |
|---|---|
| Both positive | The larger value |
| Both negative | The most negative value |
| One positive, one negative | The two values added together |
Where collapsing happens
Collapsing happens in three situations:
- Adjacent siblings, as in the example above.
- A parent and its first or last child, when nothing separates their margins. There must be no padding, border or content between them.
- An empty block, whose own top and bottom margins collapse together.
The second case causes the most confusion. If a child has margin-top: 2rem and its parent has no padding or border, the child's margin escapes the parent. The parent moves down, and its background does not grow to include the gap.
<div class="parent">
<p class="child">Hello</p>
</div>
<style>
.parent { background: #eef; }
.child { margin-top: 2rem; }
</style>You expect a blue box with 2rem of space above the text inside it. Instead the blue box starts 2rem lower, and the text touches its top edge.
What stops collapsing
Collapsing does not happen in these cases:
- Flex and grid items. Their margins never collapse with siblings or with the container.
- Floated or absolutely positioned boxes.
- Padding or a border between a parent and its child.
- A new block formatting context on the parent.
display: flow-rootcreates one without side effects, so it is the cleanest fix for the parent and child case.
.parent {
display: flow-root;
}With this rule on .parent, the child's 2rem margin stays inside the blue box.
Why this matters for grouping
The proximity principle depends on predictable distances. Suppose you give headings margin-top: 3rem and paragraphs margin-bottom: 1.5rem. You may expect 4.5rem between a paragraph and the next heading, but you get 3rem. The groups still read correctly, but your numbers no longer match what you see. The mismatch gets worse when a component's margin collapses through its wrapper.
Three habits keep this under control:
- Space in one direction only. Use
margin-block-starton siblings, as in the flow rule from the previous chapter, so there is only one margin at each boundary. - Use
gapinside flex and grid containers, where collapsing cannot occur. - Use padding on containers when you need space inside them, because padding never collapses.
Common errors
- Adding
margin-topto the first child to push content away from the parent's edge, then seeing the whole parent move. - Expecting two margins to add up, then measuring a smaller gap.
- Using negative margins to pull elements together without checking the mixed-sign rule.
- Adding
overflow: hiddento stop collapsing. It works, but it also clips shadows and focus outlines. Preferdisplay: flow-root.
Exercise
A .section has no padding or border. Its first child is a heading with margin-top: 2rem, and the section sits below a paragraph with margin-bottom: 1rem. How much space appears between the paragraph and the heading, and what one-line change keeps the heading's margin inside the section?
Show answer
The paragraph's bottom margin, the section's top margin (0) and the heading's top margin all touch, so they collapse to the largest value: 2rem. The heading's margin escapes the section, so the section's box starts 2rem lower. Adding display: flow-root to .section stops the heading margin escaping. The section then has its own 2rem inside, and the paragraph's 1rem sits outside it, so the visible gap between paragraph and heading is 3rem.