The queue at Grandma Marni’s stall: Mrs. Yanti’s and Dodi’s margins are chalked on the ground exactly where the browser put them. Do two margins that meet add…
Why it matters
"Why is my gap only 24 when I set 24 + 16?" and "why is there a gap above my header?" are two faces of margin collapse. Knowing the rule = spacing you can predict, with no hours lost debugging.
The key idea
Touching vertical margins merge, they do not add up
Common mistakes
A child’s top margin "leaks" outside the parent A parent with no padding, border or BFC: its first child’s margin-top merges outward. Put display: flow-root on the parent (cleanest), or padding-top: 1px / overflow: hidden.
Spacing flex items with margins comes out different from normal blocks In flex and grid margins do not merge, so 24 + 16 really is 40. Use gap on the parent: one number, no surprises.
Thinking left/right margins merge too Only vertical margins (in a horizontal writing mode) merge. Left/right always add up in full.
Try it in this playground
Raise B’s margin-top past 24 The gap follows B’s margin, not 24 + B. The largest wins.
Make A’s margin-top leak, then stop the leak Type 30px in A’s margin-top, then write display: flow-root or padding-top: 1px on .parent. The room moves inside the lane.
Write display: flex; flex-direction: column on .parent Flex items do not merge: the gap becomes the full sum.
Give B a negative margin-top A negative margin is subtracted from the largest positive one, not maxed.