Home HTML Attributes part HTML part Attribute Definition
Definition and Usage The part global attribute exposes an element inside a shadow tree as a named CSS shadow part . Page authors can then style it with the ::part() pseudo-element on the host — without opening the whole shadow tree to arbitrary CSS.
Use stable, intentional part names as a public styling API for your component. For nested custom elements, forward parts with exportparts .
Encapsulated by default. Only nodes you mark with part (and optionally re-export) are styleable via ::part().
part is for shadow trees. On light-DOM-only pages, part does nothing useful for styling. Put it on nodes inside attachShadow().
Syntax
HTML Syntax <element part ="part-name [part-name …]">
Space-separated list of part names. Style from outside with host::part(part-name).
Values
Attribute Values Value Description Try it nameExpose one CSS part named name Try ita bMultiple part names on the same element Try itwith exportparts Nested parts forwarded to the outer tree Try it
Try it
Live Preview A themed button component. Toggle an external ::part(label) style.
Code
HTML Examples 1. Basic ::part 2. Multiple parts 3. Theme a component Example 1 — Expose and Style a Part Mark a shadow node with part, then style it from the page.
<!-- In shadow root: -->
<span part="label">Save</span>
<!-- Outside: -->
<style>
fancy-btn::part(label) { color: crimson; font-weight: 700; }
</style>
<fancy-btn></fancy-btn> Open Try It Yourself for a live ::part demo.
How It Works Add part="label" inside the shadow tree.Select with ::part(label) on the host.Only exported parts accept that styling.Internal classes stay private to the component.Example 2 — Multiple Part Names One element can advertise several part names.
<button part="control primary">Go</button>
<style>
my-widget::part(control) { border-radius: 999px; }
my-widget::part(primary) { background: navy; color: #fff; }
</style> Multi-part demo is in Try It Yourself.
How It Works Space-separated names act like multiple exports.Each ::part() can target one name.Useful for themes that mix shared and specific hooks.Keep the list small — parts are a public API.Example 3 — Theme from the Outside Consumers restyle a component without piercing encapsulation.
progress-ring::part(track) { stroke: #e2e8f0; }
progress-ring::part(bar) { stroke: #2563eb; } Themed component demo is in Try It Yourself.
How It Works Component authors export only intended surfaces.App CSS owns the theme via ::part.Nested hosts need exportparts to bubble parts up.Document your parts like any public API.Edge Cases & Pitfalls Parts are a public contract — rename carefully.
scope Must be in a shadow tree ::part only reaches elements with part inside a shadow root.
nested Nested shadows need exportparts Inner parts are not visible to the outer page unless forwarded.
api Treat names as stable API Renaming a part is a breaking change for consumers.
css Limited CSS reach ::part styles the part element; complex descendant piercing still is not allowed.
Compatibility
Browser Support part is an HTML attribute for browsers. It is not a Node.js, Deno, or Bun API.
✓ Widely available
part CSS shadow parts (::part) are supported in modern Chromium, Firefox, and Safari.
94% Global support (approx.)
Google Chrome 73+
Full support
Mozilla Firefox 72+
Full support
Apple Safari 13.1+
Full support
Microsoft Edge 79+
Full support
Opera 60+
Full support
Samsung Internet 11+
Full support
Internet Explorer No support
Not supported part Modern browsers
Bottom line: Safe for modern Web Components. Pair with exportparts for nested trees.
Remember
Key Takeaways Role: export shadow nodes as CSS parts.
Style: use host::part(name) from outside.
Multiple: space-separated part names are allowed.
Nested: forward with
exportparts.
One line: part exposes shadow internals for ::part() styling without breaking encapsulation for everything else.
Frequently Asked Questions What does the HTML part attribute do? It marks an element inside a shadow tree as a named CSS part so page authors can style it with the ::part() pseudo-element on the host.
Where do I put part? On elements inside the shadow root. Outside the shadow tree, part has no effect for ::part().
How do I style a part? From outside: my-el::part(name) { ... }. The name must match the part attribute value.
Can one element have multiple parts? Yes. Use a space-separated list, like part="label icon", then style with ::part(label) or ::part(icon).
How is part different from class? class inside shadow is encapsulated. part intentionally exports a stable styling hook across the shadow boundary.
What about nested shadow trees? Use exportparts on the inner host to forward nested parts to the outer tree.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion