Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why do adjacent vertical margins collapse instead of adding together?

Quick answer

In normal block flow, adjoining vertical margins can collapse into one margin instead of adding together. For two positive margins, the larger one sets the gap, not their sum. This commonly happens between siblings and in certain parent-child cases. Add a border or padding, establish a new block formatting context, or use flex/grid with gap when you want a clearer spacing rule. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I gave two sections margin-bottom: 24px and margin-top: 16px, but the visible gap is 24px, not 40px. Is the browser ignoring one of the margins?

Geez

It is probably collapsing the margins. When two positive vertical margins on adjacent block boxes in normal flow meet, they can combine into a single margin whose size is the larger value. So in your example the gap is 24px, rather than 24px plus 16px. [S1]

Here is a small reproduction. The sections are block elements in ordinary document flow:

<style>
  .first { margin-bottom: 24px; }
  .second { margin-top: 16px; }
</style>

<section class="first">First section</section>
<section class="second">Second section</section>

That behavior applies to vertical margins, not horizontal ones. It is a CSS layout rule, not a sign that the browser failed to apply your declaration. [S1]

Follow-up question

Does this only happen between siblings? I also see a gap above a heading inside a container, even though the container has no top margin.

Geez

It can happen in more than one arrangement. Adjacent siblings are a common case, but a parent’s margin can also collapse with the top margin of its first child, or with the bottom margin of its last child, under qualifying conditions. An empty block can also have its top and bottom margins collapse together. [S1]

That is why a child’s top margin may appear to create space outside its parent rather than inside it. A border or padding on the parent can separate the parent and child margins. Qualifying content or sizing can also prevent that particular parent-child collapse, as can establishing a new block formatting context on the parent. [S1]

For a quick check, inspect both the child and its parent in your browser’s developer tools. Look for adjoining vertical margins and check whether the parent has a border, padding, relevant content, or a formatting context that changes the behavior.

Follow-up question

What should I change if I want a visible 40px gap between those sections, without relying on the bigger margin winning?

Geez

Choose a spacing rule that directly represents the result you want. If the sections are siblings in a repeated vertical layout, put them in a flex or grid container and set gap: 40px. Margins do not collapse between flex or grid items, and gap specifies space between items rather than adding a margin to each one. [S1] [S2]

.sections {
  display: flex;
  flex-direction: column;
  gap: 40px;
}
<div class="sections">
  <section>First section</section>
  <section>Second section</section>
</div>

If you want to keep ordinary block flow, give one element a single spacing responsibility—for example, margin-bottom: 40px on each section except the last. Alternatively, if a boundary is what you need, add padding or a border where appropriate. Avoid putting large opposing margins on both neighbors and expecting them to sum.

Follow-up question

Could I add display: flow-root to the parent? And how do I decide which fix is best for the rest of this page?

Geez

A parent with display: flow-root establishes a new block formatting context, which prevents its margins from collapsing with its children. [S1] That can be useful when the specific problem is a parent-child collapse. It does not mean that every pair of sibling margins inside the parent stops collapsing, so it may not solve the spacing between your two sections.

For repeated spacing between items, use a flex or grid layout with gap. For a simple block-flow document, assign spacing consistently to one side of each element, or use a parent’s padding when you need space inside a boundary. gap is documented for multi-column, flex, and grid layouts—not ordinary block flow. [S2]

There is no single best fix for every layout. Pick the one that matches the structure and spacing relationship you intend, then verify the result in that context.

A practical checklist

  • Confirm the boxes are in normal block flow and the margins are vertical.

  • Check whether adjacent positive margins are collapsing; the larger value sets the gap.

  • Inspect parent-child boundaries for padding, borders, content, sizing, or a new block formatting context.

  • Use flex or grid with gap for consistent spacing between items in those layouts.

  • If keeping block flow, assign spacing consistently and check the surrounding markup.

A few more useful answers

Do horizontal margins collapse too?

No. The margin-collapsing behavior described here concerns vertical margins on block boxes in normal flow. [S1]

Will `gap` space ordinary block-flow elements?

The cited guidance documents gap for multi-column, flex, and grid layouts, not ordinary block flow. Use it when the items are in one of those layout modes. [S2]

Does `display: flow-root` stop every margin collapse inside the parent?

It prevents the parent’s margins from collapsing with its children. It does not, by itself, establish that sibling margins inside the parent cannot collapse. [S1]

Sources

References used for this guide.

  1. [S1] Mastering margin collapsing — CSS | MDN
    developer.mozilla.org · Checked
  2. [S2] gap CSS property — CSS | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work