Why Do CSS Margins Collapse in Block Contexts?
Margin collapsing in CSS occurs when the vertical margins of two adjacent block-level boxes combine into a single margin instead of adding together. This core layout behavior happens exclusively in normal flow within a Block Formatting Context (BFC) across three primary scenarios: adjacent siblings, parent-child relationships lacking separating boundaries, and empty blocks. Understanding the specific structural conditions that trigger or prevent this mechanism is essential for predictable web layouts.
Adjacent Siblings
The most common trigger for margin collapsing is consecutive sibling elements in the normal document flow. When two block-level elements sit directly on top of one another without any clearance, inline content, or intervening padding/borders, their touching margins combine.
The resulting space between the elements is not the sum of both margins. Instead, the browser applies the following rules:
- Two positive margins: The collapsed margin equals the maximum of the two values. For example, a bottom margin of 30px followed by a top margin of 20px yields a 30px gap.
- Positive and negative margins: The browser subtracts the absolute value of the largest negative margin from the largest positive margin.
- Two negative margins: The browser takes the most negative value (the one with the largest absolute value).
Parent and Child Elements
A parent element and its first or last block child can collapse their
margins if nothing separates them. This often leads to unexpected
spacing where applying margin-top to a child element pushes
the entire parent down instead of creating internal spacing.
Parent-child collapse triggers under two conditions:
- Top margins: The parent element has no top border, no top padding, no inline content, and no clearance separating its top margin from the first child's top margin.
- Bottom margins: The parent element has no bottom
border, no bottom padding, no inline content, and has an
autoor unspecifiedheight(ormin-height/max-heightconstraints that do not establish a fixed barrier) separating its bottom margin from the last child's bottom margin.
Empty Blocks
An empty block element can collapse its own top and bottom margins into a single margin if it meets specific structural criteria:
- It contains no text, inline elements, or child elements.
- It has no border (
border-toporborder-bottom). - It has no padding (
padding-toporpadding-bottom). - It has no explicit
heightormin-heightdefined.
When these conditions are met, the element's top and bottom margins collapse together. If this empty block sits between other block elements, its combined margin will also collapse with the adjacent siblings' margins.
Conditions That Prevent Margin Collapsing
Margin collapsing is strictly a feature of standard vertical block layouts. It is automatically disabled when elements establish new formatting contexts or change layout models:
- Flexbox and Grid containers: Margins on flex items and grid items never collapse.
- Floated elements: A floating box does not collapse margins with any other box.
- Positioned elements: Elements with
position: absoluteorposition: fixeddo not participate in margin collapsing. - Inline-block elements: Elements styled with
display: inline-blockdo not collapse margins with neighbors or parents. - New formatting contexts: Setting
overflow: hidden,overflow: auto,display: flow-root, orcontain: layoutcreates a new BFC, preventing margins inside the container from collapsing with margins outside it. - Separation barriers: Adding at least
1pxof padding, a1pxsolid/transparent border, or line content between the margin boundaries will stop parent-child or empty-block collapse immediately.