1

IE displays a default scrollbar on the page, which appears even if the content is too short to require a scrollbar.

The typical way to remove this scrollbar (if not needed), is to add this to your CSS:

html {
  height: 100%;
  overflow: auto;
}

I'm trying to do the same thing in Javascript (without requiring that in my CSS), but I can't seem to find a way to get access to the <html> element. I know I can access the <body> element with document.body, but that doesn't seem to be sufficient, I need the wrapping <html> element.

Any tips?

4
  • 3
    This is what CSS is for. This is not what Javascript is for. Why are you trying to do it in Javascript? Commented Jun 9, 2010 at 1:00
  • 1
    I disagree. (Not that that's what CSS for, but disagreeing that there aren't reasons why you'd need to do this in javascript.) The question is sound: ... how do you access the element? (styles and CSS aside.) Commented Jun 9, 2010 at 1:41
  • I wanted to this this conditionally (instead of applying it all the time). If that were not the case, i would just do it in CSS. Commented Jun 9, 2010 at 15:43
  • Even if you want to do it conditionally, it's better to do with a CSS class than with Javascript access to the style properties. But even so if the condition is applied client-side, you'd obviously need access to the <html> element. To Funka, I never said the question is invalid, I asked for clarification. Commented Jun 9, 2010 at 18:31

3 Answers 3

2

You're looking for the document.documentElement property.

Sign up to request clarification or add additional context in comments.

1 Comment

In IE, this element is only available in standards mode.
1

I guess for completeness' sake, I'll add another way to access it:

document.getElementsByTagName('html')[0];

Obviously this is a little more verbose, but it's always going to work regardless of the structure or standards-mode of your document.

Comments

0

You can also reach the HTML element with:

var html = document.body.parentNode;
alert(html.nodeName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.