:host() CSS Pseudo Class
Description
The :host() pseudo-class in CSS is a powerful selector used within the context of Web Components to target the shadow host — the element in the main DOM that contains a shadow DOM — from inside its shadow tree. Essentially, it allows developers to apply styles to the host element itself while encapsulating the internal structure of the shadow DOM. This is particularly useful because styles within a shadow DOM are scoped and normally do not affect elements outside of it.
A primary use of :host() is to conditionally style a shadow host based on its attributes or state. For instance, if a custom element <my-widget> has a highlighted attribute, you can style the host when this attribute is present:
:host([highlighted]) {
background-color: yellow;
border-radius: 8px;
}
This will only apply styles to <my-widget> when it has the highlighted attribute, while keeping the styles inside the shadow DOM encapsulated. You can also combine :host() with pseudo-classes such as :hover to react to user interactions:
:host(:hover) {
transform: scale(1.05);
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
Another useful feature is the ability to pass a selector to :host(), allowing you to apply styles only when the host matches a specific condition. For example:
:host(.dark-mode) {
background-color: #222;
color: #fff;
}
This targets the host element only if it has the dark-mode class, keeping the internal structure of the shadow DOM unaffected. It is also worth noting that :host() is often combined with ::part() for more granular control, styling specific parts of a component while still being able to style the host itself.
Syntax
:host(<compound-selector>) {
/* ... */
}
Example
Browser Support
The following information will show you the current browser support for the CSS :host() pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.
This psuedo class is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 1st January 2026
