Skip to content

Encapsulation in Shadow DOM — Complete Guide

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Encapsulation in Shadow Dom. We cover key concepts, practical examples, and best practices to help you master this topic.

Shadow DOM encapsulation isolates DOM structure, styles, and behavior from the host page, preventing CSS leaks and DOM access from outside.

What You'll Learn

  • The three types of encapsulation: DOM, style, and behavior
  • How styles are scoped within the shadow tree
  • How inherited styles cross the boundary
  • The limits of encapsulation

Why It Matters

Encapsulation is the core value of Shadow DOM. Without it, components leak styles and internals, causing conflicts and maintenance issues.

flowchart LR
  A[Encapsulation] --> B[DOM Encapsulation]
  A --> C[Style Encapsulation]
  A --> D[Behavior Encapsulation]
  B --> E[Hidden internals]
  C --> F[Scoped CSS]
  D --> G[Retargeted events]

DOM Encapsulation

DOM inside the shadow tree is not accessible via document.querySelector from outside.

const host = document.querySelector('#encap-demo');
const shadow = host.attachShadow({ mode: 'open' });
shadow.innerHTML = '<p id="secret">Hidden paragraph</p>';

// These do NOT find the shadow paragraph
console.log('querySelector:', document.querySelector('#secret')); // null

// Must go through the shadow root
console.log('shadowRoot query:', host.shadowRoot.querySelector('#secret')); // Found

Style Encapsulation

Styles defined inside the shadow tree do not leak out, and external styles do not leak in.

class EncapsulatedWidget extends HTMLElement {
    constructor() {
        super();
        this.attachShadow({ mode: 'open' });
        this.shadowRoot.innerHTML = `
            <style>
                .widget { background: #3498db; color: white; padding: 16px; }
                .widget h2 { font-size: 1.5em; margin: 0; }
            </style>
            <div class="widget">
                <h2>Encapsulated Widget</h2>
                <p>These styles cannot be overridden by external CSS.</p>
            </div>
        `;
    }
}
customElements.define('encap-widget', EncapsulatedWidget);

// External CSS does NOT affect the widget:
// .widget { background: red !important; } /* No effect */

Inheritable Properties

Some CSS properties inherit into the shadow tree.

class InheritDemo extends HTMLElement {
    constructor() {
        super();
        this.attachShadow({ mode: 'open' });
        this.shadowRoot.innerHTML = `
            <style>
                :host { all: initial; /* Prevent inheritance */ }
                /* Or explicitly set */
                p { color: initial; font-family: initial; }
            </style>
            <p>This text resets inherited styles</p>
        `;
    }
}
customElements.define('inherit-demo', InheritDemo);

Common Mistakes

  1. Assuming ALL styles are blocked (inheritable properties cross the boundary)
  2. Using !important in host page expecting to override shadow styles (it will not)
  3. Expecting querySelector to find shadow elements

Practice Questions

  1. Can external CSS override a shadow tree's styles? No. Styles are fully encapsulated.
  2. What types of encapsulation does Shadow DOM provide? DOM, style, and event encapsulation.

Mini Project

Create a page that demonstrates all three types of encapsulation with interactive controls. Show a widget with Shadow DOM and one without. Toggle external CSS and observe the difference.

What's Next

Lesson 6: Styling Shadow DOM

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro