Why Do My Margins Collapse? (and How to Stop It)
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.
What the diagram shows, in plain text (header { background: blue; } around an h1 with line-height: 40px, everything else at browser defaults):
- Browser defaults from the WHATWG HTML Standard:
bodyhas an 8px margin, and anh1is 2em (32px) withmargin-block: 0.67em, so 0.67 x 32 = 21.44px. - Before: the h1’s 21.44px, the header’s 0 and the body’s 8px top margins collapse into one, max(21.44, 0, 8) = 21.44px of white above the blue. The header is only 40px tall, and the h1’s 21.44px bottom margin escapes below it too.
- Fix,
header { display: flow-root; }: both h1 margins stay inside, so the header is 21.44 + 40 + 21.44 = 82.88px tall with the body’s 8px above it. - Fix plus
body { margin: 0; }: the header touches the top of the window, and the first paragraph starts 82.88 + 16 = 98.88px down.
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.
The four questions in the diagram, in order:
- Is either box a flex or grid item, a float, absolutely positioned, or
display: inline-block? Then the margins stay separate and add up. - Are they left and right margins? Horizontal margins never collapse (in a vertical writing mode, it’s the other way round).
- Is anything between them: padding, a border, a line of text, clearance past a float, or the edge of a box with
display: flow-rootoroverflow: hidden? Then they stay separate. - For a parent’s bottom margin, or an empty box, is there a set
heightor amin-heightabove 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:
| Setup | Margins that touch | Rule | Gap |
|---|---|---|---|
Two paragraphs, browser default margin-block: 1em at 16px | 16 / 16px | equal: one of them | 16px, not 32px |
h2 (24px text, 0.83em) above a paragraph | 19.92 / 16px | largest wins | 19.92px |
h1 first in a header at the top of the page | 8 (body) / 0 (header) / 21.44px (h1) | largest of three | 21.44px above the header |
Section with margin-bottom: 32px, then an article (margin 0) whose first h2 has margin-top: 24px | 32 / 0 / 24px | largest of three | 32px |
Paragraph, empty div with margin: 20px 0, paragraph (16px margins) | 16 / 20 / 20 / 16px | all four collapse through the empty div | 20px |
The same, but the div has height: 1px | 16 / 20px, then 20 / 16px | two separate collapses + the 1px div | 20 + 1 + 20 = 41px |
A margin-bottom: 30px, B margin-top: 20px, B’s first child margin-top: -10px | 30 / 20 / -10px | largest positive + most negative | 30 — 10 = 20px |
| A 40px, B -15px, B’s first child -25px | 40 / -15 / -25px | largest positive + most negative | 40 — 25 = 15px |
A margin-bottom: -8px, B margin-top: -20px | -8 / -20px | no positive margin: most negative wins | -20px (B overlaps A by 20px) |
| A 30px and B 20px as items of a flex column | none: flex items never collapse | margins add up | 50px |
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:
| CSS on the header | Header height | Space above the header | Side effects |
|---|---|---|---|
| nothing (the problem) | 40px | 21.44px | the h1’s margins escape at the top and the bottom |
padding-block: 1px | 1 + 21.44 + 40 + 21.44 + 1 = 84.88px | 8px | 2px taller; works everywhere |
border-block: 1px solid transparent | 84.88px | 8px | 2px taller; the border is invisible |
display: flow-root | 82.88px | 8px | none you can see; it also contains floats |
overflow: hidden | 82.88px | 8px | clips anything that sticks out, like dropdown menus and children’s shadows |
overflow: auto | 82.88px | 8px | shows a scrollbar if content overflows |
display: flex; flex-direction: column | 82.88px | 8px | children become flex items, so their margins stop collapsing with each other too |
display: grid | 82.88px | 8px | children become grid items, same as flex |
contain: layout | 82.88px | 8px | also a stacking context and the containing block for absolute and fixed children |
margin-trim: block | 40px | 8px | removes 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.
gapin flex and grid stacks. No margins, no collapsing, no:last-childfixes.- Trim the edges.
margin-trim: blockdrops 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
gapin 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
- W3C: CSS 2.1, section 8.3.1 Collapsing margins — the adjoining rules, the four pairs, the positive and negative sums
- W3C: CSS 2.1, section 17.4 Tables in the visual formatting model — margins on the table wrapper box
- W3C: CSS1, section 4.1.1 Vertical formatting — collapsing to the maximum since 1996
- W3C: CSS Box Model Module Level 3 — Recommendation, 11 April 2024; points to CSS 2.1 for collapsing
- W3C: CSS Display Module Level 3 — Candidate Recommendation Draft, 5 June 2026: flow-root, block formatting context
- W3C: CSS Flexible Box Layout Module Level 1 — flex item and flex container margins do not collapse
- W3C: CSS Grid Layout Module Level 2 — grid item and grid container margins do not collapse
- W3C: CSS Containment Module Level 2 — layout containment establishes an independent formatting context
- W3C: CSS Box Model Module Level 4, margin-trim — Working Draft, 4 August 2024
- W3C: CSS Writing Modes Level 4 — collapsing in the block direction
- WHATWG HTML Standard: Rendering — default body, heading and paragraph margins
- MDN Web Docs: Mastering margin collapsing — three basic cases, zero margins, negative margins, flex and grid
- MDN Web Docs: margin — which elements take margins
- web.dev Learn CSS: Spacing — margin collapse and why it exists
- Josh W. Comeau: The Rules of Margin Collapse — December 7, 2020, updated September 13, 2024
- MDN browser-compat-data — browser versions, release 8.1.10, read November 23, 2026
- Can I use: display: flow-root — 97.33% of users, data updated November 22, 2026, read November 23, 2026
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.