tkane
tkane

Reputation: 1777

How can I reference the <HTML> element's corresponding DOM object?

This is how you access the <body> element to set a background style:

document.body.style.background = '';

But how can I access the <html> element in a similar manner? The following doesn't work:

document.html.style.background = '';

Upvotes: 43

Views: 20025

Answers (3)

Kunal Vashist
Kunal Vashist

Reputation: 2471

Why too set the style on the HTML , do use body tag for any styling ..

to change the background applied on html you have to get to the root of the page

using document.documentElement can do

Use this

document.documentElement.style.background = '';

Upvotes: 0

Quentin
Quentin

Reputation: 944008

The root element (<html>) can be found in document.documentElement, not document.html.

This is because documentElement is standard DOM and not an HTML specific extension.

Upvotes: 9

Rob W
Rob W

Reputation: 349122

The <html> element can be referred through the document.documentElement property. In general, the root element of any document can be referred through .documentElement.

document.documentElement.style.background = '';

Note: .style.background returns the value for background as an inline style property. That is, defined using <html style="background:..">. If you want to get the calculated style property, use getComputedStyle:

var style = window.getComputedStyle(document.documentElement);
var bgColor = style.backgroundColor;

Upvotes: 57

Related Questions