Full answer
Margin collapsing is a rule of block layout: when the vertical margins of two block boxes touch, they merge into one margin instead of adding. It confuses beginners because it only happens in the vertical direction, and only in some situations.
It happens when:
- Adjacent siblings: the bottom margin of one block meets the top margin of the next.
- Parent and first or last child, when nothing separates them (no border, padding, inline content or clear).
- An empty block whose own top and bottom margins touch.
The merged size follows simple rules. Two positive margins give the larger one. Two negative margins give the one with the larger magnitude (the most negative). One positive and one negative are added together.
It does not happen between flex or grid items, for floated or absolutely positioned boxes, for inline-block boxes, or for the root element. A new block formatting context on the parent stops parent-child collapse.
In normal flow, the gap between the first two boxes is 30px (the larger of 30 and 20), not 50px. In the second pair, 30 and -10 combine to 20px. In the flex column, margins do not collapse, so the gap is 50px.
If you want predictable spacing, many teams avoid collapse on purpose. A common approach is to space siblings with one-direction margins only (top, or bottom, not both), or with gap in a flex or grid container.
<style>
body { margin: 0; }
.b { height: 20px; background: #9cf; }
.m1 { margin-bottom: 30px; }
.m2 { margin-top: 20px; }
.neg { margin-top: -10px; }
.flex { display: flex; flex-direction: column; }
.flex .m1 { margin-bottom: 30px; }
.flex .m2 { margin-top: 20px; }
</style>
<div id="blockwrap">
<div class="b m1" id="a1"></div>
<div class="b m2" id="a2"></div>
</div>
<div id="negwrap">
<div class="b m1" id="n1"></div>
<div class="b neg" id="n2"></div>
</div>
<div class="flex" id="flexwrap">
<div class="b m1" id="f1"></div>
<div class="b m2" id="f2"></div>
</div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Only vertical margins collapse; horizontal margins add.
- Cases: adjacent siblings, parent and child, and empty blocks.
- Positive pairs give the larger; negative pairs the most negative; mixed signs are added.
- Flex and grid items, floats and absolute boxes do not collapse.
- Padding, border or a new BFC stops parent and child collapse.
Common mistakes
- adding margin-top to the first child inside a parent and seeing the whole parent move down instead. The child margin collapsed through the parent.
- believing horizontal margins collapse too. Left and right margins always add up.
Follow-ups
- how can you stop a child margin escaping its parent?
- Why does a child's margin-top move the whole parent?
- Does gap in flex or grid collapse with margins?
Tips
- Choose one direction for vertical spacing in components, for example margin-bottom only. Then collapse rarely matters.
- Give the 30 and 20 example and say the gap is 30. Numbers make it stick.
- Mention that using margin in one direction avoids most surprises.