According to w3, the margin of an element is transparent:
I've built a very basic HTML page:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
body {
background-color: red;
}
</style>
</head>
<body></body>
</html>
In Chrome, the inspector reports that "body" has an 8px margin:
But on the page, the margin is full of the background color! (Notice that there's no space between my bookmarks bar and the red background - I promise I haven't scrolled.
What's up with that?
The CSS specification special cases it.
The background of the root element becomes the background of the canvas and covers the entire canvas, anchored (for 'background-position') at the same point as it would be if it was painted only for the root element itself. The root element does not paint this background again.
For HTML documents, however, we recommend that authors specify the background for the BODY element rather than the HTML element. For documents whose root element is an HTML "HTML" element or an XHTML "html" element that has computed values of 'transparent' for 'background-color' and 'none' for 'background-image', user agents must instead use the computed value of the background properties from that element's first HTML "BODY" element or XHTML "body" element child when painting backgrounds for the canvas, and must not paint a background for that child element. Such backgrounds must also be anchored at the same point as they would be if they were painted only for the root element.
… presumably because people were too used to <body background="#ff0000">
from the bad old days.
来源:https://stackoverflow.com/questions/34681127/why-doesnt-html-body-background-color-obey-margin