::part() CSS Pseudo Element
Description
The ::part() pseudo-class in CSS is a specialized selector that allows developers to style elements inside a shadow DOM from the outside. Shadow DOM encapsulation typically prevents external styles from affecting the internal elements of a web component, ensuring a self-contained component style. However, there are situations where controlled styling of specific internal parts is desirable. The ::part() pseudo-class provides a standardized mechanism to expose and style those internal parts without breaking the encapsulation.
To use ::part(), the shadow DOM component must define a part attribute on elements that should be stylable externally. For example, a custom button component might define its inner <code>span</code> as <span part="label">Button Text</span>. Once a part is defined, the outer stylesheet can target it using the ::part() pseudo-class, like so:
custom-button::part(label) {
color: white;
background-color: blue;
padding: 8px 16px;
border-radius: 4px;
}
This allows the external CSS to style the label part of the <code>custom-button</code> component without directly accessing the shadow DOM. You can even combine it with other pseudo-classes or selectors for more advanced effects:
custom-button::part(label):hover {
background-color: darkblue;
}
The ::part() pseudo-class is particularly useful when creating reusable components that need flexible theming or custom styling from outside their encapsulated shadow DOM. It's different from the ::slotted() pseudo-class, which allows styling of nodes projected into slots inside shadow DOM but cannot style arbitrary internal elements.
Additionally, ::part() can be combined with multiple parts if a shadow DOM element exposes more than one part:
custom-card::part(header),
custom-card::part(footer) {
font-weight: bold;
}
This demonstrates that ::part() allows granular, controlled styling while maintaining the encapsulation benefits of the shadow DOM, supporting maintainable and themeable web components.
Syntax
custom-element::part(foo) {
/* Styles to apply to the `foo` part */
}
Example
Browser Support
The following information will show you the current browser support for the CSS ::part() pseudo element. Hover over a browser icon to see the version that first introduced support for this CSS psuedo element.
This psuedo element is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 31st December 2025
