Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does changing the body's background color change the entire page's background?

I logged the height and padding values using this code:

jQuery( document ).ready( function() {

    console.log( jQuery('body').css('height') );
    console.log( jQuery('body').css('padding-top') );
    console.log( jQuery('body').css('padding-bottom'));

}); //end ready

This outputs:

20px
0px
0px

If the height of the body is only 20 pixels, why does the entire background of the browser change black when I use this CSS:

body {
    background: black;
}

I'm using Chrome as my browser. If you're curious as to how I ran into this question, I ran into a problem of adding a click event to the body that didn't ever seem to fire due to the body's default height.

like image 860
Kacy Avatar asked Mar 31 '14 19:03

Kacy


People also ask

Why should you use the body element to change the background color for the entire Web page?

The main reason is because the HTML takes the background-color of BODY since: The background of the root element becomes the background of the canvas and covers the entire canvas [...] So since the default background-color of HTML is transparent it will take the one from BODY .

What happens if color property is applied to the body element?

The body element is the root-element, and thus, as required by the CSS rules it loses its background style and the background style is applied to the containing canvas (the webpage area in the browser), therefor the entire screen is blue. The other properties stay with the element (e.g. the border ).

How do you change the background color of an entire page in HTML?

To set the background color in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <body> tag, with the CSS property background-color. HTML5 do not support the <body> tag bgcolor attribute, so the CSS style is used to add background color.

What body attributes can change the background color?

The HTML <body> bgcolor Attribute is used to define a Background color of a Document.


1 Answers

A long time ago there was something called document.bgcolor, or something like that, that would let you set the background of the document itself, but that was deprecated.

Instead it was decided that setting document.body.style.backgroundColor, or in other words setting the background of the body, would also set the background color for the document automagically, as the document object has no style property, but it's still visible when the body/html elements does not completely cover the document, that's why the entire page goes black even if the body element does not cover the entire document.

like image 129
adeneo Avatar answered Oct 19 '22 02:10

adeneo