jQuery :root Selector

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Document root

What You’ll Learn

The :root selector matches the document root element — exactly one node. In HTML, official jQuery docs say it is always the <html> element. Available since jQuery 1.9; also valid standard CSS.

01

Syntax

:root

02

One match

Document top

03

Official

nodeName demo

04

vs html

Same in HTML

05

CSS :root

Variables

06

Native

querySelector

Introduction

Every HTML page is a tree of nested elements. At the very top sits one root node — for HTML documents, that is the <html> element. jQuery’s :root pseudo-class selects that single root element, no matter how deep your page content grows.

Added in jQuery 1.9, :root aligns with the standard CSS :root pseudo-class. The official demo reads $(":root")[0].nodeName and displays it — typically HTML in uppercase for HTML documents.

Understanding the :root Selector

Think of :root as “give me the top of the document tree”:

  • $(":root") → exactly one element — the document root.
  • In HTML → same node as $("html") and document.documentElement.
  • In XML/SVG → root tag name may differ, but :root still finds the top node.
  • CSS :root { --color: blue; } sets variables on that same element.
💡
Beginner Tip

Most day-to-day jQuery code uses $("html") or $(document.documentElement). Reach for :root when you want the semantic “document root” name — especially in code shared across HTML and XML contexts.

📝 Syntax

Official jQuery API form (since 1.9):

jQuery
jQuery( ":root" )

// read the root tag name:
$( ":root" )[ 0 ].nodeName

// HTML equivalent selectors:
$( "html" )
$( document.documentElement )

// native CSS (standard):
document.querySelector( ":root" )

Parameters

  • No arguments — returns the single root element of the current document.

Return value

  • A jQuery object containing exactly one element — the document root.

Official jQuery API example

jQuery
$( "<span>" )
  .html( $( ":root" )[ 0 ].nodeName )
  .appendTo( "#log" );

⚡ Quick Reference

Selector / APIHTML documentMatch count
$(":root")<html>1
$("html")<html>1
document.documentElement<html>1 node
document.querySelector(":root")<html>1 (native CSS)
$("body")<body>1 (not root)

📋 :root vs html and body

Document root vs common page containers.

:root
:root

Document root (1)

html
html

Same node in HTML

body
body

Page content host

documentElement
document.documentElement

Native DOM API

Examples Gallery

Example 1 follows the official jQuery :root demo. Examples 2–5 cover match count, comparison with html, setting root attributes, and native querySelector.

📚 Official jQuery Demo

Display the root element’s tag name.

Example 1 — Official Demo: Root nodeName

Official jQuery demo — append a span showing the root tag name to #log.

jQuery
$( "<span>" )
  .html( $( ":root" )[ 0 ].nodeName )
  .appendTo( "#log" );
Try It Yourself

How It Works

$(":root")[0] is the DOM node; .nodeName returns HTML for HTML documents.

Example 2 — Exactly One Match

Verify that :root always returns a single-element jQuery collection.

jQuery
console.log( ":root length →", $( ":root" ).length );
console.log( "tagName →", $( ":root" )[ 0 ].tagName );
Try It Yourself

How It Works

Unlike list or form selectors, :root never matches zero or many — always exactly one root node.

📈 Practical Patterns

Compare selectors, set attributes, and use native APIs.

Example 3 — :root vs html

Confirm both selectors reference the same DOM node in HTML documents.

jQuery
var same = $( ":root" )[ 0 ] === $( "html" )[ 0 ];
console.log( "Same element?", same );
Try It Yourself

How It Works

In HTML, :root and html resolve to document.documentElement.

Example 4 — Set lang on the Root

Update the document language attribute through :root — affects accessibility and translation tools.

jQuery
$( ":root" ).attr( "lang", "en-US" );
$( "#langOut" ).text( "Root lang → " + $( ":root" ).attr( "lang" ) );
Try It Yourself

How It Works

Because :root is the html element, attribute changes apply document-wide.

Example 5 — Native querySelector(":root")

Unlike many jQuery extensions, :root is standard CSS — use it without jQuery when needed.

jQuery
var root = document.querySelector( ":root" );
console.log( root === document.documentElement ); // true in HTML

// CSS custom properties often target :root in stylesheets:
// :root { --brand: #2563eb; }
Try It Yourself

How It Works

CSS authors use :root for design tokens; jQuery uses the same selector to reach that node in script.

🚀 Common Use Cases

  • Language / direction — set lang or dir on :root.
  • Theme toggles — add data-theme on the root for CSS variable switching.
  • Debugging — log root nodeName in embedded or XML contexts.
  • Feature detection — compare root tag across HTML vs SVG documents.
  • Global classes — add no-js / js class on root (often done at load).
  • CSS variables — mirror stylesheet :root { --token: value; } patterns in jQuery scripts.

🧠 How jQuery Evaluates :root

1

Parse selector

Recognize the :root pseudo-class — no prefix or argument needed.

Query
2

Resolve document

Use the active document’s top-level element — not scoped to a subtree.

Context
3

Return root node

In HTML, that node is document.documentElement (<html>).

Match
4

Single-element collection

Chain .attr(), .addClass(), or read [0].nodeName.

📝 Notes

  • Available since jQuery 1.9 — selects the document root element.
  • In HTML, root is always the html element (official jQuery docs).
  • Standard CSS — works in native querySelector(":root").
  • Always matches exactly one element — never zero, never many.
  • Not the same as body — body is a child of the root.
  • Do not confuse with form input type="reset" — that uses the :reset selector instead.

Browser Support

The :root pseudo-class is standard CSS and works in jQuery 1.9+. Native document.querySelector(":root") is supported in all modern browsers — it returns the same node as document.documentElement in HTML.

jQuery 1.9+ · standard CSS

jQuery :root Selector

Document root — html in HTML pages; native querySelector supported.

100% jQuery + native CSS
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:root Universal

Bottom line: One match only; in HTML equals $('html') and document.documentElement.

Conclusion

The :root selector matches the single document root — in HTML, the <html> element. The official demo displays its nodeName.

Use it when you want the semantic document root; in everyday HTML pages, $("html") is equally valid.

💡 Best Practices

✅ Do

  • Use :root for document-wide lang or theme attributes
  • Remember it always returns exactly one element
  • Pair with CSS :root { --var: ...; } for design tokens
  • Use querySelector(":root") when jQuery is not loaded
  • Prefer semantic root naming in XML/multi-format apps

❌ Don’t

  • Confuse :root with :reset (form button)
  • Expect :root to match body
  • Assume a loop over $(":root") will iterate many nodes
  • Scope :root inside a container — it always targets the document
  • Replace every $("html") without reason — both work in HTML

Key Takeaways

Knowledge Unlocked

Five things to remember about :root

One node — top of the tree.

5
Core concepts
1 02

One match

Always

Rule
html 03

HTML

Same node

Tip
CSS 04

Standard

Native OK

Compare
demo 05

Official

nodeName

Demo

❓ Frequently Asked Questions

:root selects the element that is the root of the document — exactly one node. In HTML, official jQuery docs state that root is always the html element. Available since jQuery 1.9.
In HTML documents they target the same element — the documentElement. $('html') uses the tag name; $(':root') uses the document-root pseudo-class. In XML or SVG documents the root tag name may differ, but :root still finds the top-level element.
Always one — the single root node of the document tree. The official demo reads $(':root')[0].nodeName and displays it.
They target the same concept — the document root. CSS :root is standard and widely used for custom properties on the html element. jQuery :root (since 1.9) selects that same root node in the DOM.
Yes. Unlike many jQuery form pseudo-classes, :root is standard CSS. document.querySelector(':root') returns the root element in modern browsers — equivalent to document.documentElement in HTML.
It creates a span, sets its HTML to the root element's nodeName (HTML in uppercase for HTML documents), and appends it to #log — proving :root resolves to the html element.
Did you know?

CSS authors declare global design tokens with :root { --primary: #2563eb; } because custom properties inherit from the root. jQuery’s $(":root") targets that same html node — so you can read or update attributes in script while variables live in CSS on the identical element.

Continue to :target Selector

Learn how to select the element referenced by the URL hash fragment.

:target selector 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.

6 people found this page helpful