---
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