What is shadow root

Learn what is shadow root with practical examples, diagrams, and best practices. Covers html, google-chrome, dom development techniques with visual explanations.

Understanding the Shadow DOM and Shadow Root

Hero image for What is shadow root

Explore the Shadow DOM, Shadow Root, and Shadow Host concepts, their role in web component encapsulation, and how to inspect them using Chrome DevTools.

The Shadow DOM is a powerful web standard that allows developers to encapsulate the styling and behavior of web components, preventing them from clashing with other parts of the document. At its core, the Shadow DOM introduces a concept called the 'Shadow Tree', which is a separate DOM tree attached to an element in the regular DOM. This article will delve into the specifics of the Shadow Root, its relationship with the Shadow Host, and how it contributes to component isolation.

What is a Shadow Root?

A Shadow Root is the root node of a Shadow Tree. It's essentially a hidden DOM subtree that is attached to a regular DOM element, known as the 'Shadow Host'. The content within a Shadow Root is rendered separately from the main document's DOM, providing strong encapsulation for styles and scripts. This means that CSS rules defined inside the Shadow Root will not leak out to the main document, and CSS rules from the main document will not penetrate into the Shadow Root, unless explicitly designed to do so (e.g., using CSS custom properties or specific ::part selectors).

flowchart TD
    A[Regular DOM Element] --> B{Attach Shadow Root?}
    B -->|Yes| C[Shadow Host]
    C --> D[Shadow Root]
    D --> E[Shadow Tree (Encapsulated DOM)]
    B -->|No| F[Standard DOM Element]
    style A fill:#f9f,stroke:#333,stroke-width:2px
    style C fill:#bbf,stroke:#333,stroke-width:2px
    style D fill:#ccf,stroke:#333,stroke-width:2px
    style E fill:#eef,stroke:#333,stroke-width:2px

Flowchart illustrating the creation of a Shadow Root and Shadow Tree

Shadow Host and Shadow Root Relationship

The element to which a Shadow Root is attached is called the Shadow Host. The Shadow Host is a regular DOM element, but it gains special properties once a Shadow Root is attached. From the perspective of the main document, the Shadow Host appears as a single element, but internally, it renders the content of its Shadow Tree. This relationship is crucial for understanding how web components work: the component's internal structure and styling are hidden within its Shadow Root, while the component itself is represented by its Shadow Host in the main document.

const hostElement = document.createElement('div');
hostElement.textContent = 'I am the Shadow Host.';
document.body.appendChild(hostElement);

const shadowRoot = hostElement.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = `
  <style>
    p { color: blue; }
    ::slotted(span) { color: green; }
  </style>
  <p>Hello from the Shadow DOM!</p>
  <slot></slot>
`;

const slottedContent = document.createElement('span');
slottedContent.textContent = 'This content is slotted.';
hostElement.appendChild(slottedContent);

Creating a Shadow Host and attaching a Shadow Root with slotted content

Inspecting Shadow Roots with Chrome DevTools

Chrome DevTools provides excellent support for inspecting Shadow DOM structures. By default, Shadow Roots might not be immediately visible in the Elements panel. To view them, you need to enable a specific setting. This allows you to dive into the encapsulated content, debug styles, and understand the internal structure of web components.

1. Open DevTools

Right-click on any element on a webpage and select 'Inspect' or press F12 (Windows/Linux) / Cmd + Option + I (macOS).

2. Access Settings

In the DevTools panel, click the three vertical dots (â‹®) in the top-right corner to open the 'Customize and control DevTools' menu, then select 'Settings'.

3. Enable Shadow DOM

In the Settings panel, navigate to the 'Elements' section. Look for the option 'Show user agent shadow DOM' and ensure it is checked. You might also want to check 'Show HTML comments' and 'Show whitespace characters' for a more complete view.

4. Inspect Shadow Root

Close the Settings panel. Now, when you inspect elements that use Shadow DOM (like <video> controls, <input type="range">, or custom web components), you will see a shadow-root entry beneath the host element in the Elements panel. You can expand it to view its internal structure.