JavaScript Document rootElement Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

document.rootElement returns the document’s root Element when that root is an SVG <svg> element; otherwise it returns null (MDN). Learn it for legacy SVG code — new work should use document.documentElement.

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.rootElement. Start with this line.

JavaScript
document.rootElement
CallWhat it does
document.rootElementPrimary signature.

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

Library declarations
JavaScript
document.rootElement

Parameters

ParameterDescription
Writable?Optional. Read-only
ReplacementRequired. document.documentElement

Return Value

Return ValueDescription
*SVG root Element, or null

JavaScript Examples

Example 1 — Read rootElement

On an HTML page, MDN-style behavior is null.

JavaScript
console.log(document.rootElement);
// null on HTML; SVGSVGElement on SVG documents
Try It Yourself

How It Works

  1. Read the property — MDN returns an SVG root or null.
  2. HTML demos almost always log null.
  3. SVG documents return the root <svg> element.
  4. Remember: this API is deprecated; demos only.

Example 2 — rootElement vs documentElement

See why HTML pages need documentElement.

JavaScript
console.log(document.rootElement);      // null (HTML)
console.log(document.documentElement);  // <html>…
console.log(document.documentElement.tagName); // "HTML"
Try It Yourself

How It Works

  1. rootElement — SVG-only root; HTML → null (MDN).
  2. documentElement — always the document root Element.
  3. tagName — on HTML pages, "HTML".
  4. Takeaway: never rely on rootElement for HTML.

Example 3 — Prefer documentElement

MDN migration pattern for new code.

JavaScript
// Prefer this everywhere (HTML + SVG)
const root = document.documentElement;
console.log(root.tagName);

// Avoid in new projects:
// document.rootElement
Try It Yourself

How It Works

  1. Skip rootElement in new projects (deprecated).
  2. Read documentElement — one API for every document type (MDN).
  3. Use tagName (or other Element APIs) on the root.
  4. Keep legacy reads only when maintaining old SVG code.

Browser Support

Document.rootElement is a legacy SVG Document extension. MDN marks it Deprecated. Engines may still expose it; new code should use document.documentElement.

✓ Deprecated · Legacy

Document.rootElement

SVG root Element or null. Prefer documentElement for all documents.

Legacy Deprecated
Google Chrome Legacy support
Yes*
Microsoft Edge Legacy support
Yes*
Mozilla Firefox Legacy support
Yes*
Apple Safari Legacy support
Yes*
Opera Legacy support
Yes*
Internet Explorer Legacy
Yes*
rootElement Legacy

Bottom line: Still present in many browsers for compatibility, but deprecated. See the MDN rootElement reference.

Frequently Asked Questions

MDN: for SVG documents, the Element that is the document root (often an SVGSVGElement); otherwise null. It is deprecated in favor of document.documentElement.
Yes. MDN marks Document.rootElement as Deprecated. Prefer document.documentElement, which returns the root element for all document types.
MDN scopes rootElement to SVG roots. On a typical HTML document it returns null. Use document.documentElement for the <html> element.
MDN: for a non-empty SVG document, rootElement is an SVGSVGElement identical to documentElement.
Always use document.documentElement. It is the standard property for the document’s root Element.
No. It is read-only. New code should read document.documentElement instead.

Next: scripts

Continue with the Document scripts property.

scripts →

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.

10 people found this page helpful