Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-elements

The ::part() pseudo-element

Reference for the CSS ::part() pseudo-element: let pages style named parts inside a web component's shadow tree through the part attribute, with limits.

The ::part() pseudo-element lets a page style specific elements inside a web component's shadow tree. The component author marks the elements that may be styled with the part attribute; the page then selects them from outside with host::part(name).

This gives components a deliberate styling API: the author decides which pieces are open for styling, and everything else stays private.

CSS
fancy-button::part(label) {
  color: crimson;
}

Try it

Styling exposed parts

Syntax

CSS
host-selector::part(<name>) { }
host-selector::part(<name> <name2>) { }
host-selector:hover::part(<name>) { }
Property Value
Applies to Elements with a matching part attribute inside a shadow tree
Inherited Follows the usual inheritance inside the shadow tree
Animatable Properties follow their own rules

Values

Syntax Description
::part(name) Elements whose part attribute contains name.
::part(a b) Elements whose part contains both names.
part="a b" In the component: gives one element several part names.
exportparts="inner: outer" Re-exports parts of a nested component under new names.

Usage notes

  • No descendant selectors. x-card::part(title) span is not valid. You can only style the part itself, not elements inside it, unless they are parts too.
  • State pseudo-classes are allowed. ::part(label):hover and :focus work. Structural pseudo-classes like :first-child do not.
  • Beats shadow styles. For the same property, a page rule on ::part() wins over the component's own internal rule unless that one uses !important.
  • Nested components must use exportparts to pass inner parts upward.
  • Custom properties are the other tool. They inherit into shadow trees, so they suit theme values such as colors; use ::part() for properties without a variable.
  • Document the names. Part names are part of a component's public API; changing them breaks users' styles.
  • Closed shadow roots. The part attribute works with both open and closed roots, since it is declared on elements.

Browser support

Supported in all current browsers.