Sub topic 9
Margin collapsing
Margin collapsing is a rule in CSS where adjoining vertical margins combine into a single margin instead of adding together. This chapter assumes you know the margin property, the difference between block and inline display, and the box-sizing basics from earlier chapters.
What collapsing means
When the bottom margin of one block box touches the top margin of the next block box, the browser does not add them. It uses the larger of the two values.
<style>
h2 { margin-bottom: 24px; }
p { margin-top: 16px; }
</style>
<h2>Heading</h2>
<p>Paragraph</p>The gap between the heading and the paragraph is 24px, not 40px. The smaller margin is absorbed by the larger one. If both margins are equal, the result is that single value. The rule exists so that stacked paragraphs and headings keep a consistent rhythm without doubling their spacing.
When margins collapse
Collapsing happens only when all of these are true:
- The margins are vertical (top and bottom). Horizontal margins never collapse.
- The boxes are in normal block flow, in the same block formatting context.
- Nothing separates the margins, such as padding, a border, or content.
There are three common cases:
- Adjacent siblings. The bottom margin of one box and the top margin of the next sibling.
- Parent and first or last child. If a parent has no padding, border, or clearance between it and its first child, the child's top margin collapses with the parent's top margin. The child's margin appears to push the parent down. The same applies to the last child and the parent's bottom margin, provided the parent has no explicit height.
- Empty boxes. If a box has no content, padding, border, or height, its own top and bottom margins collapse together, and may collapse with neighbours too.
With negative margins the rule changes. If one margin is negative, the browser adds the largest positive margin and the most negative margin. For example, 30px and -10px give 20px.
What does not collapse
Margins do not collapse in these situations:
- Between flex items or grid items. Each flex or grid item establishes its own formatting context, so its margins stay separate.
- On floated elements and absolutely positioned elements.
- On
inline-blockboxes. - Between a parent and child when the parent has padding or a border on that side.
- When the parent creates a new block formatting context, for example with
overflowset to something other thanvisible, or withdisplay: flow-root.
Preventing parent-child collapse
A frequent surprise is a child heading whose top margin seems to leak out of its container. The container's background does not move down with the heading. Instead, the whole container shifts.
The cleanest fix is display: flow-root, which makes the container a new block formatting context without side effects.
.container {
display: flow-root;
}
.container > h1 {
margin-top: 2rem;
}Now the h1 margin stays inside .container. Other fixes also work, but each has a cost:
| Fix | Side effect |
|---|---|
| `display: flow-root` | None in modern browsers |
| `overflow: auto` or `hidden` | Can clip content or add scrollbars |
| `padding-top: 1px` or `border-top` | Adds visible size to the box |
| Flex or grid container | Changes how the children are laid out |
A practical habit
Many teams avoid the problem by spacing elements in one direction only, for example using margin-bottom on every element and never margin-top. Collapsing still occurs between siblings, but the results are easier to predict.
Common errors
- Expecting 40px when a 24px margin meets a 16px margin. The result is 24px.
- Adding
margin-topto a first child and wondering why the parent moves instead of the child. - Assuming horizontal margins collapse like vertical ones. They add together.
- Forgetting that flex and grid children do not collapse, so spacing changes when you switch
displayon a container.
Exercise
A .parent has no padding or border. Its first child has margin-top: 30px, and .parent has margin-top: 20px. How far does the top edge of .parent sit from the element above it, assuming that element has no bottom margin? Name one way to keep the child's margin inside the parent.
Show answer
The margins collapse, so the gap is 30px, the larger value. To keep the child's margin inside the parent, set display: flow-root on .parent. Adding padding or a border to the parent also works.
Exercise
Two siblings in a flex container each have margin-block: 20px. What is the vertical gap between them, and why?
Show answer
The gap is 40px. Flex items do not participate in margin collapsing, so the 20px bottom margin of the first item and the 20px top margin of the second item add together.