Shadow DOM
A browser standard that encapsulates a component's internal DOM tree and CSS styles away from the host document to prevent style leakage
Table of Contents
Structure Diagram
What is Shadow DOM?
A browser standard that encapsulates a component's internal DOM tree and CSS styles away from the host document to prevent style leakage. It is a core concept in Rendering & Frontend within Web Dev.
Core Principles
Shadow DOM is a Web Components browser standard that attaches a hidden, encapsulated DOM subtree (Shadow Tree) to an element (Shadow Host), isolating it from the main document (Light DOM).
CSS rules declared inside a Shadow Root never leak out, and host-page global styles cannot accidentally break the component's internal layout. Browsers themselves use Shadow DOM under the hood to render built-in controls like <video> players and <input type="range"> sliders.
Practical Takeaways
- Ideal for third-party embeds and extensions: Chat widgets, feedback modals, and browser extension overlays injected into unknown host pages stay visually intact inside a Shadow Root.
- Expose controlled theming hooks: Because global selectors cannot pierce the shadow boundary, expose CSS Custom Properties (which inherit across the boundary) and
::part()selectors for intentional styling overrides. - Use Declarative Shadow DOM for SSR:
<template shadowrootmode="open">allows server-rendered HTML to establish shadow boundaries before JavaScript executes, eliminating Flash of Unstyled Content.
Notable Examples
- Embeddable SaaS widgets & Chrome extensions: Guarantee consistent UI across thousands of arbitrary customer stylesheets.
- Framework-agnostic design systems: Built with Lit or Stencil so React, Vue, and vanilla apps share identical UI primitives.