:host() CSS Pseudo Class

Description

The :host() pseudo-class in CSS is a special selector used in the context of web component styling. It allows you to target the shadow host element itself, which is the element that hosts a web component. This selector is primarily used within the shadow DOM (Document Object Model) to style the encapsulated content of a web component.

Here's a brief breakdown of how :host() works:


  1. Targeting the Shadow Host: When you use :host(), you are selecting the shadow host element itself, which is the top-level element of the web component.

  2. Scoped Styling: It's used to apply CSS styles to the shadow DOM of a web component, creating a level of encapsulation that prevents the styles from leaking out to the rest of the document and vice versa. This allows you to create reusable and isolated components.

  3. Customization: You can define styles for the shadow host element using :host(). This is particularly useful when you want to customize the appearance of a specific instance of a web component.


Here's a simple example:
/* Styling the shadow host element of a web component */
:host {
display: block;
background-color: #f0f0f0;
border: 1px solid #ccc;
padding: 10px;
}

In this example, the :host selector is used to style the shadow host element by setting its display, background color, border, and padding properties.

Overall, :host() is a key feature in the world of web components and shadow DOM styling, enabling developers to create encapsulated and customizable UI components that don't interfere with the styling of the main document.

Syntax

:host(<compound-selector>) {
  /* ... */
}

Example

Sorry, no example yet!

Browser Support

The following table will show you the current browser support for the CSS :host() pseudo class.

Desktop
Edge Chrome Firefox Opera Safari
7954634110
Tablets / Mobile
Chrome Firefox Opera Safari Samsung Webview
54634110654

Last updated by CSSPortal on: 1st October 2023