When setting the background color for the html
element, it's important to note that if you don't specify a color, the browser will default to using the background color of the body
element instead. This may result in not seeing the intended difference.
To resolve this issue, simply assign a different background color to the html
element and also ensure that the body
element has a specified height:
html {
background-color: blue; /* new */
}
body {
border: 1px solid red;
width: 600px;
height: 600px; /* new */
margin: 0 auto;
background: white;
}
Exploring the connection between html
, body
, and background-color
.
The default background-color
value for all elements is set to transparent
.
If no specific background-color
is defined for the html
element (i.e., remains transparent
), then according to CSS rules, the browser will inherit the background-color
from the body
element.
As stated in the specifications:
3.11.2. The Canvas Background and the HTML <body>
Element
In documents where the root element is an HTML HTML
element or an XHTML html
element: If the computed value of background-image
on the root element is none
and its background-color
is transparent
, browsers are required to adopt the computed values of the background properties from the first HTML BODY
or XHTML body
child element. The initial values of these BODY
element's background properties are used, and the inherited values are treated as if they were initially specified on the root element. Authors of HTML documents are advised to define the canvas background for the BODY
element rather than the HTML
element.