Faragiri

Learn The box model CSS

Margin collapsing measured twice, the three things that stop it, and why a layout built on gap never meets any of it.

4Sections
3Min read
4Depth

Recommended Before This

0 / 1
  • CSS

Two stacked blocks, the first with margin-bottom: 30px and the
second with margin-top: 20px. Measured in a browser on 2026-09-02,
the actual gap between them is 30px, not 50. Vertical margins
between adjacent blocks collapse to the larger of the two, and the smaller one
does nothing at all.

Then the version that costs an afternoon. A parent with no padding and no
border, containing one child with margin-top: 40px: the parent's
height measured 10px, the height of the child alone, and the child's top is 0px
below the parent's top. The margin did not push the child down inside the
parent. It escaped, and moved the parent instead.

Why it escapes, and the three things that stop it

A margin collapses through an element when there is nothing between it and
the edge. Anything at all in the way ends it, and the usual three are
padding-top: 1px, border-top, or making the parent a
new formatting context with display: flow-root. Setting
overflow: hidden also works and is the version most codebases have,
inherited from an era before flow-root existed and with a side effect nobody
wanted.

Two blocks, four states

Where the second margin went

Nothing in the stylesheet changes between these frames. The layout engine resolves the two declared margins into one before anything is painted.

  1. 01Both margins declared30px below the first block, 20px above the second
  2. 02The larger one winsThey collapse to a single 30px, and the 20 does nothing
  3. 03A child's margin escapesWith no padding or border above it, the parent measured 10px tall
  4. 04One pixel of padding ends itOr a border, or flow-root. The margin is now inside the parent

The strip is vertical margins only. Horizontal margins never collapse, and neither do any margins inside a flex or grid container, which is why gap has none of these frames.

Interactive Practice Lab

Edit and run this snippet directly in your browser (zero setup)

CSS
.demo {  display: flex;  flex-wrap: wrap;  justify-content: space-between;  gap: 12px;}.box {  flex: 1 1 80px;  text-align: center;  background: #0ea5e9;}

Live Big-O Complexity & Binary Search Race Lab

Compare complexity curves at scale and watch Linear vs Binary Search race live.

Which is why the modern answer is usually to stop using margins for this at
all. In a flex or grid container, margins between children do not collapse and
gap sets the spacing directly, with no interaction between
siblings. A layout built on gap has none of the behaviour on this
page.

The other measurement that surprises people

Padding and border are outside the declared width by default, so
width: 300px with 20px of padding and a 2px border occupies 344px.
That default is why * { box-sizing: border-box } is the first line
of nearly every stylesheet written since about 2012: it makes
width mean the total, so the same box is 300px and adding padding
does not change the layout around it.

Default content-box math

How a 300px box grows to 344px without border-box

By default, padding and border sit outside declared width, so 20px padding and 2px borders on both sides add 44px to the rendered width.

Horizontal box size in px

  1. 300pxDeclared width
  2. 20pxLeft padding
  3. 20pxRight padding
  4. 2pxLeft border
  5. 2pxRight border
  6. 344pxTotal content-box width

Adding box-sizing: border-box keeps the total at 300px so adding 1px of padding to stop margin collapse no longer widens the element.

The two facts interact in one place worth knowing. With
border-box, adding padding-top to fix a collapsing
margin no longer widens or heightens the element, so the fix is genuinely free.
That is the practical reason to have both, and it is why they are usually found
in the same three lines at the top of a stylesheet.

What to run in 2026

The browser's element inspector is the tool, free in every browser, and its
box diagram is the thing to look at rather than the stylesheet. It shows the
computed margin, and a collapsed margin appears there at its declared value
while the layout ignores it, which is the exact discrepancy that makes this
confusing. It replaced adding a background colour to see where a box actually
is.

2026 platform tools

Inspecting collapsed boxes and containing margins cleanly

Browser devtools show the difference between declared and rendered margins, while display: flow-root replaces overflow: hidden for containment.

  1. 01Computed margin vs rendered gap
  2. 02display: flow-root;
  3. 03overflow: hidden
  1. 01Element inspector box diagramDisplays declared margin numbers even when vertical collapse makes the layout ignore the smaller one
  2. 02Formatting context without clipping (2026)Creates a block formatting context across all browsers to trap child margins with no side effects
  3. 03Legacy containment workaroundContained margins before flow-root existed, at the cost of clipping any content that overflowed

Checking the box diagram replaces temporary background colours; flow-root replaces overflow: hidden.

display: flow-root is the second, free with the platform and
supported everywhere as of 2026. It creates a block formatting context and does
nothing else, which is precisely what overflow: hidden was being
used for. It replaced that hack, and the difference is that flow-root does not
also clip anything that overflows.

Where this stalls people

The stall is that the numbers in the stylesheet and the numbers on the screen
disagree, with no error and nothing in the file to blame. Somebody sets a margin
to 20px, sees 30, sets it to 10, still sees 30, and concludes that CSS is
arbitrary. It is not: the sibling's 30 is winning, and the file being edited is
the wrong one.

Three boxes to check first

Read both neighbours and the parent before editing a margin

Changing 20px to 10 still renders 30 when another box owns the winning margin or lets it escape.

  • Larger vertical margin wins
  • Unblocked child margin escapes parent
  • Check computed sizes in order
  • Element being edited
  • Adjacent sibling box
  • Parent container
  • Element being editedSetting 20px or 10 has no effect if its margin is smaller than its neighbour's
  • Adjacent sibling boxOften holds the 30 margin that wins the vertical collapse
  • Parent containerMay have no padding, border, or flow-root, letting a child margin push the parent instead

Inspecting computed values across both neighbours and the parent in order finds the real source before any stylesheet rule is rewritten.

The habit that ends it: when a gap is not the number you set, read the
computed value of both neighbours and the parent, in that order, before
changing anything. Two of the three will explain it. What lays these boxes out
once they are correct is
laying out along one axis and
laying out in two dimensions, neither
of which has any of this behaviour, and it all sits inside
how a page is laid out and how it looks.

Alongside this

Above this