Margin Collapsing: When Vertical Margins Combine and How to Stop It
Why vertical margins combine instead of adding up: sibling, parent and child, and empty box collapsing, the math with negative margins, what prevents collapsing including flow-root, and spacing patterns like the owl selector, gap and margin-trim.
kvi5hnuCode Sprout team member9 min readOn this page
Put a paragraph with margin-bottom: 30px above one with margin-top: 20px, and the gap between them is 30px, not 50px. Vertical margins in normal flow collapse: adjoining margins combine into one. That's deliberate, and mostly helpful, but it also causes the most confusing spacing bugs in CSS, such as a heading's margin appearing outside its colored container.

Why margins collapse
Browsers give paragraphs, headings and lists top and bottom margins, so text has space around it wherever it's placed. If margins added up, two paragraphs would be twice as far apart as a paragraph and the edge of the page. Collapsing makes margins behave like minimum distances: each element asks for at least this much space around it, and the largest request wins. A heading with margin-bottom: 1.5em gets 1.5em of space after it, whether the next element has a 1em top margin or none.
What collapses
Only block-direction margins (top and bottom in horizontal text) of block boxes in normal flow collapse. Left and right margins never do. Padding and borders never do. There are three cases:
- Adjacent siblings: one box's bottom margin and the next box's top margin.
- Parent and first or last child: a parent's top margin and its first child's top margin, when nothing separates them: no padding, border or inline content. The same goes for the bottom margins of a parent and its last child, if the parent's height is
auto. - Empty boxes: an element with no content, padding, border or height has its own top and bottom margins collapse together, and with its neighbors'.
Collapsing chains through all of these: a heading's bottom margin, a <div>'s top margin and the top margin of the paragraph inside that <div> all collapse into a single margin.
The parent and child case
Case 2 is the surprising one. A child's top margin doesn't push the child down inside its parent; it escapes through the parent's top edge and becomes the parent's margin:
<header class="banner"> <!-- background: navy, no padding -->
<h1>Harbor Café</h1> <!-- margin-top: 0.67em from the browser -->
</header>
The <h1>'s margin collapses with the <header>'s, so the gap appears above the navy header, and the heading touches its top edge. The same happens to the last child's bottom margin, below the header.
The math
| Margins meeting | Result | Example |
|---|---|---|
| all positive | the largest | 30px and 20px give 30px |
| positive and negative | largest positive plus most negative | 30px and −10px give 20px |
| all negative | the most negative | −30px and −10px give −30px |
With several margins in one chain, the rule is the same: take the largest positive margin and add the most negative one. For example, a list item's margin-bottom: 24px, its list's margin-bottom: -10px and the next heading's margin-top: -16px give 24 − 16 = 8px.

The gaps are measured in the browser and match the table. The flex column is the control: the same margins simply add up there.
What stops collapsing
body { font: 0.9rem system-ui, sans-serif; margin: 0; padding: 0.5rem 1rem; display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: 1rem; align-items: start; }
.case { background: #fbe7a8; display: flow-root; } /* contains whatever escapes the parent */
.label { font-weight: 700; margin: 0 0 0.25rem; }
.parent { background: #2f7d4f; color: white; }
.parent h3 { margin: 24px 0; background: #1f5636; padding: 0.3rem 0.5rem; font-size: 1rem; }
.padded { padding-block: 1px; }
.flow-root { display: flow-root; }
The yellow area is outside the green parent. In the first case, the heading's 24px margins sit there, outside it.

The measurements show the difference: the 1px padding adds its own 2px to the height, while flow-root and flex contain the margins exactly.
Margins between a parent and its child stop collapsing when any of these are true:
- The parent has padding or a border on that side. Even
padding-top: 1pxis enough. - The parent establishes a new block formatting context (article 44). The cleanest way is
display: flow-root, which exists for exactly this purpose and has no other side effects.overflowwithhidden,autoorscrollalso works, but clips or scrolls content.overflow: clipdoesn't: it clips without creating a formatting context. - There's inline content, such as text, between the margins.
- For the bottom margins only: the parent has a set
height, or amin-heightthat makes it taller than its content.
And a box's margins never collapse at all if it is:
- a flex or grid item. Margins between flex or grid items always add up. A flex or grid container's own margins still collapse with its siblings and parent, but never with its children's.
- floated (article 54) or absolutely positioned (article 56).
inline-block, or any inline-level box.- the root element,
<html>. - a table cell or row, which has no margins at all.
The most direct fix is often to remove the child's margin and give the parent padding instead:
.banner { padding-block: 1rem; }
.banner h1 { margin: 0; }
Spacing patterns
Collapsing gets complicated when every element sets both top and bottom margins. Spacing is easier to reason about with a few conventions.
Margins in one direction
Set only bottom margins (margin-block-end) on text elements, or only top margins. Sibling margins then never meet, so nothing collapses between siblings, and each margin is easy to find.
The owl selector
The "lobotomized owl" selector, * + *, named for how it looks, selects every element that follows a sibling. Putting the margin on it adds space between elements, but never before the first one:
.flow > * + * {
margin-block-start: var(--flow-space, 1em);
}
- The
>limits it to direct children of a container, so nested components keep their own spacing. - The first child gets no top margin, so there's no double space at the top of a padded card, and nothing to escape through its top.
- A custom property lets each container or element choose the space:
.flow h2 { --flow-space: 2em; }gives more room before headings. - Classes for this pattern are often called
.flowor.stack. Reset the children's browser margins first (margin-block: 0), or the old bottom margins still collapse with the new top ones.
:where(.flow > * + *) keeps the selector's specificity at zero (article 15), so any other rule can override it.
gap
For component layouts, a flex or grid column with gap is the simplest stack of all:
.stack { display: flex; flex-direction: column; gap: 1rem; }
gap only goes between items, and margins don't collapse in flex and grid. The trade-off is that the container's children are no longer laid out in normal flow: they become flex items, which changes how floats, auto margins and some widths behave. For long-form text with mixed elements, the owl pattern fits better, and for components, gap does.
margin-trim
margin-trim: block (article 39) on a container removes its first child's top margin and its last child's bottom margin, solving the "double space at the top of a padded card" problem from the other side. It's in Safari and Chrome 155, not yet in Firefox.
Debugging spacing
When you see an unexpected gap above or below a box, or text pressed against the top of a colored container, suspect collapsing:
- In DevTools, hover the elements around the gap. The margin overlay shows which element's margin it is, often a child's.
- Look for a parent with a background but no padding or border.
- Fix it with padding on the parent,
display: flow-root, or by removing the child's margin.
What's current
- The collapsing rules have been stable since CSS 2.
display: flow-rootworks everywhere. margin-trimis in Safari and Chrome 155, not in Firefox.- Flex and grid layouts with
gapavoid collapsing entirely, which is one reason modern component CSS rarely runs into it.
Common questions
Why is there a gap above my header, outside its background?
The first child's top margin, often an <h1>'s, collapses through the header's top edge. Give the header padding or display: flow-root, or set the heading's top margin to 0.
Do horizontal margins collapse? No, never. Only block-direction margins collapse, which means left and right margins in vertical writing modes.
Why don't my margins collapse in a flex container? Margins of flex and grid items never collapse. That's often convenient, but it means two items with 1rem margins are 2rem apart.
Does overflow: hidden stop collapsing?
Yes, it creates a new block formatting context. But it also clips overflowing content, such as shadows and dropdowns. display: flow-root does the same without clipping.
How can a margin be negative after collapsing? When all the meeting margins are negative, the most negative wins, so the elements overlap by that amount.
Summary
- Adjoining block-direction margins of normal-flow blocks collapse into one: siblings, a parent with its first or last child, and empty boxes.
- Positive margins: the largest wins. With negatives, add the largest positive and the most negative.
- A child's margin escapes its parent unless padding, a border, inline content or a new formatting context separates them.
display: flow-rootis the clean fix. - Flex and grid items, floats, absolutely positioned boxes, inline-blocks and the root never collapse.
- Use one-direction margins, the
.flow > * + *owl pattern, orgapto keep spacing predictable.
Practice questions
An <h2> has margin-bottom: 1.5rem, and the following <p> has margin-top: 1rem. How far apart are they? What if the <p>'s margin is -0.5rem?
Answer
1.5rem, the larger margin. With −0.5rem, the gap is 1.5 − 0.5 = 1rem.
A .card with a background and no padding contains a <p> with margin: 1rem 0. The card's text touches its top edge, and there's extra space above the card. Give two fixes.
Answer
The paragraph's margins collapse through the card. Either give the card padding (padding: 1rem, and remove the paragraph's margins), or add display: flow-root to the card so the margins stay inside it.
Write a rule that puts 1.25em of space between all direct children of .prose, but none above the first.
Answer
.prose > * { margin-block: 0; }
.prose > * + * { margin-block-start: 1.25em; }
Two items in a column flexbox each have margin: 1rem 0. How much space is between them, and why?
Answer
2rem. Margins of flex items don't collapse, so both margins count.
Previous: Padding and Margins: Percentages, auto Centering and Negative Margins
Next: Borders: Styles, Widths, Colors, border-radius, border-image and corner-shape

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.