JavaScript Document documentElement Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.documentElement is a read-only instance property that returns the root Element of the document—for HTML, the <html> element. Learn how it differs from document.body, how to read lang / dir / classes on the root, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

Element

03

HTML

<html>

04

vs

document.body

05

Common

lang / class

06

Status

Baseline widely

Introduction

Every HTML page has a tree that starts with the <html> tag. That element wraps <head> and <body>. In the DOM, that root element is document.documentElement.

MDN: the property returns the Element that is the root of the document (for HTML, the <html> element). For non-empty HTML documents it is always that element; for XML it is whatever element is the document root.

💡
Why use the root?

Theme classes (dark), lang, and dir are often set on <html>. document.documentElement is the standard handle for those page-wide attributes.

Related Document tutorials: doctype, body, dir, Document constructor.

Understanding Document.documentElement

A read-only instance property on Document. Its value is an Element—the document root.

  • HTML pages — almost always the <html> element (MDN).
  • XML pages — whichever element is the root (MDN).
  • Children — typically <head> and <body> (see MDN’s childNodes example).
  • Not assignable — mutate attributes on the element; do not replace the property.
  • Same object — usually identical to document.querySelector("html").

📝 Syntax

JavaScript
document.documentElement

Value

An Element object — the root element of the document (MDN).

MDN-style children walk

JavaScript
const rootElement = document.documentElement;
const firstTier = rootElement.childNodes;
// NodeList of direct children (e.g. head and body)

for (const child of firstTier) {
  // do something with each direct child of the root element
}

⚡ Quick Reference

GoalCode / note
Get rootdocument.documentElement
Tag namedocument.documentElement.tagName"HTML"
Set languagedocument.documentElement.lang = "en"
Theme classdocument.documentElement.classList.add("dark")
Directiondocument.documentElement.dir or document.dir
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.documentElement.

Type
Element

Root node

Access
read-only

No setter

HTML
<html>

Usual root

Status
baseline

Standard API

📋 Common root element tasks

TaskTypical code
Read page languagedocument.documentElement.lang
Toggle dark modeclassList.toggle("dark") on the root
Scroll metricsdocument.documentElement.scrollTop (with caveats vs body)
CSS custom propertiesstyle.setProperty("--brand", "#…") on the root

Examples Gallery

Examples follow MDN Document: documentElement. Use View Output or Try It Yourself for each case.

📚 Getting Started

Identify the root and walk its direct children (MDN).

Example 1 — Read the Root Element

Confirm tag name and that it is the <html> element.

JavaScript
const root = document.documentElement;
console.log(root.tagName); // "HTML"
console.log(root === document.querySelector("html")); // true
Try It Yourself

How It Works

tagName for HTML elements is typically uppercase in HTML documents.

Example 2 — Direct Children of the Root (MDN)

List child node names under documentElement.

JavaScript
const rootElement = document.documentElement;
const names = [];

for (const child of rootElement.childNodes) {
  if (child.nodeType === Node.ELEMENT_NODE) {
    names.push(child.nodeName);
  }
}

console.log(names.join(", "));
// often "HEAD, BODY"
Try It Yourself

How It Works

childNodes also includes text/whitespace nodes—filter to ELEMENT_NODE when you only want tags.

📈 Body, Lang & Themes

Everyday uses of the root element handle.

Example 3 — documentElement vs body

Root and body are different nodes in the same tree.

JavaScript
console.log("root:", document.documentElement.tagName);
console.log("body:", document.body.tagName);
console.log(
  "body is child of root:",
  document.body.parentElement === document.documentElement
);
Try It Yourself

How It Works

Put page chrome / theme on the root; put content and most UI inside body.

Example 4 — Read and Set lang on the Root

Page language belongs on <html lang>.

JavaScript
const html = document.documentElement;
console.log("Before:", html.lang);

html.lang = "en-US";
console.log("After:", html.lang);
Try It Yourself

How It Works

Updating lang helps accessibility tools and can affect hyphenation / fonts in some engines.

Example 5 — Theme Class on <html>

Toggle a dark-mode class on the document root.

JavaScript
const root = document.documentElement;
root.classList.toggle("dark");

console.log("Has dark:", root.classList.contains("dark"));
console.log("className:", root.className);
Try It Yourself

How It Works

CSS like html.dark { … } or :root.dark can theme the whole page from one class.

🚀 Common Use Cases

  • Dark / light themes — toggle classes on the root.
  • i18n — set lang and pair with dir.
  • CSS variables — store design tokens on :root / html.
  • Tree walks — start from the root when listing top-level structure.
  • Feature flagsdata-* attributes on <html> for global config.
  • Scroll helpers — some layouts use documentElement.scrollTop / clientHeight.

🧠 Where documentElement Sits in the Tree

1

Document node

The document object owns the whole tree.

Document
2

Optional doctype

document.doctype may appear before the root element.

DOCTYPE
3

Root element

document.documentElement<html> on web pages.

Root
4

Head and body inside

Direct element children are usually head then body.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Read-only; returns an Element.
  • Non-empty HTML documents: always an <html> element (MDN).
  • XML: root may be any element name (MDN).
  • Related: body, doctype, dir, Document constructor.

Universal Browser Support

Document.documentElement is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.documentElement

Read-only root Element of the document — the

element on HTML pages.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported (legacy)
Full support
Document.documentElement Excellent

Bottom line: Use document.documentElement to access the page root. Prefer it for lang, dir, and theme classes; use document.body for page content.

Conclusion

Document.documentElement is the standard way to reach the document’s root element—almost always <html>. Use it for page-wide attributes and classes, and remember it is different from document.body.

Continue with documentURI, body, doctype, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Put global theme / lang / dir on documentElement
  • Use classList instead of rewriting className blindly
  • Prefer documentElement over hard-coded querySelector("html")
  • Keep content in body; keep document chrome on the root
  • Remember XML roots may not be named html

❌ Don’t

  • Assign to document.documentElement
  • Confuse the root with document.body
  • Assume childNodes contains only elements
  • Dump large UI into attributes on <html>
  • Forget that body can be null very early in parsing

Key Takeaways

Knowledge Unlocked

Five things to remember about document.documentElement

The document root Element — usually <html>.

5
Core concepts
02

Status

baseline

Standard
🔒03

Access

read-only

DOM
📄04

HTML

<html>

Typical
👤05

vs

document.body

Compare

❓ Frequently Asked Questions

The Element that is the root element of the document. For HTML documents that is the <html> element.
No. MDN marks Document.documentElement as Baseline Widely available (since July 2015). It is a standard read-only Document instance property.
documentElement is the root (<html>). document.body is the <body> element inside that root. Head and body are typically children of documentElement.
MDN: for any non-empty HTML document it will always be an <html> element. Empty or unusual documents may differ; still null-check in defensive code.
No. It is read-only. You can change attributes on the element (lang, class, dir) but you cannot replace the root via this property.
MDN: for any non-empty XML document, documentElement is whatever element is the root of that document (not necessarily html).
Did you know?

In CSS, the :root pseudo-class selects the same element as document.documentElement in HTML documents. That is why global custom properties are often declared under :root { }.

Next: documentURI

Learn how to read the document location as a string.

documentURI →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful