Of course. In CSS, by default, adjacent top and bottom margins overlap. This was a reasonable workaround before the adjacent selector ( +
) was counted / supported, as it meant that if you wrote h2 {margin-top: 3em;}
, you should have 3 places at a distance above your h2s, even if there was a paragraph in front of him with a lower limit of 1m.
In your second example, since the <div>
has no top or bottom padding or borders, its top and bottom fields are adjacent to the top and bottom fields by default <h1>
. Despite the fact that the <div>
s fields have no height, theyre still treated as if they existed, and therefore the <h1>
s fields should overlap them. Since the <div>
fields are by definition outside the background color <div>
, the <h1>
s fields must also be located outside.
In the first example, since the <div>
has a border, its fields no longer adjoin the <h1>
s fields, so there is no overlap. You can get the same effect by adding the top and bottom additions to the <div>
: http://jsfiddle.net/ahNUX/7/
(I'm not sure that you mean <div>
"reverse inheritance" of its child additions. In your examples, neither <div>
nor <h1>
have uppercase letters. The space inside the <div>
in the first example is created by the upper and lower fields <h1>
.)
Paul D. Waite
source share