More CSS answers

by Danny
Published 2 min read

Vertical margins between blocks merge into one instead of adding up. Learn the three cases where margins collapse and how flex, grid, gap or flow-root stop it.

The short answer

#

In normal block layout, touching vertical margins merge into a single margin equal to the larger of the two. Two paragraphs with margin: 24px 0 end up 24px apart, not 48px. To stop it, lay the elements out with flexbox or grid and use gap, or give the parent display: flow-root.


_10
.stack {
_10
display: flex;
_10
flex-direction: column;
_10
gap: 1.5rem; /* spacing that never collapses */
_10
}

Why it happens

#

Margin collapsing is a deliberate part of CSS block layout. It was designed for documents: a heading with a bottom margin followed by a paragraph with a top margin should not get a double gap. It only affects vertical (block direction) margins, and only between block-level boxes in normal flow.

There are three cases.

Adjacent siblings

The bottom margin of one block and the top margin of the next merge. The gap is the larger margin, not the sum.


_10
h2 { margin-bottom: 2rem; }
_10
p { margin-top: 1rem; }
_10
/* gap between them: 2rem */

Parent and first or last child

If nothing separates a parent's top edge from its first child's top margin, the child's margin passes through the parent. This is the confusing one: you give a child margin-top, and the whole parent moves down instead, often taking its background with it.


_10
.card { background: lightgrey; }
_10
.card h2 { margin-top: 2rem; }
_10
/* the gap appears above .card, outside its background */

The same applies to the last child's bottom margin.

Empty blocks

An element with no height, padding, border or content has its own top and bottom margins collapse together, and then with its neighbours.

With negative margins, the result is the largest positive margin plus the most negative one. Two negative margins collapse to the more negative value.

How to fix it

#

Use flex or grid with gap

Margins never collapse between flex items or grid items. This is the most predictable option for spacing a group of elements, and gap puts the spacing on the container so you do not need margins on the children at all.


_10
.stack {
_10
display: grid;
_10
gap: 1rem;
_10
}

Create a new block formatting context on the parent

For the parent and child case, display: flow-root makes the parent contain its children's margins. It has no other side effects, unlike the older trick of overflow: hidden.


_10
.card {
_10
display: flow-root;
_10
background: lightgrey;
_10
}

Put something between the margins

Any padding or border on the parent separates it from the child's margin. Even padding-top: 1px stops the collapse, though it is clearer to use real padding for spacing inside a box.


_10
.card {
_10
padding-block: 1.5rem;
_10
}
_10
_10
.card > :first-child {
_10
margin-top: 0;
_10
}

Use margins in one direction only

A common convention is to only set margin-bottom (or margin-block-end) on content elements. Siblings then never have two margins touching, so there is nothing to collapse. The "owl" selector * + * is another way to apply space only between siblings.


_10
.prose > * + * {
_10
margin-block-start: 1em;
_10
}

Common pitfalls

#
  • Expecting horizontal margins to collapse. They never do; only block-direction margins collapse.
  • Fixing a parent and child collapse with overflow: hidden. It works, but it also clips shadows, focus outlines and dropdowns. Prefer display: flow-root.
  • Assuming padding on the child helps. It must be on the parent, between the parent's edge and the child's margin.
  • Forgetting that floats, absolutely positioned elements and inline-block elements do not collapse margins, so layouts change when you switch these on or off.
  • Mixing margin-based spacing with gap. Both apply, so the spacing doubles.
#

Was this resource helpful?