Shadow DOM — technology for creating an isolated DOM subtree inside an element. CSS from the outer document does not bleed in (except inheritable properties), and outside JS querySelector cannot see the shadow tree. Foundation of the Web Components standard. Used by: YouTube Player, Google Docs, CodePen embeds, Shopify Polaris.
Below: details, example, related terms, FAQ.
Free online tool — HTTP header checker: instant results, no signup.
class MyCard extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({ mode: 'open' });
shadow.innerHTML = `
<style>
p { color: red; } /* scoped */
</style>
<p><slot>Default</slot></p>
`;
}
}
customElements.define('my-card', MyCard);The Shadow DOM is a web standard that allows developers to create a separate, encapsulated DOM tree for a web component. This encapsulation ensures that the styles and scripts defined for the outer document do not interfere with the styles and scripts of the component itself. The Shadow DOM is a crucial part of the Web Components standard, which aims to enhance reusability and maintainability of web applications.
When creating a Shadow DOM, developers typically use the attachShadow() method, which is called on a standard DOM element. This method takes a configuration object, allowing for mode specification:
shadowRoot property of the host element.Here's a simple example:
const host = document.querySelector('#my-element');
const shadow = host.attachShadow({ mode: 'open' });
shadow.innerHTML = 'Hello from Shadow DOM!
';In this example, a new shadow root is created for the element with the ID my-element, and a paragraph is added to it. This content will not be affected by any styles in the main document.
The Shadow DOM provides several advantages that enhance web development practices. One of the primary benefits is style encapsulation. CSS styles defined in the main document do not affect the inner workings of a component styled with its own Shadow DOM. This isolation allows developers to create reusable components without worrying about style conflicts.
Additionally, Shadow DOM improves code maintainability. By encapsulating functionality and styling within a component, developers can create a clear separation of concerns. Each component can be developed, tested, and maintained independently, which is particularly useful in large applications.
Another significant advantage is the ability to create complex components without polluting the global namespace. This is achieved through the encapsulation provided by Shadow DOM, which prevents outside scripts from accessing or manipulating the internal structure of the component. As a result, components can be more robust and secure.
Furthermore, Shadow DOM enhances performance. By reducing the number of global styles and scripts that need to be loaded and processed, it can lead to faster rendering times and improved overall performance of the web application.
Shadow DOM is widely utilized in various modern web applications to create reusable components that function independently of the surrounding document. Here are some practical use cases:
<my-modal> element can be designed to manage its own styles and scripts using Shadow DOM.To implement a Shadow DOM in a custom element, you can follow these steps:
class MyElement extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({ mode: 'open' });
shadow.innerHTML = 'This is inside Shadow DOM!
';
}
}
customElements.define('my-element', MyElement);In this example, MyElement encapsulates its styles and structure, making it reusable and isolated from the external document.
Shadow: same JS context, same origin, lightweight. iframe: full browsing context, heavyweight, cross-origin isolation. Shadow for widgets, iframe for third-party untrusted content.
Usually no (React virtual DOM is enough). But a React web components wrapper might. Material UI v6 optionally.
::part() for exposed parts, :host for host element, CSS custom properties inherit. Tailwind via ::part — possible.
Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.