CKTIS / JavaScript
ShadowRoot — the root of a Shadow DOM tree
ShadowRoot represents the root of a Shadow DOM tree associated with a host element. It provides access to a component's internal structure. The open and closed modes differ in access through host.shadowRoot, but neither creates a security boundary.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
const root = host.attachShadow({ mode: "open" });
const sameRoot = host.shadowRoot;
root.append(document.createElement("span"));ShadowRoot is the root of a Shadow DOM tree, a specialized DocumentFragment associated with a host element. Its internal nodes form a separate tree: host.querySelector() does not search them, whereas root.querySelector() searches inside it. The root.host property identifies the host.
Create a root with host.attachShadow({ mode: "open" }), not new ShadowRoot(). The method returns the root; with open, host.shadowRoot also exposes it. With closed, that property returns null, but a retained reference still works. closed restricts access through that property; it does not protect against untrusted code.
Shadow DOM supports component encapsulation, but it is neither a separate document nor complete style isolation: CSS inheritance still matters. A slot element can present the host's children without moving them into the shadow tree. You do not need to register a custom element to use Shadow DOM on a supported host such as div.
This is a web-platform API, not a feature of a particular ECMAScript edition. Having Node 24 in a repository does not establish browser support. The example requires a browser with attachShadow and compares both modes without modifying the page.
Examples
(() => {
const host = document.createElement("div");
const root = host.attachShadow({ mode: "open" });
const label = document.createElement("span");
label.textContent = "Hello";
root.append(label);
console.log(root instanceof ShadowRoot);
console.log(host.shadowRoot === root);
console.log(root.host === host);
console.log(root.querySelector("span").textContent);
console.log(host.querySelector("span") === null);
const closedHost = document.createElement("div");
const closedRoot = closedHost.attachShadow({ mode: "closed" });
console.log(closedHost.shadowRoot === null);
console.log(closedRoot.host === closedHost);
})();Result
true true true Hello true true true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.