Favorites

HTML exportparts Global Attribute

exportparts
HTML Global AttributeThis global attribute is supported by all modern browsers.
Updated 22nd November 2026

The exportparts attribute passes the shadow parts of a nested web component up to its parent component, optionally renamed, so they can be styled with ::part().

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

Description

The exportparts attribute is a global HTML attribute used in conjunction with Web Components and the Shadow DOM. It allows a custom element to expose selected internal shadow parts so they can be styled from outside the component using the ::part() pseudo-element.

By default, styles inside a Shadow DOM are encapsulated, meaning external stylesheets cannot target internal elements. The exportparts attribute provides a controlled way to expose specific parts of a component’s internal structure without breaking encapsulation entirely.

This makes it especially useful when building reusable, themeable components that still maintain a clean separation between internal implementation and external styling.

How exportparts Works

Parts declared with the part attribute can already be styled one level up: from the document for a component used in the page, with my-button::part(button). exportparts is needed when components are nested: it is placed on the inner component, inside the outer component's shadow tree, and forwards (and optionally renames) the inner parts so that they become parts of the outer component and can be styled from the document.

Example: Simple Exported Part

Inner component (shadow DOM of <my-button>)
<button part="button">Click me</button>
Outer component (shadow DOM of <my-toolbar>)
<my-button exportparts="button"></my-button>
External CSS
my-toolbar::part(button) {
  background-color: #4caf50;
  color: white;
  border-radius: 6px;
}

In this case, the button part of the inner component becomes a part of <my-toolbar> and can be styled from the document.

Renaming Exported Parts

You can rename parts when exporting them. This is useful for maintaining consistent naming conventions or avoiding conflicts.

<my-button exportparts="button: primary-button"></my-button>

Now it can be styled like this:

my-toolbar::part(primary-button) {
  padding: 10px 16px;
}

Exporting Multiple Parts

Multiple parts can be exported by separating them with commas:

<custom-card exportparts="header, body, footer"></custom-card>

Or with renaming:

<custom-card exportparts="header: card-header, footer: card-footer"></custom-card>

Why Use exportparts?

The exportparts attribute helps solve common styling challenges when working with Shadow DOM components:

  • Allows safe and intentional styling from outside a component
  • Preserves encapsulation while enabling customization
  • Prevents direct DOM access or reliance on internal structure
  • Encourages reusable and themeable UI components

This is particularly useful for design systems, UI libraries, and reusable widgets.

Relationship to part and ::part()

Feature Purpose
part Defines a stylable element inside a Shadow DOM
exportparts Exposes internal parts to the outside
::part() Selects and styles exported parts

Together, these features create a controlled styling API for web components.

Notes and Best Practices

  • Only elements with a part attribute can be exported.
  • Exported parts do not expose the full DOM - only styling access.
  • Avoid over-exporting parts to keep components maintainable.
  • Use consistent naming to make styling intuitive for consumers.

Syntax

<custom-element exportparts="internal-name: external-name"></custom-element>

Values

  • internal-nameThe name defined via the part attribute inside the child component's shadow DOM.
  • external-nameThe name that will be visible to the outside world (the document or the grandparent).

Example

<!DOCTYPE html>
<html>
<head>
<title>exportparts Example</title>
<style>
/* 3. THE GRANDPARENT (Main Page) styles the nested part */
/* Note: 'super-button' is the alias created by the Parent */
x-parent::part(super-button) {
background-color: #ff5722;
color: white;
border-radius: 8px;
padding: 10px 20px;
border: none;
cursor: pointer;
}
</style>
</head>
<body>

<h1>Web Components exportparts</h1>

<x-parent></x-parent>

<script>
// --- CHILD COMPONENT ---
customElements.define('x-child', class extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' }).innerHTML = `
<div>
<button part="action-button">Child Button</button>
</div>
`;
}
});

// --- PARENT COMPONENT ---
customElements.define('x-parent', class extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' }).innerHTML = `
<div style="border: 2px dashed #ccc; padding: 20px;">
<h3>Parent Component</h3>
<x-child exportparts="action-button: super-button"></x-child>
</div>
`;
}
});
</script>

</body>
</html>
Open in editor

Browser Support

The following information will show you the current browser support for the HTML exportparts global attribute. Hover over a browser icon to see the version that first introduced support for this HTML global attribute.

This global attribute is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee