HTML part Attribute

Beginner
6 min read
Updated: Nov 2026
3 examples
Live preview

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().

HTML Syntax

syntax.html
<element part ="part-name [part-name…]">

Space-separated list of part names. Style from outside with host::part(part-name).

Attribute Values

ValueDescriptionTry it
nameExpose one CSS part named nameTry it
a bMultiple part names on the same elementTry it
with exportpartsNested parts forwarded to the outer treeTry it

Live Preview

A themed button component. Toggle an external ::part(label) style.

The label inside the shadow root has part="label".

Result
Save

external ::part off

HTML Examples

Example 1 — Expose and Style a Part

Mark a shadow node with part, then style it from the page.

HTML
<!-- In shadow root: -->
<span part="label">Save</span>

<!-- Outside: -->
<style>
  fancy-btn::part(label) { color: crimson; font-weight: 700; }
</style>
<fancy-btn></fancy-btn>
Try It Yourself

How It Works

  1. Add part="label" inside the shadow tree.
  2. Select with ::part(label) on the host.
  3. Only exported parts accept that styling.
  4. Internal classes stay private to the component.

Example 2 — Multiple Part Names

One element can advertise several part names.

HTML
<button part="control primary">Go</button>

<style>
  my-widget::part(control) { border-radius: 999px; }
  my-widget::part(primary) { background: navy; color: #fff; }
</style>
Try It Yourself

How It Works

  1. Space-separated names act like multiple exports.
  2. Each ::part() can target one name.
  3. Useful for themes that mix shared and specific hooks.
  4. Keep the list small — parts are a public API.

Example 3 — Theme from the Outside

Consumers restyle a component without piercing encapsulation.

HTML
progress-ring::part(track) { stroke: #e2e8f0; }
progress-ring::part(bar) { stroke: #2563eb; }
Try It Yourself

How It Works

  1. Component authors export only intended surfaces.
  2. App CSS owns the theme via ::part.
  3. Nested hosts need exportparts to bubble parts up.
  4. 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.

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.

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

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.
On elements inside the shadow root. Outside the shadow tree, part has no effect for ::part().
From outside: my-el::part(name) { ... }. The name must match the part attribute value.
Yes. Use a space-separated list, like part="label icon", then style with ::part(label) or ::part(icon).
class inside shadow is encapsulated. part intentionally exports a stable styling hook across the shadow boundary.
Use exportparts on the inner host to forward nested parts to the outer tree.

Next: pattern

Validate text input with a regular expression pattern.

pattern tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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