Sub topic 6
When vertical margins collapse
This chapter assumes you know the CSS box model, can read rem values, and have met margin and padding in the previous chapters. It covers one behavior of block layout: under certain conditions, two vertical margins merge into one. Knowing the conditions lets you predict the space between groups instead of fixing it by trial and error.
What collapsing is
Margin collapsing is a rule of normal block layout. When the vertical margins of two elements touch, the browser does not add them. It combines them into a single margin.
.box-a { margin-bottom: 2rem; }
.box-b { margin-top: 1rem; }If .box-a and .box-b are adjacent block siblings in normal flow, the space between them is 2rem, not 3rem. The larger margin wins, and the smaller one is absorbed.
This matters for grouping. Your spacing scale only works if you can predict the distance between two elements. If margins sometimes add and sometimes collapse, the same pair of classes can produce different gaps in different places, and your groups become uneven.
Three facts frame everything else:
- Only vertical margins (in a horizontal writing mode, the block-axis margins) collapse. Left and right margins never do.
- Only margins that touch collapse. Anything between them, such as padding, a border, or content, stops the merge.
- Padding never collapses. This is one reason padding is more predictable than margin.
The three situations where margins collapse
The CSS specification describes three cases. You will meet all of them in real pages.
1. Adjacent siblings
Two block siblings in normal flow, where the bottom margin of the first touches the top margin of the second. This is the .box-a and .box-b case above.
2. A parent and its first or last child
If a parent has no padding, border, or content separating it from its first child, the child's top margin merges with the parent's top margin. The same applies to the last child's bottom margin and the parent's bottom margin.
<section class="hero">
<h1>Welcome</h1>
</section>
<style>
.hero { background: #eef; }
.hero h1 { margin-top: 2rem; }
</style>You would expect the 2rem to push the heading down inside the blue box. Instead, the heading's margin merges with the section's top margin. The whole section moves down by 2rem, and the heading sits flush against the top of the blue background. The margin ends up outside the box you coloured.
3. Empty blocks
If a block has no height, padding, border, or content, its own top and bottom margins touch each other and collapse into one. That single margin can then collapse with its neighbors too. An empty <div> with margin: 2rem 0 between two paragraphs adds no more space than the larger of the surrounding margins would.
How the combined margin is calculated
The rule depends on the signs of the margins involved.
| Margins involved | Resulting space |
|---|---|
| Both positive | The larger one |
| One positive, one negative | The positive value plus the negative value |
| Both negative | The one with the larger absolute value (the most negative) |
For example, 2rem against -0.5rem gives 1.5rem. A -1rem against a -3rem gives -3rem. Negative margins are rare in a spacing system, and mixing them with collapsing makes results hard to read. Avoid them for grouping.
What stops collapsing
You can stop a collapse deliberately. These are the common tools.
- Padding or a border on the parent. Even
padding-top: 1pxor a transparent border separates the parent's margin from the child's. This works but is a hack, because the extra pixel stays in the layout. display: flow-rooton the parent. This creates a new block formatting context, which contains its children's margins. It has no visual side effects, so it is the cleanest fix for the parent and child case.display: flexordisplay: gridon the parent. Margins of flex items and grid items do not collapse with each other or with the container.overflowother thanvisible.overflow: hiddenorautoalso contains margins, but it can clip shadows and focus outlines, so preferflow-root.- Floats and absolute positioning. Floated and absolutely positioned elements do not collapse margins. You rarely choose these just for this purpose.
display: inline-block. Inline-block boxes do not collapse with their neighbors.
The fix for the hero example is one line:
.hero {
background: #eef;
display: flow-root;
}
.hero h1 {
margin-top: 2rem;
}Now the 2rem stays inside the section, and the heading sits 2rem below the blue box's top edge.
Collapsing and your spacing system
Collapsing shows up differently depending on which spacing tool you use. This ties back to the earlier chapters on padding, margin, and gap.
Gap never collapses
The gap property on flex and grid containers sets a fixed space between children. Because flex and grid items do not collapse margins, the gap is always exactly what you wrote. This is the main reason gap is the most predictable way to separate cards or form fields.
.card-list {
display: grid;
gap: var(--space-4);
}Every card is exactly var(--space-4) from its neighbors. If you also put margins on the cards, those margins add to the gap, because grid items do not collapse them. Pick one mechanism per container.
Flow rules rely on collapsing being predictable
The flow rule from the spacing scale uses only one margin per element:
.flow > * + * {
margin-block-start: var(--flow-space, var(--space-3));
}
.flow.tight {
--flow-space: var(--space-2);
}Each child, except the first, gets a top margin. No child sets a bottom margin, so two margins never meet. The result is the same whether collapsing happens or not. This pattern avoids most collapse surprises.
The pattern can still go wrong when children set their own margins. If a <p> inside .flow has margin-bottom: 2rem from a global stylesheet, and the next element gets --flow-space: 1rem as its top margin, those two margins collapse to 2rem. Your .tight variant would then not look tight. Reset default margins on the elements you place inside a flow container, or use a reset such as * { margin: 0; } before your flow rules.
Mixing groups with different spacing
Collapsing can help. Suppose a heading has margin-top: var(--space-5) and the paragraph above it ends with margin-bottom: var(--space-3). The space above the heading is var(--space-5), not the sum. The larger margin dominates, which usually matches your intent: the heading starts a new group, so it needs the larger gap. Collapsing means you only have to ask "what is the biggest separation this element needs?"
Debugging collapsed margins
When a gap is not what you expected, work through these steps.
- Open the browser's developer tools and select the element. The box model view shows the margin area in orange.
- Check whether the parent's margin is larger than its own CSS says. If so, a child's margin has merged into it.
- Look at the parent for padding, border,
display, andoverflow. If none of them separate it from the child, adddisplay: flow-root. - Look for empty elements, such as an empty
<p>or a<div>used as a spacer. Remove them and set the margin on a real element. - Check that both elements are really in normal block flow. Flex and grid items never collapse.
A worked example
Here is a small page where collapsing causes two problems at once.
<main>
<section class="intro">
<h2>About us</h2>
<p>We build small tools.</p>
</section>
<section class="team">
<h2>Team</h2>
<p>Five people.</p>
</section>
</main>
<style>
:root {
--space-3: 1rem;
--space-5: 2.5rem;
}
* { margin: 0; }
section { margin-block: var(--space-5); }
h2 { margin-block-start: var(--space-3); }
</style>Two things happen here:
- Between the sections, the bottom margin of
.intro(2.5rem) and the top margin of.team(2.5rem) collapse into2.5rem, not5rem. That is probably what you want. - Each
<h2>hasmargin-block-start: 1rem, but it is the first child of its section, and the section has no padding or border. The heading's1remmerges with the section's2.5rem, so the heading sits flush at the top of its section and the1remhas no visible effect.
If you want the heading to have its own space inside the section, add display: flow-root to section. The 1rem then appears inside the section, and the distance from the previous section's content to the heading becomes 2.5rem + 1rem. Decide which result you want before you pick a fix, because both are valid layouts that carry different grouping meanings.
Exercise
A .card has no padding or border and contains an <h3> with margin-top: 1.5rem. The card sits inside a grid container with gap: 1rem, and you gave the card a light grey background. The heading looks flush against the top of the grey area, and the card has an extra 1.5rem of space above it inside the grid cell. Explain why, and give two different ways to move the 1.5rem inside the grey area.
Show answer
The heading is the first child of the card, and nothing separates its top margin from the card's top edge. The two margins collapse, and the combined 1.5rem margin applies to the card's outer edge instead of the space inside it. Two fixes: add display: flow-root to .card, which contains the child's margin without side effects, or replace the heading's margin-top with padding: 1.5rem on .card (padding never collapses). Using padding on the card is the better choice if you want the space inside every card to be consistent.