--- url: "https://web.dev/articles/shadowdom-201" titulek: "Shadow DOM 201 | Articles | web.dev" h1: "Shadow DOM 201 Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/shadowdom-201" drobecky: ["Articles", "Shadow DOM 201"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 23:12:41" znaku: 15655 slov: 2299 kodovani: "iso-8859-2" sha256: "62bfa03722082e7469782fd5e7e73aa57727d3769a36dfaadf0478565ef59ee8" --- Shadow DOM 201 Stay organized with collections Save and categorize content based on your preferences. CSS and styling Eric Bidelman This article discusses more of the amazing things you can do with Shadow DOM. It builds on the concepts discussed in [Shadow DOM 101](/articles/shadowdom). If you're looking for an introduction, see that article. Introduction Let's face it. There's nothing sexy about unstyled markup. Lucky for us, [the brilliant folks behind Web Components](https://www.w3.org/TR/2012/WD-components-intro-20120522/#acknowledgements) foresaw this and didn't leave us hanging. The [CSS Scoping Module](https://drafts.csswg.org/css-scoping/) defines many options for styling content in a shadow tree. Style encapsulation One of the core features of Shadow DOM is the [shadow boundary](https://dom.spec.whatwg.org/#shadow-trees). It has a lot of nice properties, but one of the best is that it provides style encapsulation for free. Stated another way: `

Light DOM

` There are two interesting observations about this demo: There are other h3s on this page, but the only one that matches the h3 selector, and therefore styled red, is the one in the ShadowRoot. Again, scoped styles by default. Other styles rules defined on this page that target h3s don't bleed into my content. That's because **selectors don't cross the shadow boundary**. Moral of the story? We have style encapsulation from the outside world. Thanks Shadow DOM! Styling the host element The `:host` allows you to select and style the element hosting a shadow tree: ` ` One gotcha is that rules in the parent page have higher specificity than `:host` rules defined in the element, but lower specificity than a `style` attribute defined on the host element. This allows users to override your styling from the outside. `:host` also only works in the context of a ShadowRoot so you can't use it outside of Shadow DOM. The functional form of `:host()` allows you to target the host element if it matches a ``. **Example** - match only if the element itself has the class `.different` (e.g. ``): `:host(.different) { ... }` Reacting to user states A common use case for `:host` is when you're creating a [Custom Element](/articles/customelements) and want to react to different user states (:hover, :focus, :active, etc.). `` Theming an element The `:host-context()` pseudo class matches the host element if it or any of its ancestors matches ``. A common use of `:host-context()` is for theming an element based on its surrounds. For example, many people do theming by applying a class to `` or ``: ` ` You can `:host-context(.different)` to style `` when it's a descendant of an element with the class `.different`: `:host-context(.different) { color: red; }` This gives you the ability encapsulate style rules in an element's Shadow DOM that uniquely style it, based on its context. Support multiple host types from within one shadow root Another use for `:host` is if you're creating a theming library and want to support styling many types of host elements from within the same Shadow DOM. `:host(x-foo) { /* Applies if the host is a element.*/ } :host(x-foo:host) { /* Same as above. Applies if the host is a element. */ } :host(div) { /* Applies if the host element is a
. */ }` Styling Shadow DOM internals from the outside The `::shadow` pseudo-element and `/deep/` combinator are like having a Vorpal sword of CSS authority. They allow piercing through Shadow DOM's boundary to style elements within shadow trees. The ::shadow pseudo-element If an element has at least one shadow tree, the `::shadow` pseudo-element matches the shadow root itself. It allows you to write selectors that style nodes internal to an element's shadow dom. For example, if an element is hosting a shadow root, you can write `#host::shadow span {}` to style all of the spans within its shadow tree. `
Light DOM
` **Example** (custom elements) - `` has `` children in its Shadow DOM. Each panel hosts its own shadow tree containing `h2` headings. To style those headings from the main page, one could write: `x-tabs::shadow x-panel::shadow h2 { ... }` The /deep/ combinator The `/deep/` combinator is similar to `::shadow`, but more powerful. It completely ignores all shadow boundaries and crosses into any number of shadow trees. Put simply, `/deep/` allows you to drill into an element's guts and target any node. The `/deep/` combinator is particularly useful in the world of Custom Elements where it's common to have multiple levels of Shadow DOM. Prime examples are nesting a bunch of custom elements (each hosting their own shadow tree) or creating an element that inherits from another using [``](/articles/shadowdom-301#shadow_insertion_points). **Example** (custom elements) - select all `` elements that are descendants of ``, anywhere in the tree: `x-tabs /deep/ x-panel { ... }` **Example** - style all elements with the class `.library-theme`, anywhere in a shadow tree: `body /deep/ .library-theme { ... }` Working with querySelector() Just like [`.shadowRoot`](/articles/shadowdom-301#obtaining_a_hosts_shadow_root) opens shadow trees up for DOM traversal, the combinators open shadow trees for selector traversal. Instead of writing a nested chain of madness, you can write a single statement: `// No fun. document.querySelector('x-tabs').shadowRoot .querySelector('x-panel').shadowRoot .querySelector('#foo'); // Fun. document.querySelector('x-tabs::shadow x-panel::shadow #foo');` Styling native elements Native HTML controls are a challenge to style. Many people simply give up and roll their own. However, with `::shadow` and `/deep/`, any element in the web platform that uses Shadow DOM can be styled. Great examples are the `` types and `