Example 1 — Read on an HTML Page
On HTML, rootElement is typically null (MDN).
console.log(document.rootElement);
// null on typical HTML pages How It Works
This is why beginners should use document.documentElement instead.

Document.rootElement is a deprecated instance property that returned the SVG document root. Learn why MDN recommends documentElement instead, HTML vs SVG behavior, migration patterns, and five examples with try-it labs.
Instance property
Element | null
Deprecated
Legacy SVG
documentElement
Usually null
Every document has a single root element — the top-level node in the tree. For HTML, that is usually <html>. For standalone SVG documents, it is often an <svg> element. document.documentElement is the standard way to reach it today.
MDN: Document.rootElement returns the root Element if the document is an SVG root context; otherwise null. The property is deprecated in favor of document.documentElement, which works for all document types.
Whether you work with HTML, XHTML, or SVG, use document.documentElement. It is Baseline Widely available and the property MDN recommends instead of rootElement.
Related Document tutorials: documentElement, firstElementChild, body, Document constructor.
Document.rootElementA read-only instance property from the SVG document extensions. MDN marks it deprecated.
Element, often SVGSVGElement (MDN).null (MDN: otherwise null).documentElement for non-empty SVG docs (MDN).document.documentElement (MDN).document.rootElement An Element (SVG root) or null (MDN).
const root = document.documentElement;
console.log(root.tagName); // "HTML" on HTML pages | Goal | Code / note |
|---|---|
| Get root (modern) | document.documentElement |
| Legacy SVG only | document.rootElement (deprecated) |
| HTML rootElement | Usually null (MDN) |
| Root tag name | document.documentElement.tagName |
| Theme class on root | document.documentElement.classList |
| MDN status | Deprecated — use documentElement |
Four facts about document.rootElement.
Element|nullRead-only
deprecatedAvoid
documentElementMDN
nullTypical
rootElement vs documentElementrootElement | documentElement | |
|---|---|---|
| MDN status | Deprecated | Baseline Widely available |
| HTML documents | null | HTMLElement (<html>) |
| SVG documents | SVG root (MDN) | Same root (MDN) |
| Use in 2026? | No | Yes |
Examples follow MDN Document: rootElement. Try-it labs run in HTML, so rootElement is usually null — that demonstrates why you should use documentElement.
See deprecated behavior on an HTML page.
On HTML, rootElement is typically null (MDN).
console.log(document.rootElement);
// null on typical HTML pages This is why beginners should use document.documentElement instead.
documentElementShow the difference on the same HTML document.
console.log({
rootElement: document.rootElement,
documentElement: document.documentElement.tagName
}); MDN: for SVG, both can reference the same root; for HTML, only documentElement is useful.
Patterns for legacy code and modern replacements.
documentElementReplace deprecated reads with the standard property.
// Before (deprecated)
// const root = document.rootElement;
// After (MDN recommended)
const root = document.documentElement;
console.log(root.lang); One line change fixes most legacy snippets that wrongly used rootElement on HTML.
Check whether rootElement exists before reading (audits).
const hasRootElement = "rootElement" in document;
console.log("rootElement supported?", hasRootElement); Even when the property exists, prefer documentElement for actual root access.
Always return the document root via documentElement.
function getDocumentRoot() {
return document.documentElement;
}
const root = getDocumentRoot();
console.log(root.tagName, root.nodeName); This pattern works for HTML, XHTML, and SVG without touching deprecated APIs.
rootElement.document.documentElement.documentElement.classList.documentElement Replaced rootElementrootElementSVG document extensions exposed a root accessor for standalone SVG files.
documentElementOne property covers HTML, XHTML, and SVG document roots.
rootElementNew code should not rely on the SVG-only alias.
document.documentElementBaseline, clear, and works on every document type you will build today.
rootElement is usually null while documentElement is <html>.rootElement === documentElement.Document.rootElement is marked Deprecated on MDN. Prefer document.documentElement (Baseline). Logos use the shared browser-image-sprite.png sprite from this project.
Legacy SVG root accessor — deprecated in favor of document.documentElement.
Bottom line: Do not use rootElement in new projects. document.documentElement is Baseline and returns the root Element for HTML and SVG.
Document.rootElement was a legacy SVG root accessor. MDN deprecates it in favor of document.documentElement, which returns the root Element for all modern documents. On HTML pages, rootElement is usually null anyway.
Continue with scripts, documentElement, ownerDocument, firstElementChild, or the JavaScript hub.
document.documentElement for the rootrootElement reads in auditsdocumentElementlang / dir from the root elementrootElement in new HTML projectsrootElement to return <html>rootElement or documentElementdocument.bodyrootElementdocument.rootElementDeprecated SVG alias — use documentElement instead.
Element|null
APIDeprecated
MDNnull
TypicaldocumentElement
MDNsame root
LegacyMDN states that for a non-empty SVG document, document.rootElement is an SVGSVGElement identical to document.documentElement. That is why the standard property superseded the SVG-specific name.
Learn how to list every <script> tag with document.scripts.
6 people found this page helpful