:root selects the single element that is the root of the document. Official jQuery docs note that in HTML this is always the <html> element — the same node as document.documentElement.
Remember
$(":root") // document root (1 element)
$(":root")[0].nodeName // "HTML" in HTML pages
$("html") // same node in HTML
document.documentElement // same DOM node (no jQuery)
// Always exactly one match
DOM.document.documentElement is the same reference.
CSS engine.querySelector(":root") also returns that node.
Takeaway.:root is standard CSS — native APIs support it.
Example 4 — Read / Set Attributes on the Root
Use the root element to inspect or update document-level attributes like lang.
jQuery
var before = $(":root").attr("lang") || "(none)";
$(":root").attr("lang", "en");
var after = $(":root").attr("lang");
$("#out").text("before: " + before + "\\nafter: " + after);
One line:$(":root") is the document’s top element — in HTML that means <html>, always exactly one match.
Frequently Asked Questions
:root selects the single element that is the root of the document. 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 node — the documentElement. $('html') uses the tag name; $(':root') uses the document-root pseudo-class. In XML or SVG 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 often used for custom properties on html. 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.
Use :root when you want document-root semantics (works even if the root tag is not html). Use $('html') when you specifically mean the HTML html element. For typical web pages they are interchangeable.