CSS Portal

:host() CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

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

<user-card class="active">Active User</user-card>
<user-card>Inactive User</user-card>

<script>
class UserCard extends HTMLElement {
constructor() {
super();
// Attach the shadow DOM
const shadow = this.attachShadow({ mode: 'open' });

// Create a style element
const style = document.createElement('style');
style.textContent = `
:host {
display: block;
padding: 15px;
border: 1px solid gray;
}
:host(.active) {
background: #d4edda;
border-color: #28a745;
}
`;

// Create some content
const wrapper = document.createElement('span');
wrapper.textContent = this.textContent;

// Attach everything to the shadow root
shadow.appendChild(style);
shadow.appendChild(wrapper);
}
}

// Define the new element
customElements.define('user-card', UserCard);
</script>
/* 1. Default styles for the host element */
:host {
display: block;
padding: 1rem;
margin: 10px;
border: 2px solid #ccc;
font-family: sans-serif;
border-radius: 8px;
transition: all 0.3s ease;
}

/* 2. Styles applied ONLY if the host has the .active class */
:host(.active) {
border-color: #2ecc71;
background-color: #e8f8f1;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

/* 3. Styles applied ONLY if the host has a specific attribute */
:host([disabled]) {
opacity: 0.5;
pointer-events: none;
}

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
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!