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
Fundamentals
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.
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").
Foundation
📝 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
}
Compare
⚖️ Root vs body vs document
API
Points to
Notes
document.documentElement
Root element
<html> on HTML pages
document.body
<body>
May be null before body exists
document.head
<head>
Sibling of body under the root
document.doctype
DocumentType
Not an Element; sits before the root
document
Document node
Owner of the whole tree
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Get root
document.documentElement
Tag name
document.documentElement.tagName → "HTML"
Set language
document.documentElement.lang = "en"
Theme class
document.documentElement.classList.add("dark")
Direction
document.documentElement.dir or document.dir
MDN status
Baseline Widely available
Snapshot
🔍 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
Compare
📋 Common root element tasks
Task
Typical code
Read page language
document.documentElement.lang
Toggle dark mode
classList.toggle("dark") on the root
Scroll metrics
document.documentElement.scrollTop (with caveats vs body)
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported (legacy)
Full support
Document.documentElementExcellent
Bottom line: Use document.documentElement to access the page root. Prefer it for lang, dir, and theme classes; use document.body for page content.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.documentElement
The document root Element — usually <html>.
5
Core concepts
🌳01
Returns
root Element
API
✓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 { }.