Sub topic 9
Margin collapsing
This chapter assumes you know the four box layers (content, padding, border, margin), how box-sizing changes width calculations, and the difference between block and inline display. It covers the one place where the box model's arithmetic does not simply add up: vertical margins that merge.
What margin collapsing is
Margin collapsing happens when two vertical margins touch and the browser combines them into a single margin instead of adding them. Margins that merge this way are called adjoining margins.
Here is the case from the spine:
<style>
h2 { margin-bottom: 24px; }
p { margin-top: 16px; }
</style>
<h2>Heading</h2>
<p>Paragraph</p>You might expect a 40px gap (24 + 16). The gap is 24px. The two margins collapse, and the larger one wins.
Collapsing is defined for block-level boxes in normal flow. It affects only the vertical direction (the block axis in horizontal writing modes). Left and right margins never collapse.
The three situations where margins collapse
1. Adjacent siblings
The bottom margin of one block and the top margin of the next block sibling collapse. This is the heading-and-paragraph case above.
2. A parent and its first or last child
A parent's top margin collapses with its first child's top margin when nothing separates them. The same applies to the parent's bottom margin and its last child's bottom margin. Nothing separates them when the parent has:
- no padding on that side,
- no border on that side,
- no line boxes or clearance between them,
- and, for the bottom edge, an automatic
heightwith nomin-heightforcing extra size.
The parent and child margins then merge, and the combined margin sits outside the parent. This surprises people. If you give a child margin-top: 2rem inside a parent with no padding or border, the child does not move down inside the parent. The parent moves down, and its background starts below the gap.
<style>
.container { background: #cde; }
.container > h1 { margin-top: 2rem; }
</style>
<div class="container">
<h1>Title</h1>
</div>In this page the blue background starts at the top of the h1's text box. The 2rem gap appears above the blue area, not inside it.
3. Empty blocks
A block with no content, no padding, no border, and automatic height has its own top and bottom margins collapse together. That merged margin can then collapse with neighbouring margins as well.
<style>
.above { margin-bottom: 20px; }
.empty { margin-top: 30px; margin-bottom: 10px; }
.below { margin-top: 15px; }
</style>
<p class="above">Above</p>
<div class="empty"></div>
<p class="below">Below</p>The empty div's margins (30 and 10) collapse to 30. That result then collapses with 20 above and 15 below. The gap between the two paragraphs is 30px, because all four margins are adjoining.
The collapsing arithmetic
The result depends on the signs of the margins involved.
| Margins involved | Collapsed result | Example |
|---|---|---|
| All positive | The largest value | 24 and 16 give 24 |
| All negative | The most negative value (largest absolute value) | -10 and -30 give -30 |
| Mixed signs | Largest positive plus most negative | 30 and -10 give 20 |
| Equal values | That value, once | 20 and 20 give 20 |
With more than two margins, the rule applies to the whole group. Take margins of 20, 30, 10 and -5. The largest positive is 30 and the most negative is -5, so the result is 25.
Collapsing is not the same as one margin overriding the other. The margins merge into one shared space, and neither box "owns" it.
When margins do not collapse
Margins stay separate in these cases:
- Horizontal margins. Collapsing is a vertical-only behaviour.
- Floated elements. A float's margins never collapse with anything.
- Absolutely positioned elements. Margins of
position: absoluteandfixedboxes do not collapse. - Inline-block elements. Their margins do not collapse with neighbours or with their contents.
- Flex and grid items. Margins of flex items and grid items do not collapse with each other or with their container's margins.
- Boxes that establish a new block formatting context. The root element, elements with
overflowother thanvisible,display: flow-root, and similar boxes keep their margins from collapsing with their children. - Parent and child with a separator. Any padding or border between them prevents the parent-child collapse.
A subtle point: the flex or grid container's own margins can still collapse with its siblings, because the container itself is a block-level box. Only the items inside it are exempt.
<style>
.row { display: flex; flex-direction: column; }
.row > div { margin: 20px 0; background: #eee; }
</style>
<div class="row">
<div>One</div>
<div>Two</div>
</div>Here the gap between One and Two is 40px, not 20px, because flex items do not collapse.
Preventing unwanted collapse
Pick the least invasive fix that matches your intent.
- Add padding or a border to the parent. Even
padding-top: 1pxstops the parent-child collapse, but it adds visible space. - Use
display: flow-rooton the parent. This creates a new block formatting context and has no visual side effects. It is usually the best fix. - Set
overflowto something other thanvisible. This works, but it can clip content such as shadows or dropdowns. - Make the parent a flex or grid container. The children become items, so their margins no longer collapse with the parent.
.container {
display: flow-root;
}
.container > h1 {
margin-top: 2rem;
}With flow-root, the 2rem margin stays inside the container. The container's background now covers the gap.
A later chapter explains block formatting contexts in detail. For now, treat flow-root as the switch that contains a box's margins.
Designing spacing so collapse rarely matters
Many teams avoid the whole problem with a spacing convention:
- Margin in one direction only. Give elements
margin-bottom(ormargin-top) and never both. Two margins in the same direction never meet in a way that surprises you. - Use
gapin flex and grid layouts.gapadds space between items, and it never collapses. - Put spacing on a wrapper. Use padding on containers rather than margin on their first and last children.
.stack > * + * {
margin-block-start: 1rem;
}This "lobotomized owl" selector adds space only between consecutive siblings. It uses a logical property, so it follows the writing direction.
Debugging a collapse
Collapsed margins are easy to misdiagnose, because the browser does not report that a collapse happened.
- Select the element in developer tools. The orange margin overlay shows each box's margin area.
- Look for margin overlays that overlap or that extend outside the parent. A child whose margin overlay sits above the parent's border edge has collapsed through the parent.
- Toggle
display: flow-rooton the parent in the Styles pane. If the layout shifts, the cause was a parent-child collapse. - Temporarily add
border: 1px solid redto the parent. If the gap moves inside, you have confirmed it.
Common mistakes:
- Adding
margin-topto a first child and wondering why the parent moves. - Expecting
margin-bottom: 24pxplusmargin-top: 16pxto produce 40px. - Adding
paddingto fix a collapse, then forgetting that padding is included in the parent's size. - Assuming flex items collapse like block siblings.
Summary of the behaviour
- Vertical margins of block boxes in normal flow collapse when they touch.
- The three cases are siblings, parent and first or last child, and empty blocks.
- Positive margins give the largest value. Negative margins give the most negative value. Mixed signs add the largest positive and the most negative.
- Floats, absolutely positioned boxes, inline-blocks, flex items, grid items, and boxes that create a new block formatting context do not collapse.
- Use
display: flow-rootor a one-direction margin convention to avoid surprises.
Exercise
A section has no padding or border and margin-top: 40px. Its first child, an h2, has margin-top: 24px. The section follows a div with margin-bottom: 10px. What is the gap between the div and the top of the h2, and how would you keep the h2's 24px margin inside the section?
Show answer
All three margins (10, 40, and 24) are adjoining, so they collapse to the largest, 40px. The h2 sits 40px below the div, and the section has no visible internal gap. To keep the h2's margin inside the section, set display: flow-root on the section (or add padding or a border). Then the section's 40px collapses with the div's 10px to give 40px, and the h2's 24px stays inside, so the h2 sits 64px below the div.
Exercise
Two block siblings have margin-bottom: 30px and margin-top: -10px. What is the gap between them? What changes if you make their parent a flex column container?
Show answer
Mixed signs add the largest positive and the most negative, so the gap is 30 + (-10) = 20px. In a flex column container the items no longer collapse, so the margins simply add: 30 + (-10) = 20px as well. The result is the same here only because the arithmetic coincides. With two positive margins such as 30px and 10px, the block gap would be 30px and the flex gap would be 40px.