The HTML part attribute
The HTML part attribute exposes an element inside a shadow DOM so the host page can style it with the CSS ::part() pseudo-element. It is a global attribute used when authoring web components, giving consumers controlled styling hooks across the shadow boundary.
Overview
Styles do not normally cross the shadow boundary, which protects a web component's internals but also stops consumers from theming them. The part attribute opens specific, named holes in that wall: mark an internal element with part="label" and the outside page can target it with my-component::part(label) { … }.
You can give an element several space-separated part names, and to forward parts from a nested component you use exportparts.
Values
| Value |
|---|
| One or more space-separated part names. |
Example
<!-- Inside the component's shadow DOM -->
<span part="title">Card title</span>
<!-- In the host page's CSS -->
my-card::part(title) { color: navy; }
Best practices
- Treat each
partname like a class rather than an id. Several elements can share a name, and::part(tab selected)matches an element that carries both names, in either order, but not one with a single name. - Follow
::part(label)with a user-action pseudo-class such as:hover, or with::before, but not with a structural one such as:first-child. That selector is invalid, so the whole rule is dropped, along with the other selectors in its list. - Expect a page's
::part()rule to beat the component's own normal declarations for that element. Inside the component, mark a declaration!importantwhen it must hold, since inner important rules win over outer ones. - Change part names at run time through the
partproperty, a token list withadd(),remove()andtoggle(). Toggling a name such asselectedrestyles the element through matching::part()rules. - Closing the shadow root doesn't hide its parts. A component attached with
mode: 'closed'still answers the page's::part()rules in Chrome and Firefox, so leave the attribute off elements you don't want restyled.
Frequently asked questions
What does the part attribute do?
How do I style inside a web component from outside?
::part() pseudo-element.






















