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 margin collapse 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 behind margin collapse 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 to understanding margin collapse is understanding when two margins are adjoining. Here are the basic situations:
An Element’s Own Top and Bottom Margins
When do margins not collapse?
There are several exceptions to the margin collapse 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.
There are some additional, more complex scenarios that prevent margin collapse that aren’t covered here. For updated and complete information about margin collapse, see the CSS Box Model Spec.