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:

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:

Empty Blocks

An empty block element can collapse its own top and bottom margins into a single margin if it meets specific structural criteria:

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: