$("#toggle").click(function(){
$("html").toggleClass("bg");
});
html.bg {
background: blue;
}
body {
background: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html class="bg">
<head>
</head>
<body>
Test
<br>
<button id="toggle">Toggle HTML background</button>
</body>
</html>
I found that if you apply a CSS background to body, it takes up the whole page (no matter what the actual height or width of body is).
However, if you apply a CSS background to both html and body, the background for body does not take up the whole page.
Is this discrepancy expected behavior?
How would I go about superimposing two fullscreen backgrounds (say, a background color and a semi-transparent image?)
This is correct behavior.1 In standards mode,
body, as well ashtml, doesn’t immediately take up the entire height of the viewport, even though it appears so when you only apply a background to the latter. In fact, thehtmlelement will take on the background ofbodyif you don’t give it its own background, andhtmlwill pass this on to the canvas:That said, however, you can superimpose any background image over a background color on a single element (either
htmlorbody), without having to rely on two elements — simply usebackground-colorandbackground-imageor combine them in thebackgroundshorthand property:If you wish to combine two background images, you need to rely on multiple backgrounds. There are chiefly two days to do this:
In CSS2, this is where styling both elements comes in handy: simply set a background image to
htmland another image tobodywhich you wish to superimpose over the first. To ensure the background image onbodydisplays at full viewport height, you need to applyheightandmin-heightrespectively as well:Incidentally, the reason why you have to specify
heightandmin-heighttohtmlandbodyrespectively is because neither element has any intrinsic height. Both areheight: autoby default. It is the viewport that has 100% height, soheight: 100%is taken from the viewport, then applied tobodyas a minimum to allow for scrolling of content.In CSS3, the syntax has been extended so you can declare multiple background values in a single property, eliminating the need to apply backgrounds to multiple elements (or adjust
height/min-height):The only caveat is that in a single multi-layered background, only the bottommost layer may have a background color. You can see in this example that the
transparentvalue is missing from the upper layer.And don’t worry — the behavior specified above with propagating background values works exactly the same even if you use multi-layered backgrounds.
If you need to support older browsers, though, you’ll need to go with the CSS2 method, which is supported all the way back to IE7.
My comments under this other answer explain, with an accompanying fiddle, how
bodyis actually offset fromhtmlby default margins even though it looks like it’s being padded out instead, again owing to this seemingly strange phenomenon.1 This may have its roots in setting the HTML
backgroundandbgcolorattributes ofbodycausing the background attribute to apply to the entire viewport. More on that here.