日本語

How CSS Margin Collapsing Happens and How to Prevent It

​​CSS​CSS Grid,Flexbox,Playwright

When stacked elements have margins that don't add together and only the larger value remains, or when a margin-top on a heading inside a card has no effect and leaves a gap above the card, the cause is CSS margin collapsing. It's expected behavior under the spec. It only affects the top and bottom margins of block-level elements. Adding display: flow-root to the parent, or making the parent a flex or grid container, stops collapsing between parent and child.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened test pages with Playwright, read element positions with getBoundingClientRect(), and measured the gaps between elements in px. I did not test Firefox or Safari. I only covered horizontal writing mode and did not cover behavior under writing-mode.

The Three Situations Where Collapsing Happens

MDN's page on margin collapsing says collapsing happens in these three situations:

  • The margin-bottom of an upper sibling and the margin-top of a lower sibling that are adjacent
  • The margin-top of a parent and the margin-top of its first child, or the margin-bottom of a parent and the margin-bottom of its last child (when there is no padding, border, or content between them)
  • The margin-top and margin-bottom of an element that has no content and no height

In each case, the collapsed margins become one. For positive values, only the larger one remains. The sections below show what I measured in the test environment.

Adjacent Elements Keep Only the Larger Margin

I stacked two 40px-tall divs vertically, gave the upper one a margin-bottom and the lower one a margin-top, and measured the gap between them.

Upper margin-bottom Lower margin-top Gap
24px 16px 24px
16px 16px 16px
24px -10px 14px
-10px -20px -20px (20px overlap)

With 24px and 16px, the gap was 24px, not 40px. When a negative margin is involved, the result is the largest positive value plus the most negative value. With 24px and -10px, the gap was 14px. When both are negative, the more negative value (-20px) is used, and the two elements overlapped by 20px. This matches the calculation described on the MDN page.

A Child's margin-top Escapes the Parent

The hardest case to notice is collapsing between a parent and its child. I put a single child with margin-top: 24px inside a parent with a background color and measured. The 24px gap appeared above the parent, between the previous element and the parent, not inside it. The top edge of the parent and the top edge of the child were at the same position.

Two screens stacked for comparison. The top one, labeled "collapsing applies (nothing set on the card)", shows a white gap between a gray header and a light-blue card, circled with a pink dotted line and labeled "24px escaped the card." The heading sits flush against the top edge of the card. The bottom one, labeled "display: flow-root on the card", shows the card starting directly below the header, with a pink dotted line over the heading area labeled "24px is inside the card." (The labels in the image are in Japanese.)
The heading’s margin-top: 24px either escapes the card, or stays inside it with display: flow-root

The image above shows a heading inside a card (light blue) with margin-top: 24px. With nothing set, the heading's margin moved outside the card, leaving a white gap between the header and the card. The heading text sat flush against the card's top edge. When I added display: flow-root to the card, the 24px stayed inside it and the heading had a light-blue space above it.

When the parent also had a margin-top, the parent's and child's margins collapsed, and the larger one went outside the parent. With a parent margin of 10px and child margin of 24px, the gap above the parent was 24px. With a parent margin of 40px, it was 40px. In both cases the child stayed flush with the parent's top edge.

The last child's margin-bottom behaves the same way. In a parent with nothing set, the child's 24px bottom margin appeared below the parent, between the parent and the next element.

Stopping Parent-Child Collapsing

Collapsing stops when something separates the parent from the child at its top or bottom, or when the parent creates a new block formatting context (BFC). I set margin-top: 24px on the first child and changed the parent's style, and measured the results below.

Parent style Gap above parent Parent top to child top
None 24px 0px
padding-top: 1px 0px 25px
border-top: 1px solid 0px 25px
display: flow-root 0px 24px
overflow: hidden 0px 24px
overflow: clip 24px 0px
display: flex; flex-direction: column 0px 24px
display: grid 0px 24px

Adding padding-top or border-top stops collapsing, but the child moves down by that amount (1px in this case). If you want to stop collapsing without adding space or shifting the child, display: flow-root is the right choice. MDN's page on block formatting contexts says that a new BFC prevents margin collapsing, and that display: flow-root creates a new BFC without other side effects.

overflow: hidden also stopped collapsing. However, overflow controls how overflowing content is handled, and the same MDN page warns about side effects such as clipping shadows that extend past the element. overflow: clip does not create a BFC, so collapsing did not stop.

When I made the parent a flex or grid container, the child's margin stayed inside the parent. The CSS Flexbox spec also says that a flex container's margins do not collapse with the margins of its contents.

Collapsing also did not happen when there was text or other inline content before the child inside the parent. This matches what the MDN page says: collapsing does not occur when inline content (such as text) separates the margins.

On the bottom side, height can also stop it (min-height depends on its value)

When I tested the same thing with the last child's margin-bottom: 24px, collapsing stopped with padding-bottom, with display: flow-root, and also when I set height: 80px on the parent. min-height gave different results depending on its value. With min-height: 100px, which is larger than the content height (40px for the child), collapsing stopped. With min-height: 20px, which is smaller, the 24px stayed outside the parent. The MDN page says that a parent with min-height doesn't collapse, but in the test environment collapsing did not stop when the value was smaller than the content.

An Empty Element Collapses Its Top and Bottom Margins Into One

I put a div with no content and no height, with margin: 30px 0, between two 40px-tall elements, one with margin-bottom: 16px and one with margin-top: 16px.

html
<div class="a" style="height: 40px; margin-bottom: 16px"></div>
<div style="margin: 30px 0"></div>
<div class="b" style="height: 40px; margin-top: 16px"></div>

The gap between the upper and lower elements was 30px. The empty element's top and bottom margins, along with the margins of the elements on either side, all collapsed together, and only the largest value, 30px, remained. When I gave the empty element padding-top: 1px, height: 1px, or min-height: 1px, the gap became 61px (30px + 1px + 30px).

Placing an empty div as a separator and expecting the margin alone to create the spacing you want won't work, because of this collapsing.

Layouts That Don't Collapse

In the layouts below, the 24px and 16px margins added together, and the gap was 40px.

  • Children inside a parent with display: flex; flex-direction: column
  • Children inside a parent with display: grid
  • Adjacent display: inline-block elements
  • Adjacent float elements (with clear: both on the lower element)

The CSS Flexbox spec (section on flex item margins) and the CSS Grid spec (section on grid item margins) both say that margins of adjacent items do not collapse.

Left and right margins don't collapse either. When I placed two inline-block elements with margin: 0 10px side by side, the gap between them was 20px. The MDN page also notes that collapsing only happens in the vertical direction.

Set Spacing with flex or grid gap

If you want to set spacing between elements without thinking about collapsing, make the parent a flex or grid container and specify the spacing with gap.

css
.stack {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

In a parent with gap: 24px where the children have their margins set to 0, the gap between the children was 24px. Because the parent is a flex container, the margins of the first and last children never escape the parent.

However, if a child keeps its margin, that margin is added on top of gap. In a parent with gap: 24px, when I gave the second child margin-top: 16px, the gap became 40px. If a heading or paragraph keeps its margin inside a gap container, the spacing ends up wider than the gap value. In a parent that uses gap, set the top and bottom margins of the children to 0 so the spacing is determined by the gap value alone.

​Posted by 86