Margin Collapsing and Formatting Contexts
Why vertical margins sometimes merge into one, what a block formatting context is, and the small set of properties that stop the collapsing or contain floats.
Margin Collapsing and Formatting Contexts
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- In normal block flow, adjoining vertical margins merge into a single margin: the larger of the two (or the sum of a positive and a negative). Two siblings with `margin-bottom: 20px` and `margin-top: 30px` end up 30px apart, not 50px.
- A parent's top margin also merges with its first child's top margin when nothing (padding, border, a new formatting context) separates them. The child's margin then appears to 'leak' out of the parent.
- Margins do not collapse inside flex or grid containers, between floats, or on absolutely positioned or `inline-block` boxes. Only block-axis margins collapse.
- `display: flow-root` creates a new block formatting context without side effects. It stops margin leakage from the inside and makes a container contain its floated children.
- The long-term fix is to avoid relying on margin between siblings: use `gap` in flex and grid, or margin in one direction only.
The bug that looks like a bug
You give a heading margin-top: 40px, and instead of space between the heading and its container’s edge, the whole container shifts down. You put margin-bottom: 20px on one paragraph and margin-top: 30px on the next and expect 50px of space; you see 30px. Nothing in the stylesheet is wrong. The browser is doing something deliberate called margin collapsing, and it will keep surprising you until you know the rule.
The reason it exists is typographic. Imagine a page of stacked paragraphs, each with 1em above and below. If margins added, the space between two paragraphs would be 2em and the space above the first would be 1em. Collapsing makes the space between paragraphs equal to the space above the first. It was designed for documents, and it only applies to block flow.
The three situations
Margins collapse when they are adjoining and vertical (in the block direction):
- Adjacent siblings. The bottom margin of one block and the top margin of the next. Result: the larger of the two. I measured a 20px and a 30px margin giving 30px between the boxes.
- Parent and first (or last) child. If there is no padding, border, or line of content between a parent’s top edge and its first child’s top margin, the two margins merge, and the combined margin sits outside the parent. This is the “leak”.
- Empty blocks. A box with no height, padding, border or content has its own top and bottom margin collapse through it.
When the values are mixed in sign, the rule is: take the biggest positive, add the most negative. A 30px bottom margin meeting a −10px top margin leaves 20px, which I confirmed.
What stops it
Collapsing needs the margins to touch. Anything that separates them stops it:
- Padding or a border on the parent. A single pixel of
padding-topwas enough in my test: the parent started at 0 and the child sat 41px down (40px margin plus 1px padding), so the margin stayed inside. - A new formatting context on the parent:
display: flow-root,overflowother thanvisible,display: flexorgridon the parent,position: absolute, and floats. - Different layout modes. Flex and grid items never collapse their margins with each other. Inline-block, floated and absolutely positioned boxes do not collapse either.
Block formatting contexts
A formatting context is a region where children are laid out by one set of rules. The default one is the block formatting context (BFC) of the page: blocks stacked vertically, inline content flowing in lines. Some boxes establish a new, independent BFC, and it has three properties that matter:
- Margins of the BFC’s children do not collapse with the BFC element’s own margins.
- Floats inside it are contained, so the box grows to enclose them.
- It will not overlap floats outside it.
That explains the classic float bug. A float is out of flow, so a parent with only floated children measures nothing:
<div class="parent"><div class="float"></div></div>
My test parent measured 2px, its border only, and measured 102px once it was display: flow-root. Float clearing hacks existed to work around this; flow-root is the clean answer.
Why flow-root, and not overflow: hidden
For years the way to create a BFC was overflow: hidden. It works, and also clips shadows and focus outlines, and turns the element into a scroll container (see overflow and scroll containers). display: flow-root does only the BFC job. Kevin Powell’s runner-up video is devoted to it. It has been supported since Chrome 58, Firefox 53 and Safari 13.
Working with it instead of against it
Two habits sidestep nearly all of this.
Use gap between siblings in flex and grid containers. The spacing belongs to the parent, it never collapses, and the first and last items do not get stray outer margin.
Pick one direction for margins, usually top (margin-block-start) or bottom, on a stack of blocks. Then there is nothing to collapse, because margins of a single direction are never adjoining. The > * + * pattern from the selectors lesson does exactly this.
What the video covers
Kevin Powell’s “Collapsing margins - what they are and how to deal with them” is about 14 minutes and is from April 2021. His chapter list covers what collapsing margins are, how flexbox and grid differ, the wrong solution he sees all the time, using flow-root, and a more controlled solution. The numeric tests above are my own in Chromium, since I could not read a transcript.
Where this fits
Margin collapsing is the layout side effect of the box model: margin is outside the box, and normal flow decides how neighbouring outside edges combine. Positioning and sticky takes elements out of the flow entirely. The frontend roadmap’s box model and cascade lesson mentions margin collapse briefly.
Check yourself
Two paragraphs: the first has `margin-bottom: 30px`, the second has `margin-top: -10px`. How far apart are they?
20px. When one margin is negative, the collapsed margin is the largest positive plus the most negative, so 30 + (−10) = 20. I measured 20px in Chromium.
A card has no padding or border, and its first child heading has `margin-top: 40px`. Where does the 40px of space end up?
The child's 40px margin merges with the card's own top margin, so the card itself is pushed down 40px and the heading sits flush against the card's top edge. The gap appears above the card, not inside it. Padding, a border, or `display: flow-root` on the card keeps the margin inside.
Why does a parent with only floated children have zero height, and how does `display: flow-root` fix it?
A float is taken out of normal flow, so the parent has no in-flow content to size itself around. A block formatting context must contain its floats, and `flow-root` creates one. In my test the parent measured 2px (just its border) without it and 102px with it.
Do vertical margins between two items in a flex column collapse?
No. Flex items are not laid out in block flow, so their margins never collapse. Two items with 20px and 30px margins sit 50px apart, which I measured. The same is true for grid items.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.