:host-context() CSS Pseudo Class
The :host-context() pseudo-class matches the shadow host when one of its ancestors matches a selector. It is deprecated and not supported in Firefox or Safari.
Description
The :host-context() pseudo-class is used inside a shadow root stylesheet to select the shadow host when the host or any of its ancestors matches the compound selector given as its argument. It lets a web component react to where it is placed, for example to a theme class on the <body>.
/* Inside the component's shadow root */
:host-context(.dark-theme) {
background: #222;
color: #eee;
}
Compare it with :host, which always matches the host, and :host(), which only looks at the host element itself. :host-context() looks up the whole chain of ancestors in the light DOM, crossing shadow boundaries.
This pseudo-class is deprecated. It was removed from the CSS Scoping specification because it is costly to implement, and Firefox and Safari never shipped it. It still works in Chromium-based browsers, but new code should not rely on it. Better alternatives are CSS custom properties, which inherit into shadow trees (for example a --card-bg variable set by the theme), or container style queries.
Syntax
:host-context(<compound-selector>) {
/* ... */
}Values
- <compound-selector>A compound selector (no combinators) that is tested against the shadow host and each of its ancestors.
Example
Browser Support
The following information will show you the current browser support for the CSS :host-context() pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS pseudo class.
Desktop





Tablets & Mobile





