--- url: "https://web.dev/articles/shadowdom-v1" titulek: "Shadow DOM v1 - Self-Contained Web Components | Articles | web.dev" h1: "Shadow DOM v1 - Self-Contained Web Components Stay organized with collections Save and categorize content based on your preferences." popis: "Shadow DOM allows web developers to create compartmentalized DOM and CSS for web components" jazyk: "en" kanonicka: "https://web.dev/articles/shadowdom-v1" drobecky: ["Articles", "Shadow DOM v1 - Self-Contained Web Components"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:27:26" znaku: 34741 slov: 5098 kodovani: "iso-8859-2" sha256: "bb8c6a84589fdff72ff1676113b9c1ba59677c11fd614e84a63653c3c99c0e78" --- Shadow DOM v1 - Self-Contained Web Components Stay organized with collections Save and categorize content based on your preferences. Shadow DOM allows web developers to create compartmentalized DOM and CSS for web components Eric Bidelman Summary Shadow DOM removes the brittleness of building web apps. The brittleness comes from the global nature of HTML, CSS, and JS. Over the years we've invented an exorbitant [number](http://getbem.com/introduction/) [of](https://github.com/css-modules/css-modules) [tools](https://www.smashingmagazine.com/2011/12/an-introduction-to-object-oriented-css-oocss/) to circumvent the issues. For example, when you use a new HTML id/class, there's no telling if it will conflict with an existing name used by the page. [Subtle bugs](http://www.2ality.com/2012/08/ids-are-global.html) creep up, CSS specificity becomes a huge issue (`!important` all the things!), style selectors grow out of control, and [performance can suffer](https://developer.chrome.com/blog/css-containment). The list goes on. **Shadow DOM fixes CSS and DOM**. It introduces **scoped styles** to the web platform. Without tools or naming conventions, you can **bundle CSS with markup**, hide implementation details, and **author self-contained components** in vanilla JavaScript. Introduction Shadow DOM is one of the three Web Component standards: [HTML Templates](https://www.html5rocks.com/en/tutorials/webcomponents/template/), [Shadow DOM](https://dom.spec.whatwg.org/#shadow-trees) and [Custom elements](/articles/custom-elements-v1). [HTML Imports](https://www.html5rocks.com/en/tutorials/webcomponents/imports/) used to be part of the list but now are considered [deprecated](https://developer.chrome.com/blog/chrome-70-deps-rems#deprecate-html-imports). You don't have to author web components that use shadow DOM. But when you do, you take advantage of its benefits (CSS scoping, DOM encapsulation, composition) and build reusable [custom elements](/articles/custom-elements-v1), which are resilient, highly configurable, and extremely reusable. If custom elements are the way to create a new HTML (with a JS API), shadow DOM is the way you provide its HTML and CSS. The two APIs combine to make a component with self-contained HTML, CSS, and JavaScript. Shadow DOM is designed as a tool for building component-based apps. Therefore, it brings solutions for common problems in web development: **Isolated DOM**: A component's DOM is self-contained (e.g. `document.querySelector()` won't return nodes in the component's shadow DOM). **Scoped CSS**: CSS defined inside shadow DOM is scoped to it. Style rules don't leak out and page styles don't bleed in. **Composition**: Design a declarative, markup-based API for your component. **Simplifies CSS** - Scoped DOM means you can use simple CSS selectors, more generic id/class names, and not worry about naming conflicts. **Productivity** - Think of apps in chunks of DOM rather than one large (global) page. `fancy-tabs` demo Throughout this article, I'll be referring to a demo component (``) and referencing code snippets from it. If your browser supports the APIs, you should see a live demo of it just below. Otherwise, check out the [full source on Github](https://gist.github.com/ebidel/2d2bb0cdec3f2a16cf519dbaa791ce1b). [View source on Github](https://gist.github.com/ebidel/2d2bb0cdec3f2a16cf519dbaa791ce1b) What is shadow DOM? Background on DOM HTML powers the web because it's easy to work with. By declaring a few tags, you can author a page in seconds that has both presentation and structure. However, by itself HTML isn't all that useful. It's easy for humans to understand a text- based language, but machines need something more. Enter the Document Object Model, or DOM. When the browser loads a web page it does a bunch of interesting stuff. One of the things it does is transform the author's HTML into a live document. Basically, to understand the page's structure, the browser parses HTML (static strings of text) into a data model (objects/nodes). The browser preserves the HTML's hierarchy by creating a tree of these nodes: the DOM. The cool thing about DOM is that it's a live representation of your page. Unlike the static HTML we author, the browser-produced nodes contain properties, methods, and best of all… can be manipulated by programs! That's why we're able to create DOM elements directly using JavaScript: `const header = document.createElement('header'); const h1 = document.createElement('h1'); h1.textContent = 'Hello DOM'; header.appendChild(h1); document.body.appendChild(header);` produces the following HTML markup: `

Hello DOM

` All that is well and good. Then [what the heck is *shadow DOM*](https://glazkov.com/2011/01/14/what-the-heck-is-shadow-dom/)? DOM… in the shadows Shadow DOM is just normal DOM with two differences: 1) how it's created/used and 2) how it behaves in relation to the rest of the page. Normally, you create DOM nodes and append them as children of another element. With shadow DOM, you create a scoped DOM tree that's attached to the element, but separate from its actual children. This scoped subtree is called a **shadow tree**. The element it's attached to is its **shadow host**. Anything you add in the shadows becomes local to the hosting element, including `
...
...
`; } ... });` There are a couple of interesting things going on here. The first is that the custom element **creates its own shadow DOM** when an instance of `` is created. That's done in the `constructor()`. Secondly, because we're creating a shadow root, the CSS rules inside the ` Button ` **Flattened DOM tree** The result of the browser distributing the user's light DOM into your shadow DOM, rendering the final product. The flattened tree is what you ultimately see in the DevTools and what's rendered on the page. ` #shadow-root Settings ` The element Shadow DOM composes different DOM trees together using the `` element. **Slots are placeholders inside your component that users *can* fill with their own markup**. By defining one or more slots, you invite outside markup to render in your component's shadow DOM. Essentially, you're saying *"Render the user's markup over here"*. Elements are allowed to "cross" the shadow DOM boundary when a `` invites them in. These elements are called **distributed nodes**. Conceptually, distributed nodes can seem a bit bizarre. Slots don't physically move DOM; they render it at another location inside the shadow DOM. A component can define zero or more slots in its shadow DOM. Slots can be empty or provide fallback content. If the user doesn't provide light DOM content, the slot renders its fallback content. ` fallback content

Title

Description text
` You can also create **named slots**. Named slots are specific holes in your shadow DOM that users reference by name. **Example** - the slots in ``'s shadow DOM: `#shadow-root
` Component users declare `` like so: `
content panel 1
content panel 2
content panel 3

Title

content panel 1

Title 2

content panel 2

Title 3

content panel 3
` And if you're wondering, the flattened tree looks something like this: ` #shadow-root
content panel 1
content panel 2
content panel 3
` Notice our component is able to handle different configurations, but the flattened DOM tree remains the same. We can also switch from `