Shadow DOM roots and encapsulation
Quick answer
Shadow DOM provides a separate node tree attached to a host element. It supports component-local structure and style scoping, not a security sandbox.
How it works
On a supported host call attachShadow({mode:"open"}) or attachShadow({mode:"closed"}). The returned ShadowRoot can contain ordinary elements. In open mode host.shadowRoot exposes it; in closed mode that property is null, but the creator can retain the returned reference. Document queries do not descend through ordinary shadow boundaries; query the root explicitly. Styles generally scope to their tree, while inherited properties and CSS custom properties can cross through inheritance. Do not use closed mode to protect secrets.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const host=document.createElement("div");document.body.append(host);const root=host.attachShadow({mode:"open"});const span=document.createElement("span");span.id="lesson-inner";span.textContent="ready";root.append(span);console.log(document.querySelector("#lesson-inner")===null,root.querySelector("#lesson-inner").textContent);})()Expected console output:
true readyDocument lookup does not enter the shadow tree.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const host=document.createElement("div");const root=host.attachShadow({mode:"closed"});console.log(host.shadowRoot===null,root.host===host);})()Expected console output:
true trueClosed mode hides the root from the host property but the returned root still exists.
Common mistakes
Do not confuse open mode with global document selection or assume closed mode is a security boundary.
Practice
Attach a shadow root to a div and compare document queries with root queries. Repeat with closed mode.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Shadow DOM roots and encapsulation — Knowledge Check
Apply the concept and reason about local example output.
Where should code query for an element inside an open shadow root?
What does host.shadowRoot return for a closed root?
Can inherited CSS custom properties affect content in a shadow tree?
What is the expected console output of Shadow DOM roots and encapsulation example 1?
(async () => {const host=document.createElement("div");document.body.append(host);const root=host.attachShadow({mode:"open"});const span=document.createElement("span");span.id="lesson-inner";span.textContent="ready";root.append(span);console.log(document.querySelector("#lesson-inner")===null,root.querySelector("#lesson-inner").textContent);})()What is the expected console output of Shadow DOM roots and encapsulation example 2?
(async () => {const host=document.createElement("div");const root=host.attachShadow({mode:"closed"});console.log(host.shadowRoot===null,root.host===host);})()