How to change the width of the html selector with javascript

190 views Asked by At

In my CSS I set the width of the html like this:

html {width: 1000px; }

I would like to be able to change this width dynamically with javascript similar to setting the style.width of an element. The window.document node doesn't have a style property and I can't find the width anywhere.

Where in the DOM is the 1000px stored and can I change it dynamically?

2

There are 2 answers

1
Shomz On BEST ANSWER

You can always get it like:

document.documentElement

or:

document.getElementsByTagName('html')[0]

or even:

document.body.parentElement

So to get the CSS width, just do:

document.documentElement.style.width

Docs: https://developer.mozilla.org/en-US/docs/Web/API/document.documentElement

Returns the Element that is the root element of the document (for example, the <html> element for HTML documents).

0
maček On

I like this way, personally

document.body.parentNode.style.width = "1000px";