12k
All articles

How CSS Margin Collapsing Works

See how CSS margin collapsing affects sibling and parent-child spacing, how negative margins combine, and when flow-root or flex and grid gap prevent unexpected gaps.

OpenReplay Team
OpenReplay Team
How CSS Margin Collapsing Works

When the vertical margins of two block-level boxes touch in normal flow, the browser merges them into a single margin equal to the larger of the two. Stacking margin-bottom: 30px on top of margin-top: 20px therefore gives a 30px gap, not 50px.

You set both margins and measure the gap in devtools, and it’s smaller than their sum. Or you add a top margin to a child and the parent’s background moves down with it, so the space ends up outside the parent instead of inside.

Each of these is a separate collapse case with its own rule. This article covers every case with pixel values you can check yourself and gives the modern fix for each.

Key Takeaways

  • Adjacent vertical margins in normal flow collapse into one margin equal to the largest of them, so 30px and 20px produce a 30px gap.
  • A first child’s top margin escapes its parent unless the parent has a top border, top padding, inline content before the child, or clearance. display: flow-root on the parent keeps the margin inside.
  • When positive and negative margins collapse, the result is the largest positive plus the most negative, so 30px and -10px give 20px. When all are negative, the most negative wins.
  • Margins never collapse between flex items or grid items, so gap on a flex or grid container gives predictable spacing.

What Is CSS Margin Collapse?

CSS margin collapse is the browser merging adjoining vertical margins into a single margin, and it applies only to block-direction margins in normal flow. The CSS 2 collapsing margins rules state that horizontal margins never collapse. In a horizontal writing mode, top and bottom margins can collapse, while left and right margins never do. MDN’s margin collapsing guide lists three situations where it happens: adjacent siblings, a parent and its first or last child, and empty blocks.

Sometimes the extra space is above or below text rather than between boxes. In that case the cause is usually the font’s line box, not margins, and fixing extra space around text in CSS is the relevant guide.

Adjacent Siblings: The Larger Margin Wins

Fix: give flow content margins on one side only, or use gap on a flex or grid parent.

When one block’s bottom margin meets the next block’s top margin, the browser keeps only the larger value. If both are equal, you get that value once.

<style>
  .a { margin-bottom: 30px; }
  .b { margin-top: 20px; }
</style>
<div class="a">A</div>
<div class="b">B</div>
<!-- gap between A and B = max(30, 20) = 30px, not 50px -->

Setting margins in only one direction stops two margins from meeting in the first place:

/* Every flow element: no start margin, 24px end margin */
h2, p, ul { margin-block: 0 24px; }
/* Gap between any two siblings = 24px, always */

MDN notes one exception: siblings don’t collapse when the second one has to be cleared past floats.

Parent and First or Last Child

Fix: put display: flow-root on the parent.

Margin collapse also happens across nesting levels, between a parent and its first or last child. A first child’s top margin escapes its parent when the parent has no border-top, no padding-top, no inline content (such as text) before the child, and no clearance. Zero margins still take part in collapse. This is how a child’s margin-top can push the parent down even though the parent has no margin of its own.

<style>
  section { background: #eef; }        /* no padding, no border */
  section p { margin-top: 24px; }
  /* Result: the 24px sits ABOVE section's background */

  section.fixed { display: flow-root; }
  /* Result: the 24px sits INSIDE the background */
</style>
<section><p>Text</p></section>
<section class="fixed"><p>Text</p></section>

display: flow-root creates a block formatting context, and margins don’t collapse across a BFC boundary.

The bottom edge works the same way, with two more blockers. A last child’s bottom margin escapes unless the parent has border-bottom, padding-bottom, height, or min-height:

.parent { min-height: 100px; }
.parent > :last-child { margin-bottom: 24px; }
/* Bottom margin stays inside .parent.
   The first child's top margin still escapes:
   min-height does not block top-edge collapse. */

Empty Blocks Collapse Through Themselves

Fix: delete spacer elements and put the spacing on a flex or grid parent with gap.

An empty block collapses through itself: if a block has no border, padding, inline content, height, or min-height, its own top and bottom margins collapse into one. That single margin then collapses with the margins of the blocks on either side. Properties outside this list, such as width or background, don’t stop it.

<style>
  p { margin: 16px 0; }
  .spacer { margin: 20px 0; }  /* empty div */
</style>
<p>One</p>
<div class="spacer"></div>
<p>Two</p>
<!-- 16, 20, 20, 16 all collapse: gap = 20px, not 72px -->

To see the boundary, add padding-top: 1px to .spacer. The gap becomes 41px: 20px above the padding, 1px of padding, and 20px below. This shows how the rule works. It isn’t a fix.

How Do Negative Margins Collapse?

Fix: nothing needs fixing. This is arithmetic, and the result is predictable.

When positive and negative margins collapse together, the result is the largest positive margin plus the most negative one. When every margin involved is negative, the most negative one wins.

/* Mixed: 30 + (-10) = 20px gap */
.a { margin-bottom: 30px; }  .b { margin-top: -10px; }

/* All negative: min(-10, -25) = -25px, boxes overlap by 25px */
.c { margin-bottom: -10px; } .d { margin-top: -25px; }

/* Several at once: 40, 10, -15, -5 → 40 + (-15) = 25px */

When Do Margins Not Collapse?

Margins only collapse between block-level boxes in normal flow. Anything that takes a box out of that context, or puts something between the margins, stops the collapse.

ConditionWhy collapse stops
Flex item or grid itemMargins inside flex and grid containers don’t collapse
FloatFloated boxes never collapse margins
position: absoluteAbsolutely positioned boxes are out of flow
Border or padding between the marginsThe margins are no longer adjoining
Inline content between themA line box separates the margins
Parent establishes a BFCdisplay: flow-root, or overflow: auto/hidden
Sibling with clearanceClearance separates the sibling margins

overflow: auto or hidden also creates a BFC, but it can clip content or add scrollbars. That makes flow-root the safer choice.

The Flexbox specification says a flex container’s margins don’t collapse with the margins of its contents, and adjacent flex items don’t collapse either. The Grid specification says the same about adjacent grid items. Two flex items in a column, each with margin: 20px 0, sit 40px apart. In normal flow they would be 20px apart. Clear the item margins and use gap instead:

.stack { display: flex; flex-direction: column; gap: 24px; }
.stack > * { margin: 0; }
/* Gap between items = 24px, no collapse rules involved */

Choosing the Right Fix

Find your symptom in the left column:

SymptomCaseFix
Gap smaller than the sum of the marginsAdjacent siblingsOne-direction margins, or gap
Child margin pushes the parent downParent and first childdisplay: flow-root on the parent
Space below the last child appears outside the parentParent and last childdisplay: flow-root on the parent
Spacer element adds less space than expectedEmpty blockRemove the spacer and use gap

Margin collapse follows a small set of fixed rules: the largest margin wins, negative margins are added to it, and any border, padding, inline content, or new formatting context breaks the chain. Open devtools, find the case that matches your gap, and apply its fix. After that, fixing headline breaks with text-wrap is a good next step for the text inside those boxes.

FAQs

Do margins of inline-block elements collapse?

No. CSS 2 section 8.3.1 states that margins of inline-block boxes do not collapse, not even with their own in-flow children. An inline-block also establishes a new block formatting context, so a child's top margin stays inside it. The same section excludes the root element too: margins on the html element's box never collapse.

Which properties besides display: flow-root stop a child's margin from escaping its parent?

Any property that makes the parent establish a new block formatting context works. MDN lists display: inline-block, overflow values other than visible and clip, contain set to layout, content, or paint, and multicol containers where column-count or column-width is not auto, including column-count: 1. Flex and grid containers also suppress margin collapsing. Each option changes other layout behavior, which flow-root avoids.

Why does the first heading leave a gap at the top of the page?

Browsers give body a default 8px margin, and when body has no top border or padding, the first heading's top margin collapses with it. The larger value wins, so the gap equals the heading's margin, not 8px plus that margin. Setting body margin to 0 does not remove the gap alone, because the heading's margin still escapes through body. Set the heading's margin-top to 0 as well.

Why does margin-top have no effect on a span or other inline element?

Vertical margins have no effect on non-replaced inline elements such as span, a, or em, so margin-top and margin-bottom on them neither collapse nor move surrounding lines. Horizontal margins on the same elements do apply. To make vertical margins take effect, change the element to display: inline-block or block. An inline-block's margins then apply in full and never collapse with neighbors.

Open-source session replay

Gain control over your UX

See how users are using your site as if you were sitting next to them, learn and iterate faster with OpenReplay — the open-source session replay tool for developers. Self-host it in minutes, and have complete control over your customer data.

Star on GitHub12k

We use cookies to improve your experience. By using our site, you accept cookies.