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
Fundamentals
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.
Concept
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.
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.
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.
Important
📝 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
:rootUniversal
Bottom line: One match only; in HTML equals $('html') and document.documentElement.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :root
One node — top of the tree.
5
Core concepts
🌿01
:root
Doc top
API
102
One match
Always
Rule
html03
HTML
Same node
Tip
CSS04
Standard
Native OK
Compare
demo05
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.