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?
Link to this sectionThe key is understanding when two margins are adjoining. Here are the basic situations:
Sibling Elements
The bottom margin of an element collapses with the top margin of its proceeding sibling.
Child Elements
The top margin of an element collapses with the top margin of its first child element.
The bottom margin of an element collapses with the bottom margin of its last child element.
An Element’s Own Top and Bottom Margins
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?
Link to this sectionThere are several exceptions to the 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 specs.
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.
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 Margin Collapse Resources
Link to this sectionThere 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.