Shadow DOM vs Light DOM
What you'll learn: You'll understand the crucial difference between the main document's DOM tree (light DOM) and the isolated, encapsulated DOM created by shadow roots (shadow DOM).
The Two Parallel Worlds
When you create a shadow root on an element, you're establishing two separate DOM trees that coexist but remain largely independent:
Light DOM is the regular, familiar DOM tree — the one you see in your HTML, the one accessible to external CSS and JavaScript. It's the "public" content that lives in the main document. Think of it as the front yard of a house: visible, accessible, and styled by the neighborhood rules.
Shadow DOM is the encapsulated tree attached via attachShadow(). It lives inside a shadow host element but operates in its own isolated scope. This is like the interior of the house: private, protected from outside interference, and decorated by its own internal rules.
Key Distinctions
- Scope isolation: JavaScript selectors like
document.querySelector()cannot cross the shadow boundary — they only see light DOM. Code inside shadow DOM uses its own root. - CSS isolation: Styles defined in the shadow DOM don't leak out, and external styles (mostly) don't leak in.
- Slot-based composition: Light DOM content can be projected into shadow DOM using
<slot>elements, creating a powerful composition model.
<my-component> ← Shadow host (light DOM)
#shadow-root ← Shadow DOM boundary
<style>...</style> ← Encapsulated styles
<slot></slot> ← Projection point
<p>Hello</p> ← Light DOM (slotted content)
</my-component>
Key Takeaway: Light DOM is the public, document-level tree everyone can access, while shadow DOM is an encapsulated, private tree that provides style and markup isolation for web components.