Learn CSS Visually - CSS explained one diagram at a time - flexbox, grid, the box model and more

Why Do My Margins Collapse? (and How to Stop It)

By · Updated

Disclosure: this page contains Amazon affiliate links. As an Amazon Associate we earn from qualifying purchases, at no extra cost to you.

Picture your header sliding down the page with a strip of white above it that you never asked for, and you, margin sleuth, squinting at the CSS for the culprit.

Margin collapsing is the CSS rule behind most where did that space come from? moments, and it follows four plain rules you can check in seconds.

If you like a printed reference next to the keyboard, the CSS Visual Dictionary paperback on Amazon draws margins and boxes as pictures.

Short answer: Margins collapse because CSS merges the touching top and bottom margins of block boxes into one margin, the size of the largest. It happens between siblings, between a parent and its first or last child, and through empty boxes. Stop it with padding or a border between them, display: flow-root on the parent, or flex or grid with gap.

To-scale before and after diagram of margin collapsing at the top of a page: a blue header holding an h1 with browser default margins. Before, the h1's 21.44 pixel top margin collapses through the header and the body's 8 pixel margin, leaving a 21.44 pixel white gap above the 40 pixel tall header. With display: flow-root on the header, the margins stay inside and the header grows to 82.88 pixels under the body's 8 pixel margin; adding body margin 0 puts the header at the very top of the window.
The classic gap above a header: the h1’s margin collapses out of its parent until display: flow-root keeps it inside.

What the diagram shows, in plain text (header { background: blue; } around an h1 with line-height: 40px, everything else at browser defaults):

What is margin collapsing in CSS?

Margin collapsing is when the top and bottom margins of block boxes touch and merge into one margin the size of the largest, instead of adding up. Two stacked boxes with 30px and 20px margins end up 30px apart, not 50px.

Josh W. Comeau’s The Rules of Margin Collapse shows it with two paragraphs that each have 24px margins: Instead of sitting 48px apart, their 24px margins merge together, occupying the same space!

The rule itself is in CSS 2.1, section 8.3.1, which the W3C CSS Box Model Module Level 3 still points to for the details. It covers only vertical margins: Horizontal margins never collapse.

Why do margins collapse at all?

Margins collapse so text-heavy pages get even spacing without doubling it. Two paragraphs with 1em of margin above and below sit 1em apart, not 2em, so you never get a double gap between blocks that each carry their own margin.

The web.dev Learn CSS spacing module gives the history: This behavior is rooted back to when the web was mostly just documents. It adds that collapsing margins help to set consistent spacing between elements without accidentally creating huge gaps.

The rule is as old as CSS. The CSS1 specification of 1996 already said adjoining vertical margins are collapsed to use the maximum of the margin values.

The browser’s own stylesheet shows why. The WHATWG HTML Standard’s rendering section gives p, blockquote, figure and pre a default margin-block: 1em, and each heading its own em margin. Without collapsing, paragraphs would sit two blank lines apart.

When do margins collapse?

Margins collapse only when both boxes are in-flow block boxes in the same block formatting context and nothing separates them. CSS 2.1 lists four pairs of margins that can touch, and MDN’s Mastering margin collapsing groups them into three basic cases.

Decision diagram with four questions for predicting margin collapse in CSS. One: is either box a flex or grid item, a float, absolutely positioned or inline-block? Two: are they left and right margins? Three: is there padding, a border, text, clearance or a flow-root or overflow edge between them? Four: for a parent's bottom or an empty box, is there a set height or a min-height above zero? Any yes means no collapse and the margins add up; four no answers mean they collapse to the largest positive plus the most negative margin.
Four questions predict margin collapse: any yes keeps the margins separate, four no answers merge them.

The four questions in the diagram, in order:

  1. Is either box a flex or grid item, a float, absolutely positioned, or display: inline-block? Then the margins stay separate and add up.
  2. Are they left and right margins? Horizontal margins never collapse (in a vertical writing mode, it’s the other way round).
  3. Is anything between them: padding, a border, a line of text, clearance past a float, or the edge of a box with display: flow-root or overflow: hidden? Then they stay separate.
  4. For a parent’s bottom margin, or an empty box, is there a set height or a min-height above zero? Then they stay separate.

Four “no” answers mean the margins collapse. CSS 2.1 names the pairs that can touch, and each one has its own section below.

Do the margins of two siblings collapse?

Yes. The bottom margin of a block and the top margin of its next block sibling collapse into the larger of the two, unless the second one clears a float.

With browser defaults, an h2 (24px text, margin-block: 0.83em, so 19.92px) above a paragraph with a 16px top margin leaves 19.92px between them. The paragraph’s 16px sits inside the heading’s margin rather than adding to it.

Why does a child’s margin-top push its parent down?

A first child’s margin-top moves its parent because the parent’s top margin and the child’s top margin collapse into one, and that one margin sits outside the parent. It happens whenever the parent has no top padding, no top border and no text before the child.

MDN’s margin collapsing guide adds the detail that catches everyone: These rules apply even to margins that are zero, so the margin of a descendant ends up outside its parent (according to the rules above) whether or not the parent’s margin is zero.

That’s why a parent with margin: 0 still moves. A child’s margin-top: 24px becomes the parent’s 24px, and the parent’s background starts below it.

Why does a last child’s margin-bottom stick out of its parent?

A last child’s margin-bottom collapses with its parent’s bottom margin when the parent has no bottom padding, no bottom border and an automatic height. The space then lands below the parent’s background instead of inside it.

CSS 2.1 spells out the height condition: the last child’s bottom margin and the parent’s are adjoining if the parent has ‘auto’ computed height. Give the parent a fixed height and this bottom pair stops collapsing.

What happens to the margins of an empty div?

An empty block with no padding, border, height or min-height lets its own top and bottom margins collapse into one, and that margin then merges with its neighbours’ margins. The margins “collapse through” the empty box.

So a spacer <div> with margin: 20px 0 between two paragraphs with 16px margins doesn’t add 40px. All four margins collapse into a single 20px gap.

web.dev describes an empty paragraph with 20px margins the same way: it will only create 20px of space: not 40px. Give the spacer 1px of padding or height and its two margins stop merging.

Why is there a gap above my header or at the top of the page?

A white strip above a coloured header is usually the top margin of the header’s first heading collapsing through the header and the <body>, so it lands above the header’s background. With browser defaults and an h1, that strip is 21.44px tall.

The h1‘s 21.44px (0.67em of 32px), the header’s 0 and the body’s default 8px all touch, because nothing sits between them. They collapse into max(21.44, 0, 8) = 21.44px, and the blue background starts below that.

The <html> element doesn’t join in. CSS 2.1 says Margins of the root element’s box do not collapse. So the merged margin stops at the top of the window, as the diagram at the top of this page shows.

body   { margin: 0; }              /* drop the 8px browser default */
header { display: flow-root; }     /* keep the h1's margins inside */

/* or: no heading margin, padding on the header instead */
header    { padding: 16px 24px; }
header h1 { margin: 0; }

A navbar, a hero section or a card with a heading inside behaves the same way. When a gap shows up above one box, select its first child in DevTools: its orange margin overlay sits outside the parent’s background.

How big is a collapsed margin?

A collapsed margin is the largest positive margin plus the most negative margin among all the margins that touch. With only positive margins the biggest one wins, and with only negative ones the most negative one wins.

CSS 2.1 section 8.3.1 says When two or more margins collapse, the resulting margin width is the maximum of the collapsing margins’ widths. For negatives, the maximum of the absolute values of the negative adjoining margins is deducted from the maximum of the positive adjoining margins.

The same rule covers chains of three or more margins, which is where maths done by hand usually goes wrong. Here are ten stacks worked out:

The gap between stacked blocks in ten setups — calculated by Learn CSS Visually from CSS 2.1 section 8.3.1, MDN’s margin collapsing guide and the WHATWG HTML Standard’s default styles, November 23, 2026
SetupMargins that touchRuleGap
Two paragraphs, browser default margin-block: 1em at 16px16 / 16pxequal: one of them16px, not 32px
h2 (24px text, 0.83em) above a paragraph19.92 / 16pxlargest wins19.92px
h1 first in a header at the top of the page8 (body) / 0 (header) / 21.44px (h1)largest of three21.44px above the header
Section with margin-bottom: 32px, then an article (margin 0) whose first h2 has margin-top: 24px32 / 0 / 24pxlargest of three32px
Paragraph, empty div with margin: 20px 0, paragraph (16px margins)16 / 20 / 20 / 16pxall four collapse through the empty div20px
The same, but the div has height: 1px16 / 20px, then 20 / 16pxtwo separate collapses + the 1px div20 + 1 + 20 = 41px
A margin-bottom: 30px, B margin-top: 20px, B’s first child margin-top: -10px30 / 20 / -10pxlargest positive + most negative30 — 10 = 20px
A 40px, B -15px, B’s first child -25px40 / -15 / -25pxlargest positive + most negative40 — 25 = 15px
A margin-bottom: -8px, B margin-top: -20px-8 / -20pxno positive margin: most negative wins-20px (B overlaps A by 20px)
A 30px and B 20px as items of a flex columnnone: flex items never collapsemargins add up50px

Row six surprises people the most. One pixel of height turns a 20px gap into a 41px gap, because the empty div no longer lets its margins collapse through it.

How do negative margins collapse?

Negative margins collapse by subtraction: take the largest positive margin and subtract the biggest negative one by size. A margin-bottom: 40px meeting a margin-top: -15px leaves a 25px gap.

MDN’s guide covers the all-negative case: When all margins are negative, the size of the collapsed margin is the smallest (most negative) margin. So -8px meeting -20px gives -20px, and the second box overlaps the first by 20px, not 28px.

How do I stop margins from collapsing?

Put something between the margins, or move the boxes into a layout that doesn’t collapse. For a parent and its child, display: flow-root on the parent is the cleanest fix; for a stack of siblings, a flex or grid column with gap replaces the margins altogether.

Here’s a moment almost every newbie knows: you add padding: 1px to a box just to try something, and the mystery gap vanishes. It feels like magic, and it’s really just the four rules on this page doing exactly what they say.

Here’s what each fix does to the header from the diagram, an h1 with a 40px line and 21.44px margins inside a <header>, under the body’s 8px margin:

Nine ways to keep an h1’s margins inside its header — calculated by Learn CSS Visually from CSS 2.1, the W3C CSS Display Level 3, Flexbox, Grid Layout, Containment and Box Model Level 4 specs, November 23, 2026
CSS on the headerHeader heightSpace above the headerSide effects
nothing (the problem)40px21.44pxthe h1’s margins escape at the top and the bottom
padding-block: 1px1 + 21.44 + 40 + 21.44 + 1 = 84.88px8px2px taller; works everywhere
border-block: 1px solid transparent84.88px8px2px taller; the border is invisible
display: flow-root82.88px8pxnone you can see; it also contains floats
overflow: hidden82.88px8pxclips anything that sticks out, like dropdown menus and children’s shadows
overflow: auto82.88px8pxshows a scrollbar if content overflows
display: flex; flex-direction: column82.88px8pxchildren become flex items, so their margins stop collapsing with each other too
display: grid82.88px8pxchildren become grid items, same as flex
contain: layout82.88px8pxalso a stacking context and the containing block for absolute and fixed children
margin-trim: block40px8pxremoves the h1’s margins at the header’s edges instead; not in Firefox yet

display: inline-block also stops the collapse, but it shrinks the header to the width of its text, so it’s rarely what you want for a full-width block.

Is display: flow-root the best way to stop margin collapse?

For a parent and its children, yes. display: flow-root changes nothing you can see; it only gives the box a new block formatting context, which is exactly what stops its margins merging with its children’s.

The W3C CSS Display Module Level 3 (Candidate Recommendation Draft, 5 June 2026) says a flow-root element always establishes a new block formatting context for its contents. Its glossary describes such boxes as ones that contain internal floats, exclude external floats, and suppress margin collapsing.

flow-root works in Chrome 58, Edge 79, Firefox 53 and Safari 13, which Can I use counts as 97.33% of users. It seals a box off from its children only: the box’s own margins still collapse with its siblings’ margins.

Does padding stop margin collapse?

Yes. Any padding on the parent, even 1px, keeps the parent’s margin and its child’s margin apart, and so does a border. CSS 2.1 makes it a condition: margins only touch when no line boxes, no clearance, no padding and no border separate them.

Padding only blocks the side it’s on. padding-top: 1px keeps the first child’s top margin inside, but the last child’s bottom margin still escapes until you add padding-bottom too. That’s why the fixes table uses padding-block.

Padding on the child doesn’t help, because the child’s margin sits outside its own padding. The padding has to be on the parent, between the two margins.

Why does overflow: hidden stop margin collapse?

Any overflow value other than visible turns the box into a scroll container with a new block formatting context. CSS 2.1 says boxes like that, such as floats and elements with ‘overflow’ other than ‘visible’, do not collapse their margins with their in-flow children.

Josh W. Comeau puts it in one line: margins can’t collapse across scroll container boundaries. The catch is clipping: overflow: hidden cuts off dropdown menus and shadows that stick out, and overflow: auto can add a scrollbar. Reach for flow-root unless you also want the clipping.

Why are my margins adding up instead of collapsing?

If 30px and 20px margins give you a 50px gap, the boxes are almost always flex or grid items, floats, absolutely positioned, or inline-blocks. Margins only collapse between in-flow block boxes in normal flow, so in those layouts every margin counts in full.

Something between the margins does the same: a border, padding, a line of text, or clearance past a float. And left and right margins never collapse at all, so two floated boxes with 10px side margins always sit 20px apart.

Do margins collapse in flexbox and grid?

No. Margins never collapse inside a flex or grid container, neither between the items nor between the container and its items.

The W3C Flexbox spec says The margins of adjacent flex items do not collapse. It adds that the flex container’s margins do not collapse with the margins of its contents.

The W3C CSS Grid Layout Level 2 spec says the same of grid items and grid containers. So a vertical stack made with display: flex; flex-direction: column gives 30 + 20 = 50px where plain blocks gave 30px.

In flex and grid, drop the margins and use gap. Our guides to margin vs padding and gap, how flexbox works and how CSS Grid works show it in action.

Do margins collapse in a table?

Table cells and rows don’t take margins at all, so nothing collapses inside a table. CSS 2.1 says margins apply to all elements except elements with table display types other than table-caption, table and inline-table.

The table itself does have margins. CSS 2.1 section 17.4 puts them on the table’s wrapper box, a normal block on the page, so a table’s top margin still collapses with the paragraph above it.

That wrapper box establishes a block formatting context, so nothing inside the table merges with those margins.

If you searched because of the double lines between cells, that’s a different property: border-collapse: collapse merges neighbouring cell borders and has nothing to do with margins.

Do margins collapse in vertical writing modes?

Yes, but sideways. Margins collapse in the block direction, which runs top to bottom in English and right to left in vertical Japanese or Chinese set with writing-mode: vertical-rl.

The W3C CSS Writing Modes Level 4 spec says The margin collapsing rules apply exactly with the block-start margin substituted for the top margin and the block-end margin substituted for the bottom margin. In vertical-rl, the right and left margins are the ones that collapse.

Does a height or min-height stop margins from collapsing?

A set height stops the bottom margins collapsing, but not the top ones. CSS 2.1 only lets a last child’s bottom margin collapse with its parent’s when the parent’s height is auto and its min-height is zero; the top pair ignores height.

So a parent with height: 300px still lets its first child’s margin-top escape. Josh W. Comeau describes the empty space under the child as like a moat filled with hungry piranhas: it keeps the two bottom margins apart.

An empty box works the same way. Give it a height other than 0 or auto, or a min-height above zero, and its own top and bottom margins stop collapsing into one.

Should I avoid margin collapse in my CSS?

You don’t need to fight margin collapse, just make it predictable. It only surprises you when margins come from both directions, or from a child at a parent’s edge, so a simple spacing habit removes almost every surprise:

  • Space in one direction. Give blocks only a top margin, for example .flow > * + * { margin-block-start: 1.5em; }, so two margins rarely meet.
  • Padding at a container’s edges. Space inside a card or section belongs to the container’s padding, which also gives a first child’s margin something to stop against.
  • gap in flex and grid stacks. No margins, no collapsing, no :last-child fixes.
  • Trim the edges. margin-trim: block drops children’s margins at a container’s top and bottom edges in Safari 16.4 and Chrome and Edge 155. Until Firefox adds it, .card > :first-child { margin-top: 0; } does the same job.

Our CSS box model guide draws where margins sit around a box, the outside layer that makes collapsing possible.

Do flow-root, gap and margin-trim work in every browser?

Margin collapsing itself works the same in every browser, because it has been in CSS since CSS1. The fixes are all safe today except margin-trim. These versions are from MDN’s browser-compat-data (release 8.1.10), read on November 23, 2026:

display: flow-root
Chrome 58, Edge 79, Firefox 53, Safari 13 (97.33% of users on Can I use)
display: flex
Chrome 29, Edge 12, Firefox 20, Safari 9
gap in flexbox
Chrome 84, Edge 84, Firefox 63, Safari 14.1
contain: layout
Chrome 52, Edge 79, Firefox 69, Safari 15.4
margin-block (one-direction spacing)
Chrome 87, Edge 87, Firefox 66, Safari 14.1
margin-trim
Chrome 155, Edge 155, Safari 16.4; not yet in Firefox

Next time a gap appears out of nowhere, run the four questions from the diagram before you touch a single margin. Odds are you’ll find a heading margin poking out of its parent, and one line of display: flow-root will tuck it right back in.

Sources and further reading

Questions and answers

Does padding stop margin collapse?

Yes. Padding on the parent, even 1px, keeps the parent’s margin and its child’s margin apart, and a border does the same. Padding only works on the side it’s on, so use padding-block to stop both the top and the bottom margins escaping.

Does margin collapse happen in flexbox?

No. Flex items’ margins never collapse with each other or with the flex container, and the same is true in grid. Margins of 30px and 20px give a 50px gap in a flex column, so use gap there instead of margins.

Why is margin collapse not working?

Margins only collapse between in-flow block boxes in normal flow with nothing between them. If yours add up, check for display: flex or grid on the parent, floats, absolute positioning, inline-block, padding or a border in between, or left and right margins, which never collapse.

Do margins collapse in a table?

Table cells and rows don’t take margins, so nothing collapses inside a table. The table’s own margins sit on its wrapper box and still collapse with the blocks above and below it. border-collapse is a separate property for cell borders.

Why does margin collapse happen?

It keeps spacing even on text-heavy pages: two paragraphs with 1em margins sit 1em apart, not 2em. The rule has been in CSS since CSS1 in 1996, when web pages were mostly headings and paragraphs.

How this was written: Written from CSS 2.1 section 8.3.1 (Collapsing margins, still the rule the W3C CSS Box Model Module Level 3 points to) and section 17.4 (tables), CSS1 section 4.1.1, the W3C CSS Display Module Level 3 (Candidate Recommendation Draft, 5 June 2026), the W3C Flexbox, Grid Layout Level 2, Containment Level 2 and Writing Modes Level 4 specs, the WHATWG HTML Standard’s rendering section (browser default margins), MDN Web Docs’ Mastering margin collapsing and margin reference, web.dev’s Learn CSS spacing module, Josh W. Comeau’s "The Rules of Margin Collapse", MDN’s browser-compat-data and Can I use, all read on November 23, 2026, plus Google and Bing autocomplete for "margin collapse". Every size in the diagram and tables is our own arithmetic from those rules. Code samples are simplified for learning.

Oh, one more thing…

If this helped, you’ll like our free updates: the insights we discover while researching, and the moments worth sharing with other people learning CSS — no spam, no promotions.

One short email when there’s something worth your time. Unsubscribe any time.