Favorites

:host-context() CSS Pseudo Class

:host-context()
CSS Pseudo ClassThis pseudo class is supported in some modern browsers, but not all.
Updated 22nd November 2026

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.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

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

<div class="dark-theme">
<themed-box>Inside a .dark-theme container</themed-box>
</div>
<themed-box>Outside the container</themed-box>

<script>
customElements.define('themed-box', class extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' }).innerHTML = `
<style>
:host { display: block; margin: 10px 0; padding: 16px; border-radius: 8px; background: #eef; color: #223; font-family: system-ui, sans-serif; width: 300px; }
:host-context(.dark-theme) { background: #222; color: #eee; }
</style>
<slot></slot>`;
}
});
</script>
/* The :host-context() rule is inside the component's shadow root (see the script).
The first box is dark because one of its ancestors has the class .dark-theme. */
body {
background: #fafafa;
}
Open in editor

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.

This pseudo class is supported in some modern browsers, but not all.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee