The :host()
CSS pseudo-class function selects the shadow host of the shadow DOM containing the CSS it is used inside (so you can select a custom element from inside its shadow DOM) — but only if the selector given as the function's parameter matches the shadow host. :host()
has no effect when used outside a shadow DOM.
The most obvious use of this is to put a class name only on certain custom element instances, and then include the relevant class selector as the function argument. You can't use this with a descendant selector expression to select only instances of the custom element that are inside a particular ancestor. That's the job of :host-context()
.
Note: While other functional pseudo-classes such as :is()
and :not()
accept a list of selectors as their parameters, :host()
takes a single compound selector as its parameter. In addition, while :is()
and :not()
only take into account the specificity of their argument, the specificity of :host()
is both the specificity of the pseudo-class and the specificity of its argument.