In CSS normal flow, one block element has margin-bottom: 30px and the block directly below it has margin-top: 20px. What vertical gap appears between them, and why?
answer
- larger value wins, never the sum
- block axis only, never left/right
- nothing between them means adjoining
- flex and grid items opt out
- overflow: hidden won't help siblings
basics
~10 sThe gap is 30px. Adjoining vertical margins of block boxes in normal flow collapse into one margin equal to the larger value instead of adding up, so the 20px margin contributes nothing extra.
solid answer
~40 sThe gap is 30px, not 50px. In normal flow the bottom margin of one block box and the top margin of the next are adjoining — nothing sits between them — so they collapse into a single margin equal to the larger of the two. Collapsing happens only on the block axis; left and right margins never collapse, so horizontally adjacent boxes really do get the sum. The pair stops collapsing when the boxes are not plain in-flow block boxes: flex items and grid items never collapse their margins, and neither do floated or absolutely positioned boxes. A common wrong fix is `overflow: hidden` on a sibling — a new formatting context stops a box collapsing with its own children, not with its siblings.
code
html · 14 lines<style>
.stack > div { background: #cde; }
.a { margin-bottom: 30px; }
.b { margin-top: 20px; }
.flex { display: flex; flex-direction: column; }
</style>
<div class="stack">
<div class="a">gap below me is 30px, not 50px</div>
<div class="b">margins collapsed</div>
</div>
<div class="stack flex">
<div class="a">gap below me is the full 50px</div>
<div class="b">flex items never collapse</div>
</div>go deeper
Be ready to state plainly that adjoining vertical margins merge to the larger value, and to compute the gap for two given numbers without hesitating.
Explain what makes two margins adjoining — nothing between the border edges — and name the situations that opt out, such as flex and grid items or floated boxes.
Show that you know which fixes actually apply where: a new formatting context stops a box collapsing with its children, not with its siblings, so reach for the right tool.
Be able to argue for a spacing convention across a codebase — gap-based layout or single-direction margins — so that collapsing stops being something every contributor has to reason about.
## The rule in one line When two block-level margins meet in normal flow with nothing between them they do not stack — they merge into one margin whose size is the larger of the two. This is **margin collapsing**, and the adjoining-siblings case is the one everybody meets first. ## Why the answer is 30px ```css .a { margin-bottom: 30px; } .b { margin-top: 20px; } ``` Given `<div class="a">` immediately followed by `<div class="b">`, the bottom margin of `.a` and the top margin of `.b` are *adjoining*: between the bottom border edge of `.a` and the top border edge of `.b` there is no border, no padding, no line box of text and no clearance. Adjoining margins collapse, and the used value is the larger one — 30px. The smaller margin is not "lost" in any mysterious way; both boxes simply share one 30px separation. A useful mental check: change the 20px to 5px and the gap stays 30px; change the 30px to 10px and the gap becomes 20px. The gap tracks the maximum, which is exactly what "collapse to the larger" means. ## Only the block axis Collapsing applies to margins along the **block axis** — vertical in the default horizontal writing mode. Inline-axis margins (left and right in that mode) never collapse: two boxes sitting side by side with `margin-right: 30px` and `margin-left: 20px` are separated by the full 50px. That asymmetry is the single most-tested fact here, because candidates who learned the rule as "margins collapse" over-apply it. ## What does not prevent it Padding or a border on the elements *themselves* does not stop sibling collapsing. `.a`'s `padding-bottom` sits inside `.a`, before its margin; the two margins still touch each other. What separates adjoining margins is something *between* them — an intervening element that has a border, padding, or content, or clearance introduced by `clear`. Likewise, giving `.a` `overflow: hidden` changes nothing about the sibling gap. Establishing a new block formatting context stops a box from collapsing with its **in-flow children**; it says nothing about its siblings. ## What does prevent it - **Flex and grid items.** Margins of flex items and grid items never collapse — with each other or with the container. Wrapping the two divs in a `display: flex; flex-direction: column` parent yields the full 50px. - **Floats.** A floated box's margins do not collapse with anything. - **Absolutely positioned boxes.** Same: `position: absolute` or `fixed` takes the box out of flow and out of collapsing. - **Inline-level boxes.** An `inline-block` box's vertical margins do not collapse with adjoining block margins. - **The root element.** The margins of the root element's box never collapse. ## Why the rule exists It makes default typographic flow look right. The user-agent stylesheet gives paragraphs a top and a bottom margin of about `1em`; without collapsing, consecutive paragraphs would be separated by two of them while the first paragraph in a section got only one. Collapsing normalises that to a single rhythm. Modern layouts increasingly sidestep the whole question by using flex or grid with `gap`, or by declaring spacing in one direction only, but collapsing is still the behaviour of plain block flow and it is still what the browser does to your markup by default. ## Corner cases worth knowing A margin of `0` still participates — it just always loses to a positive one. More than two margins can collapse at once when boxes nest. And negative values do not follow "the largest wins": the result is the largest positive plus the most negative, so `30px` meeting `-10px` yields `20px`.
- Why does wrapping the same two divs in a flex container change the gap to 50px?Because collapsing is a rule of block flow. Flex items are laid out by the flex algorithm, and the specification states that the margins of flex items do not collapse — with each other or with the container. Both margins are therefore used in full, giving 30px plus 20px. Grid items behave the same way.
- Do the left and right margins of two side-by-side boxes collapse the same way?No. Collapsing is confined to the block axis, which is vertical in a normal horizontal writing mode. Inline-axis margins always add, so `margin-right: 30px` next to `margin-left: 20px` gives a 50px separation. This asymmetry is deliberate and is a favourite trap in interviews.
- If the first box has margin-bottom: 30px and the second has no top margin at all, what is the gap?30px. A missing or zero top margin still participates in the collapse; it is simply the smaller of the two adjoining values, so the used margin is the 30px one. Nothing special happens for zero — it only ever loses to a positive margin.
Two people each reserving personal space in a queue: they do not add their bubbles together, they just stand at whichever distance the more demanding of the two wants.
saying these in an interview costs you the question
- Says the two margins add up to 50px
- Thinks left and right margins collapse too
- Claims overflow: hidden on a sibling prevents the collapse
- Believes padding on the elements themselves stops sibling collapsing
- Calls collapsing a browser bug rather than specified behaviour