0

I have set the height of the HTML tag to 100px and the background color to coral but it is applied to the whole document.

By default, the height of the HTML tag is auto and if I set the background color to that HTML tag then it is applied to the whole document which is obvious as the height is auto.

Please help me to understand the height property to HTML tag and body tag.

* {
  margin: 0;
  padding: 0;
}

html {
  height: 100px;
  background-color: coral;
}
<html>
    <head></head>
    <body></body>
</html>

enter image description here

Subrato Pattanaik
  • 5,331
  • 6
  • 21
  • 52

2 Answers2

2

Because that's what the specs ask for:

The background of the root element becomes the canvas background and its background painting area extends to cover the entire canvas.

Note that contrarily to what you thought, when the <html> node has its height set to auto, it won't cover the whole canvas either:

html { border: 1px solid red; }

Also note that even setting the rule on the <body> would have the same result if the <html> had its own background set to none, thanks to background-propagation.

* {
  margin: 0;
  padding: 0;
}

html {
  height: 100px;
  border: 1px solid green;
}
body {
  background-color: coral;
}
Kaiido
  • 123,334
  • 13
  • 219
  • 285
-2

The whole document is inside the tag. so when you put the colour to the html tag the colour is applied to the whole document