Search code examples
htmlcsswebkit

Why does -webkit-margin-collapse take the smallest value when there is an h1 inside of the body tag?


I'm just trying to understand the default stylesheet for Chrome.

If you have this html:

<body><h1>Hello</h1></body>

By default the <body> tag has a margin of 8px, and the <h1> tag has a default -webkit-margin-before of 0.67 em, which comes to 21.44px. Why then does the <h1> tag appear to ignore the 21.44px margin? Margin collapse rules should take the larger of the two margins.

Thanks!


Solution

  • In quirks mode, margin collapsing behaves differently in a variety of situations. In your example, the default top margin of any first child of body that has default margins, such as p or h1, will be ignored. I believe Chrome implements this behavior via the -webkit-margin-collapse property (as mentioned in your title); Firefox instead zeroes out the top margin of most body > :-moz-first-node elements (see quirk.css).

    You'll see the expected behavior when you include a DOCTYPE.