Collapse of vertical margins
This page shows some examples showing how vertical margins collapse in the box model.
Margins collapse for block elements, and only for vertical margins. The collapse rule
states that the maximum margin is used to separate flowing elements, and not the sum of the single margins.
span
element
First paragraph, having style MARGIN:1px 0px;
Second paragraph, having style MARGIN:20px 0px;
Third paragraph, having style MARGIN:20px 0px;
span
element
Things get tricky when we consider nested elements. In the example below, the collapsed margin is the maximum value (i.e. 20px), but this margin is applied only to the container element, and not to the inner division.
span
element
The issue concerning nested elements can be solved remembering the conditions that inhibits the collapsing rule:
POSITION:absolute;
property is used, the final
behaviour changes if the set this property in the outermost element or in the innermost element.
span
element