Top and bottom margins of elements may occasionally collapse into a singular margin, which equals the larger of the two margins. This behavior does not occur with left and right margins.
Consider the following example:
Example
Illustration of margin collapsing:
h1 { margin: 0 0 50px 0; } h2 { margin: 20px 0 0 0; } |
In the given example, the <h1> element has a bottom margin of 50px, while the <h2> element has a top margin set to 20px.
]Intuitively, one might expect the vertical margin between the <h1> and the <h2> to sum up to 70px (50px + 20px). However, due to margin collapse, the actual margin ends up being 50px.
Property |
Description |
margin |
A single declaration shorthand for setting all margin properties. |
margin-bottom |
Specifies the margin at the bottom of an element. |
margin-left |
Specifies the margin at the left of an element |
margin-right |
Specifies the margin at the right of an element. |
margin-top |
Specifies the margin at the top of an element |