JavaScript Document documentElement Property

Definition
Document.documentElement is a read-only property that returns the root Element of the document — for HTML, the <html> element (MDN).
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax
Use document.documentElement. Start with this line.
| Call | What it does |
|---|
document.documentElement | Primary signature. |
Built into the browser. Read or assign document.documentElement — no import required.
Library declarations
Parameters
| Parameter | Description |
|---|
| Attributes / children | Optional. lang, class, dir; head / body |
Return Value
| Return Value | Description |
|---|
| * | Root Element |
JavaScript Examples
Example 1 — Read the Root Element
Log the root’s tagName and lang.
const root = document.documentElement;
console.log(root.tagName); // "HTML"
console.log(root.getAttribute("lang")); // e.g. "en" or null
How It Works
- Get the root.
documentElement points at <html> on HTML pages. - Read
tagName. Uppercase "HTML" in HTML documents. - Read
lang. Comes from the lang attribute when present. - No replacement. The property is read-only — mutate attributes on the element instead.
Example 2 — Walk Direct Children (MDN Idea)
List element children of the root — typically head and body.
const root = document.documentElement;
const names = [];
for (const child of root.children) {
names.push(child.tagName);
}
console.log(names.join(", "));
// often: "HEAD, BODY"
How It Works
- Start at the root. Same
<html> element as Example 1. - Use
.children. Element-only list (skips whitespace text nodes). - Collect tag names. You usually see
HEAD then BODY. - MDN note.
childNodes also works but includes text nodes.
Example 3 — documentElement vs body
They are different nodes — compare with ===.
console.log(document.documentElement.tagName); // "HTML"
console.log(document.body.tagName); // "BODY"
console.log(document.documentElement === document.body); // false
console.log(document.body.parentElement === document.documentElement); // true
How It Works
- Root vs body.
documentElement is <html>; body is inside it. - Compare with
===. Different object references → false. - Check parent.
body.parentElement is the root. - Pick the right node. Page-wide attrs → root; content →
body.
Compatibility
Browser Support
Document.documentElement is Baseline Widely available (MDN: since July 2015). Returns the document’s root Element.
✓ Baseline · Widely available
Document.documentElement
Read-only root Element — <html> for HTML documents.
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full supportdocumentElement Excellent
Bottom line: Safe for production. Use it for root-level attributes (lang, class, dir) and tree walks; use document.body for page content.
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 not Deprecated, Experimental, or Non-standard.
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).
Next: documentURI
Learn the Document property that reports the document’s location as a string.
documentURI tutorial →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful