CSS Portal

HTML exportparts Global Attribute

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

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

The exportparts attribute is placed on a custom element that uses a Shadow DOM. Inside that component, internal elements must define a part attribute. The exportparts attribute then maps those internal part names to names that can be accessed from outside the component.

Example: Simple Exported Part

Custom element structure (inside Shadow DOM)
<button part="button">Click me</button>
Host element
<my-button exportparts="button"></my-button>
External CSS
my-button::part(button) {
  background-color: #4caf50;
  color: white;
  border-radius: 6px;
}

In this case, the internal button part becomes available for styling from outside the component.

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-button::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>

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
-

Last updated by CSSPortal on: 27th December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!