JavaScript Document documentElement Property

Beginner
5 min read
Updated: Nov 2026
3 examples
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.

JavaScript
document.documentElement
CallWhat it does
document.documentElementPrimary signature.

Built into the browser. Read or assign document.documentElement — no import required.

Library declarations
JavaScript
document.documentElement

Parameters

ParameterDescription
Attributes / childrenOptional. lang, class, dir; head / body

Return Value

Return ValueDescription
*Root Element

JavaScript Examples

Example 1 — Read the Root Element

Log the root’s tagName and lang.

JavaScript
const root = document.documentElement;
console.log(root.tagName);                 // "HTML"
console.log(root.getAttribute("lang"));    // e.g. "en" or null
Try It Yourself

How It Works

  1. Get the root. documentElement points at <html> on HTML pages.
  2. Read tagName. Uppercase "HTML" in HTML documents.
  3. Read lang. Comes from the lang attribute when present.
  4. 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.

JavaScript
const root = document.documentElement;
const names = [];
for (const child of root.children) {
  names.push(child.tagName);
}
console.log(names.join(", "));
// often: "HEAD, BODY"
Try It Yourself

How It Works

  1. Start at the root. Same <html> element as Example 1.
  2. Use .children. Element-only list (skips whitespace text nodes).
  3. Collect tag names. You usually see HEAD then BODY.
  4. MDN note. childNodes also works but includes text nodes.

Example 3 — documentElement vs body

They are different nodes — compare with ===.

JavaScript
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
Try It Yourself

How It Works

  1. Root vs body. documentElement is <html>; body is inside it.
  2. Compare with ===. Different object references → false.
  3. Check parent. body.parentElement is the root.
  4. Pick the right node. Page-wide attrs → root; content → body.

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.

99% Widely available
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 support
documentElement 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

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.

11 people found this page helpful