Skip to content
Jonathan Harrell

Article tags

  • css
  • layout

What’s the Deal with Margin Collapse?

The concept of margin collapse is foundational to an understanding of the box model in CSS, but it is actually quite complex and potentially confusing. The spec describing how it works is thorough but difficult to understand. This article is an attempt to give some visual examples to the concepts from the specs.

The basic idea is that if two margins are adjoining, they will collapse into one margin, which will have the greater of the two margin values (it will be the more negative of the margins if both margins are negative).

What makes margins adjoining?

The key is understanding when two margins are adjoining. Here are the basic situations:

Sibling Elements

30px30px30px
An element with a bottom margin of 30px followed by a sibling element with a top margin of 30px will have their margins collapsed into a total margin of 30px between them.

The bottom margin of an element collapses with the top margin of its proceeding sibling.

Child Elements

30px30px30px
An element with a top margin of 30px and a first child element with a top margin of 30px will collapse so that the child element’s margin collapses with that of its parent.

The top margin of an element collapses with the top margin of its first child element.

30px 30px 30px
An element with a bottom margin of 30px and its last child element with a bottom margin of 30px will collapse so that the child element’s margin collapses with that of its parent.

The bottom margin of an element collapses with the bottom margin of its last child element.

An Element’s Own Top and Bottom Margins

30px30px0px0px
An element that doesn’t take up space in the flow of the page will have its top and bottom margins collapse to 0.

The top and bottom margins of an element collapse if the element has no height, padding, or border and all of its children elements’ margins collapse (height is represented here only for clarity).

When does margin collapse not occur?

There are several exceptions to these rules. This is where things can get hard to follow. Following are some visual examples of situations where margins would not collapse. For a more complete understanding, refer to the spec.

30px 30px 30px 30px
A parent element with a top border or top padding and a top margin of 30px will not collapse with its first child element’s top margin of 30px.

If the parent element has a top border or padding, the parent’s top margin does not collapse with the first child’s top margin.

30px 30px 30px 30px
A parent element with a bottom border or bottom padding and a bottom margin of 30px will not collapse with its last child element’s bottom margin of 30px.

If the parent element has a bottom border or padding, the parent’s bottom margin does not collapse with the last child’s bottom margin.

Further Resources

There are some additional, more complex scenarios that prevent collapse that aren’t covered here. For updated and complete information, see the CSS Box Model Spec.

Subscribe

Want more front-end tips and tricks? Sign up for my newsletter to stay up-to-date.