Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

::part() — styling exposed Shadow DOM elements

The ::part() pseudo-element lets outside styles target shadow-tree elements explicitly exposed by a component.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

host-selector::part(<ident>+) { /* declarations */ }

::part() selects shadow-tree elements exposed through the part attribute. The preceding selector targets the host, not the internal element. For example, #demo::part(label) selects the exposed label part.

Arguments are names, not CSS selectors. part="tab active" exposes two names; ::part(tab active) requires both on the same element. Order does not matter; multiple elements can share a name.

This is not unrestricted encapsulation piercing. ::part(label) span does not expose descendants, and chaining ::part(button)::part(label) matches nothing. Parts from nested components need forwarding through exportparts, for example exportparts="label: caption".

Examples

<!doctype html>
<meta charset="utf-8">
<title>Shadow part example</title>
<style>
  #demo::part(label) { color: rgb(0, 128, 0); }
</style>
<div id="demo"></div>
<script>
  const host = document.getElementById('demo');
  const root = host.attachShadow({ mode: 'open' });
  const label = document.createElement('span');
  label.setAttribute('part', 'label');
  label.textContent = 'Ready';
  root.append(label);
  console.log(getComputedStyle(label).color);
</script>

Result

rgb(0, 128, 0)

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources