Forum Moderators: not2easy
[size=3]<div style="background: red; margin: 0;">
<p style="background: yellow; margin: 20px;">Test</p>
</div>
<div style="background: red; margin: 0; border: solid 1px black;">
<p style="background: yellow; margin: 20px;">Test</p>
</div>[/size]
<div style="background: red; padding: 20px; margin-bottom: 20px;">
<p style="background: yellow;">Test</p>
</div>
<div style="background: red; padding: 20px; border: solid 1px black;">
<p style="background: yellow;">Test</p>
</div>
Until now I tough it just collapse margins of two horizontally neighbour elements into the bigger one, but it seems more complicated.Not really more complicated ;) - the example highlights that this is really about adjoining margins and containing blocks (rather than collapse) - you were right in your intial thinking that the margins of the para were affecting the first div/p combination.
collapsing margins means that adjoining margins (no non-empty content, padding or border areas or clearance separate them) of ... boxes (which may be ... nested) combine to form a single margin.
Each box has a content area (e.g., text, an image, etc.) and optional surrounding padding, border, and margin
If 'height' is 'auto', the height depends on whether the element has any block-level children and whether it has padding or borders: ...
If it has block-level children, ... and/or top border, then the content starts at the top margin edge of the topmost child. Similarly, the content ends at the bottom margin edge of the bottommost child.
An element's own margins are adjoining if the 'min-height' property is zero, and it has neither top or bottom borders nor top or bottom padding, and it has a 'height' of either 0 or 'auto' and it does not contain a line box, ...
If the top and bottom margins of a box are adjoining, then it is possible for margins to collapse through it.
# If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's.
# If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's.