completefrontend
Open lab My progress
THE WEB COMPONENTS FIELD GUIDE

Theming components from the outside

Style web components from outside: theme them with CSS custom properties, restyle their parts with ::part(), add dark mode, and see what you can't style.

By the end, you can
  • 01
    Theme with custom propertiesSay which page styles pass into a shadow root, and set a library's custom properties where they take effect.
  • 02
    Style parts with ::part()Style the parts a component names, including parts it passes up from inner components, and know which selectors can't follow a part.
  • 03
    Add light and dark themesSwitch Web Awesome between light and dark with one class, and make your own component follow with color-scheme and light-dark().

How do you style a web component from outside?

Ravi's menu page for Leaf and Kettle works. He built it from Web Awesome's tags in lesson 2.1. He wired it up with JavaScript in the last lesson. Now the owner has notes. "It looks like every other site. Can it be tea green, in our serif font? And can the menu go dark in the evening?"

Ravi opens DevTools. Inside the button's shadow root, he finds an element with the class button. So he writes wa-button .button { background: red; } to test it. Nothing happens. How do you style a tag when your CSS can't see inside it?

You style a web component from outside through the doors its author built. Inherited properties and CSS custom properties pass into its shadow root. And ::part() styles the inner elements the author named as parts. Every other selector stops at the shadow root.

Here are all three, on one button:

A page selector, a custom property, and a partPop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  wa-button .button { background: red; }  :root { --wa-color-brand-50: #00883c; }  wa-button::part(label) { font-style: italic; }</style> <wa-button variant="brand">Add to order</wa-button>
ResultLoading…
  • Lines 1 and 2 load Web Awesome, exactly as in lesson 2.1.
  • Line 5 is Ravi's test. It looks for an element with the class button inside wa-button. It finds nothing, so nothing turns red.
  • Line 6 sets a custom property: a CSS variable you name yourself, with two hyphens in front. Web Awesome's theme uses --wa-color-brand-50 for its main color. Ravi gives it a tea green.
  • Line 7 styles the button's label part with ::part(). The label turns italic.
  • Line 10 is the button. variant="brand" asks for the theme's main color.

The result is a green button with an italic label. Line 5 did nothing. Lines 6 and 7 both worked. This picture shows why:

Three page rules and a component's shadow root. On the left, a window called your page holds three cards: a custom property, --wa-color-brand-50; a part selector, wa-button::part(label); and a page selector, wa-button .button. On the right, a window called shadow root holds two cards, .button and part equals label. An arrow from the custom property crosses into the shadow root to .button. An arrow from the part selector reaches part equals label. The arrow from the page selector stops before the shadow root.your pagecustom property--wa-color-brand-50partwa-button::part(label)page selectorwa-button .buttonstops at the shadow rootshadow root.buttonreads the custom propertypart="label"named by the author
Two kinds of rule get in: custom properties, which pass down into the shadow root, and ::part(), which reaches the parts the author named. A page selector stops at the edge.

A shadow root is a component's private tree, as in lesson 1.1. Page selectors can't look inside it. But two kinds of style still get in, because the author made room for them.

Real-life analogyOrdering from a kitchen you can't walk into

At Leaf and Kettle, customers don't walk into the kitchen. They can't move the pots. But the order form has boxes for milk, sugar, and cup size. And the serving hatch has a sign, so you can ask for your tea there. You get what you want through the ways the kitchen offers.

In real life: The kitchen, where customers can't go
In JavaScript: The component's shadow root
In real life: The boxes on the order form
In JavaScript: Custom properties, like --wa-color-brand-50
In real life: The serving hatch with a sign on it
In JavaScript: A part, like ::part(label)
In real life: Reaching over the counter for a pot
In JavaScript: A page selector, like wa-button .button
The main idea

A component decides how you may style it from outside. Inherited properties and custom properties pass into its shadow root. ::part() reaches the elements it names as parts. Page selectors stop at the shadow root.

The docs list the doors. Lesson 2.1 showed the CSS parts table. Each docs page also has a CSS custom properties table. This lesson uses both.

Do CSS variables pass through the shadow DOM?

Yes. Custom properties pass through, because they're inherited. An inherited property is one that children take from their parent, unless they set it themselves. color and font-family are inherited. border and padding are not.

The CSS Shadow specification explains how this crosses into a shadow root. Selectors see two separate trees: the page's and the shadow root's. But for everything after selectors, inheritance included, CSS joins them into one tree. In that tree, the shadow root's elements sit inside their host. So a value on <body> flows down into a component.

Here is a small version of tea-card, from the kit in lesson 1.4. This time its shadow root holds a name and a note:

tea-card.js: a name and a note in a shadow rootJavaScript
class TeaCard extends HTMLElement {
  constructor() {
    super();
    const shadow = this.attachShadow({ mode: "open" });
    shadow.innerHTML = `
      <style>
        .brew { color: var(--tea-accent, gray); }
      </style>
      <h3 class="name" part="name">Green tea</h3>
      <p class="brew">Brew for 3 minutes</p>`;
  }
}

customElements.define("tea-card", TeaCard);
  • Line 4 gives the card a shadow root.
  • Line 7 colors the note with var(--tea-accent, gray). var() reads a custom property. If --tea-accent isn't set, it uses the fallback after the comma: gray.
  • Line 9 is the name, an <h3> with class="name" and part="name".
  • Line 10 is the note.

The playground puts this card on a page with one rule. Pick a rule and watch the card:

Playground: which page rule reaches inside?
One page rule and a tea cardPop out in the code editor (opens in a new tab)HTML
<style>  body { color: chocolate; }</style> <tea-card></tea-card> <script src="tea-card.js"></script>
ResultLoading…
Try it yourself
It reaches inside. Green tea turns chocolate brown. The note stays gray.

color is inherited: children take it from their parent. The browser passes it from the body to <tea-card>, and on into the shadow root, so the name turns brown. The note doesn't, because line 7 of tea-card.js gives it a color of its own.

Only line 2 changes. The card's class is in the tea-card.js tab. Each choice runs in a fresh page.

Three rules reach inside, and two don't. color, font-family, and --tea-accent are all inherited. .name and tea-card h3 are selectors, and page selectors only look at the page's own tree. Why it matters: when a page style doesn't show up in a component, ask whether it's inherited.

Inheritance passing into a shadow root. Labels on the left say the page, then inside the card. A card, body, has the note color: chocolate beside it. An arrow leads down to a card, tea-card, with the note --tea-accent: green beside it. An arrow leads down into a window called shadow root, which holds two cards: h3 class equals name, and p class equals brew. Under the first: chocolate, inherited. Under the second: green, from var.the page<body>color: chocolate<tea-card>--tea-accent: greeninside the cardshadow root<h3 class="name"><p class="brew">chocolate, inheritedgreen, from var()
For inheritance, CSS treats the shadow root as the inside of <tea-card>. color passes from the body down to the name, and --tea-accent reaches the note, where var() reads it.
Real-life analogyA note on the staff noticeboard

Leaf and Kettle's manager pins a note on the staff noticeboard: "Today's color is green." Every room reads the same board, even the kitchen, which customers never see. Each cook decides where to use the color. A cook who already chose a color for the day keeps it.

In real life: The manager's note on the board
In JavaScript: A custom property on a parent, like tea-card { --tea-accent: green; }
In real life: The kitchen reads the same board
In JavaScript: The shadow root inherits the value
In real life: A cook using the color on the cakes
In JavaScript: var(--tea-accent) inside the component
In real life: A cook who already chose a color
In JavaScript: An element that sets its own value

Web Awesome's theme is custom properties

Web Awesome builds its whole look from custom properties. Its docs call them design tokens: named values for colors, fonts, space, and corners. They all start with --wa-, so they can't clash with other libraries. Set one on :root, and every component reads the new value:

Three theme tokens, set once for the whole pagePop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  :root {    --wa-color-brand-50: #00883c;    --wa-font-family-body: Georgia, serif;    --wa-border-radius-scale: 2;  }</style> <wa-button variant="brand">Add to order</wa-button><wa-button>Brewing guide</wa-button>
ResultLoading…
  • Line 5 picks :root, the <html> element at the top of the page.
  • Line 6 sets the brand color. Web Awesome's colors come in steps, from 05, the darkest, to 95, the lightest. Buttons use step 50.
  • Line 7 sets the font that every component uses.
  • Line 8 makes every corner twice as round. Its default is 1.
  • Lines 12 and 13 are two buttons.

Both buttons now use Georgia, with rounder corners. The first one is green. Three values changed, and every component on the page followed. The full list of tokens is in the theme's file, styles/themes/default.css.

When the component sets its own value

Inheritance only fills gaps. If a component sets a value itself, its own value wins. You saw this in the playground: with color on the body, the note stayed gray. Web Awesome's card works the same way:

An inherited color, and a color on the card itselfPop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  body { color: chocolate; }  wa-card.special { color: chocolate; }</style> <p>Today's teas</p><wa-card>Green tea. Brew for 3 minutes.</wa-card><wa-card class="special">Black tea. Brew for 4 minutes.</wa-card>
ResultLoading…
  • Line 5 colors the page's text chocolate brown.
  • Line 6 colors one card chocolate: the card with the class special.
  • Line 9 is a plain paragraph. Lines 10 and 11 are two cards.

The paragraph is chocolate, and so is the second card. The first card keeps its own dark text. The card gives itself a color, so it never takes the body's. Line 6 works because it styles the card element itself. That element lives in your page, so your selectors can reach it. Web Awesome's docs say the same: style wa-card directly.

Inside its shadow root, a component styles its own element with :host. :host means "the element this shadow root belongs to". When a page rule and a :host rule set the same property, the page's rule wins. The CSS cascade says so: for normal rules, the outer one wins. So a component can set defaults that you can change.

Components set defaults for their own custom properties the same way. Inside its shadow root, the rating's styles set --symbol-color-active, the color of a lit star, on :host. Its default is Web Awesome's yellow, the token --wa-color-yellow-70. So where you set it matters:

The same custom property on :root and on the ratingPop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  :root { --symbol-color-active: crimson; }  wa-rating.green { --symbol-color-active: #00883c; }</style> <wa-rating value="4"></wa-rating><wa-rating class="green" value="4"></wa-rating>
ResultLoading…
  • Line 5 sets --symbol-color-active on :root.
  • Line 6 sets it on ratings with the class green.
  • Lines 9 and 10 are two ratings. Only the second has the class.

The first rating's stars stay yellow. The second's turn green. The value on :root never arrived. The rating would inherit it, but its own :host value fills the gap first. Why it matters: set a component's own custom properties on the component. Their names are short and have no --wa-, like --symbol-color-active or --width. Theme tokens go on :root.

How do you style a web component with ::part()?

Custom properties change only what the author planned for. For anything else, the author can name an element as a part. They give it a part attribute inside the shadow root. You style it from outside with ::part() and its name. Any CSS property works.

tea-card already names one. Line 9 of tea-card.js has part="name":

Style the card's name partPop out in the code editor (opens in a new tab)HTML
<style>  tea-card::part(name) {    color: green;    font-style: italic;  }</style> <tea-card></tea-card> <script src="tea-card.js"></script>
ResultLoading…
  • Lines 2 to 5 style the card's name part: green and italic.
  • Line 8 is the card. Line 10 loads its class.

Green tea turns green and italic. The note doesn't change, because it has no part name. Part names work like classes. One element can have several names, and several elements can share one.

Why have parts at all? The CSS Shadow specification gives the reason. Doing everything with custom properties would take hundreds of them, one for each CSS property. And it would take more for each state, like :hover. A part opens one element, on purpose.

Web Awesome's Dialog docs list nine parts, such as header, title, body, and footer. Ravi styles two:

Style two of the dialog's partsPop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  wa-dialog::part(header) { background: #e3f4e8; }  wa-dialog::part(title) { font-style: italic; }</style> <wa-dialog id="guide" label="Brewing guide">  Use water at 80 degrees. Brew for 3 minutes.  <wa-button slot="footer" data-dialog="close">Close</wa-button></wa-dialog><wa-button data-dialog="open guide">Brewing guide</wa-button>
ResultLoading…
  • Line 5 gives the dialog's header a pale green background.
  • Line 6 puts the dialog's title in italics.
  • Lines 9 to 12 are the brewing guide from the last lesson. Line 13 opens it.

Open the guide. Its header is pale green, and Brewing guide is in italics.

What can follow ::part()?

A part is one element. Some selectors can follow it, and some can't. Try five:

Playground: which ::part() selector matches?
One ::part() selector on a buttonPop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  wa-button::part(label) { font-style: italic; text-transform: uppercase; }</style> <wa-button>Add to order</wa-button>
ResultLoading…
Try it yourself
It matches. The label reads ADD TO ORDER, in italics.

The Button docs list a label part. Your rule styles that element, inside the shadow root, and nothing else.

Only the selector on line 5 changes. The rule turns text italic and into capitals. Each choice runs in a fresh page.

Two rules decide. Pseudo-classes about the part's own state, like :hover, may follow it. Anything that looks inside the part, or at where it sits in its tree, never matches. MDN gives :empty and :last-child as examples. Why it matters: if a part rule does nothing, check what follows the brackets.

Real-life analogyCalling staff by their name tags

At a tea fair, some of Leaf and Kettle's staff wear name tags. You can ask for Asha by name: "Asha, could you wear the green apron?" You can't ask for "whoever is first behind the counter". And you can't ask what is in her pockets. Only the name counts.

In real life: Asha's name tag
In JavaScript: part="name" on an element inside
In real life: Asking for Asha by name
In JavaScript: tea-card::part(name)
In real life: "Whoever is first behind the counter"
In JavaScript: ::part(name):first-child, which never matches
In real life: What is in Asha's pockets
In JavaScript: Elements inside a part, which ::part() can't reach

Parts of a component inside a component

The guide's close button, the × in its corner, is a wa-button inside the dialog's shadow root. That button has its own shadow root and its own button part. A part is seen only one level up, by the tree that holds the component. So the page can't style it, unless the dialog passes it up. The exportparts attribute does that. Here is the close button in the dialog's source, shortened:

The dialog's close button passes two parts up (shortened)HTML
<wa-button
  part="close-button"
  exportparts="base:close-button__base, button:close-button__button"
>
  • Line 2 names the close button itself as a part, close-button.
  • Line 3 passes two of its parts up under new names. Its button part becomes close-button__button.

So the page styles it by that name:

An exported part, and a second ::part() that never matchesPop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  wa-dialog::part(close-button__button) { color: crimson; }  wa-dialog::part(close-button)::part(button) { color: blue; }</style> <wa-dialog id="guide" label="Brewing guide">Use water at 80 degrees.</wa-dialog><wa-button data-dialog="open guide">Brewing guide</wa-button>
ResultLoading…
  • Line 5 uses the exported name, and colors the × crimson.
  • Line 6 puts a second ::part() after the first, and asks for blue.
  • Lines 9 and 10 are the guide and the button that opens it.

Open the guide. The × is crimson, from line 5. Line 6 never matches: the spec doesn't let one ::part() follow another. You don't need to read the source to find the name. The Dialog docs list close-button__button in the CSS parts table.

A dialog's parts. A card, wa-dialog, sits above a window called shadow root. Inside are three cards: part equals title, part equals body, and part equals close-button. Under title and body are the selectors ::part(title) and ::part(body). An arrow labelled exportparts leads from close-button down to a card, part equals button, which is inside the close button. Under it is the selector ::part(close-button__button).<wa-dialog>shadow rootpart="title"part="body"part="close-button"::part(title)::part(body)exportpartspart="button"::part(close-button__button)
Each name the author gives with part is one element you can style. The close button is a component with its own parts, so the dialog passes its button part up with exportparts, as close-button__button.

Read each row across: a question, then the answer for a custom property and for a part.

Custom properties or ::part()?
What you want to knowA custom property::part()
Who decides it exists?The author, by reading it with var() inside the shadow root.The author, by giving an inner element a part name.
What can it change?Only the spots where the author used it.Any CSS property of that one element.
Where do you set it?On the component, or on an element above it, like :root.On the component, as in wa-button::part(label).
Does it reach components inside components?Yes. It keeps passing down, into every shadow root.Only when each component passes the part up with exportparts.
Can it react to :hover?Only if the author adds a second custom property for that state.Yes. ::part(label):hover works.

How do you add dark mode to web components?

The owner wants an evening menu: dark, with warm colors. In Web Awesome, that's one class. Its docs say to add class="wa-dark" to <html>, or to any section of the page. Light is the default. Other classes pick a brand color. Try four on the same card:

Playground: one class on <html>
Theme classes on the html elementPop out in the code editor (opens in a new tab)HTML
<html><link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <wa-card>  <strong slot="header">Green tea</strong>  Brew for 3 minutes. Price: 4  <wa-button slot="footer" variant="brand">Add to order</wa-button></wa-card>
ResultLoading…
Try it yourself
The page is light, and the brand button is blue.

With no class, Web Awesome uses its default theme: a light page and a blue brand color.

Only line 1 changes. There is no other CSS on the page. Each choice runs in a fresh page.

With no class, the page is light and the brand button is blue. wa-dark darkens the page and the card, and the card's text turns light. wa-brand-green changes only the brand color. Two classes can work together. And there is no other CSS on the page.

How does a class on <html> change the inside of a shadow root? It sets custom properties. Web Awesome's theme file gives them light values on :root and dark ones on .wa-dark. The card reads them with var(), so it follows. wa-dark also sets color-scheme, which comes up below. Why it matters: one class at the top themes every component, because custom properties are inherited.

Real-life analogyThe evening switch by the shop door

At closing time, Leaf and Kettle's manager flips one switch by the door. Every lamp in the shop dims, even in the kitchen. Nobody walks around turning lamps down one by one. Some lamps have two bulbs, one for the day and one for the evening. They pick the right one by themselves.

In real life: The switch by the door
In JavaScript: wa-dark on <html>
In real life: Every lamp dims, the kitchen's too
In JavaScript: Every component changes, inside its shadow root
In real life: A lamp with a day bulb and an evening bulb
In JavaScript: light-dark(#00883c, #7bd88f)

Follow the reader's setting

Many people set their computer to dark mode. CSS can ask about it with prefers-color-scheme. Web Awesome doesn't check it for you. Its docs suggest a few lines of your own:

Start dark when the reader prefers darkPop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <wa-card>Brew for 3 minutes.</wa-card> <script>  const query = matchMedia("(prefers-color-scheme: dark)");  const follow = () => document.documentElement.classList.toggle("wa-dark", query.matches);  follow();  query.addEventListener("change", follow);</script>
ResultLoading…
  • Line 7 asks the browser whether the reader prefers dark. matchMedia() gives back an object. Its matches is true or false.
  • Line 8 makes a function. classList.toggle() with a second value adds wa-dark when it's true and removes it when it's false. Styles and classes covers classList.
  • Line 9 runs the function once, at the start.
  • Line 10 runs it again whenever the setting changes.

In Chrome set to dark mode, the page starts dark. Change the setting while the page is open, and it follows. The docs add one more step: keep the reader's own choice in localStorage, and use it before the system setting.

Your own components can follow too

Ravi's tea-card isn't from Web Awesome. A dark green note would be hard to read on a dark page. CSS has two tools for this:

  • color-scheme tells the browser whether an element is light or dark. It's inherited, so it passes into shadow roots. Web Awesome's wa-dark sets it to dark.
  • light-dark() takes two colors. It uses the first in a light color scheme and the second in a dark one.

This version of tea-card.js changes line 7 to light-dark(#00883c, #7bd88f). Two boxes hold one card each:

One card class, in a light box and a dark boxPop out in the code editor (opens in a new tab)HTML
<style>  .day { color-scheme: light; }  .evening { color-scheme: dark; }  .day, .evening { background: Canvas; color: CanvasText; padding: 4px 12px; }</style> <div class="day"><tea-card></tea-card></div><div class="evening"><tea-card></tea-card></div> <script src="tea-card.js"></script>
ResultLoading…
  • Line 2 makes the first box light. Line 3 makes the second box dark.
  • Line 4 gives both boxes the browser's own colors for their scheme. Canvas is the page background, and CanvasText is the text color.
  • Lines 7 and 8 put a card in each box.

The day card shows a dark green note on white. The evening card shows a light green note on a dark background. Both are the same class. Each card took its color scheme from the box around it, through its shadow root. Burton Smith pointed this out in 2024: color-scheme crosses the shadow boundary, so components can follow it.

One class switches a whole page. On the left, day: a card, html, with arrows to two cards, wa-card and tea-card. Under them: white, and the color 00883c. Under the row: color-scheme: light. On the right, evening: a card, html class equals wa-dark, with arrows to the same two cards. Under them: dark, and the color 7bd88f. Under the row: color-scheme: dark.day<html><wa-card><tea-card>white card#00883ccolor-scheme: lightevening<html class="wa-dark"><wa-card><tea-card>dark card#7bd88fcolor-scheme: dark
wa-dark changes the theme's custom properties and color-scheme on <html>. Both are inherited, so Web Awesome's card and your own tea-card follow, inside their shadow roots.

Why to avoid :host-context()

Old tutorials theme a component with :host-context(), from inside its shadow root. It checks the host and the elements above it:

Theming with :host-context(), which only Chrome runsCSS
/* In a component's shadow root: avoid this */
:host-context(.wa-dark) .brew {
  color: #7bd88f;
}

Line 2 says: if the host or any element above it has the class wa-dark, color the note light green. It works only in Chrome. Firefox and Safari never added it. On March 19, 2025, the CSS Working Group decided to drop it from the specification, and MDN marks it deprecated. Use what this section showed instead: custom properties, color-scheme, and light-dark(). They're inherited, so they work in every browser.

Why can't page CSS style inside a web component?

You've met each edge already. Here they are together:

  • Page selectors only look at the page's own tree. .name, tea-card h3, and wa-button .button never find anything inside a shadow root.
  • Inheritance only fills gaps. A value the component sets itself wins over an inherited one.
  • ::part() styles the part, never what's inside it.
  • A part inside another component reaches the page only through exportparts.
  • The old piercing selectors are gone. Browsers removed /deep/, also written >>>, and ::shadow, as lesson 1.2 explained.

Read each row across: what you write in the page, whether it reaches inside, and why.

What reaches inside a shadow root?
What you write in the pageDoes it reach inside?Why
body { color: chocolate; }Yes, where the component sets no color of its own.color is inherited, and inheritance passes into shadow roots.
tea-card { --tea-accent: green; }Yes, wherever the component reads it with var().Custom properties are inherited too.
.name { … } or tea-card h3 { … }No.Page selectors only look at the page's own tree.
wa-button::part(label) { … }Yes, that one element.The author named it with part="label".
wa-button::part(label) span { … }No.::part() never selects inside a part.
/deep/ or >>>No. Browsers removed them.They showed the page too much of a component's inside.

Why the wall is there

The wall protects both sides. First, names can't clash. In lesson 1.3, a blog's .name rule turned a card's heading gray by accident. Inside a shadow root, it couldn't.

Real-life analogyTwo cooks called Sam

Leaf and Kettle has two cooks called Sam. One works in the café, and one in the bakery next door. When the café manager calls "Sam, more milk!", only the café's Sam hears. The bakery's Sam never gets orders meant for someone else. He still reads the shared noticeboard, though.

In real life: The café manager calling "Sam!"
In JavaScript: A page rule, like .name { color: crimson; }
In real life: The bakery's own Sam
In JavaScript: An element with the same class, inside a shadow root
In real life: The shared noticeboard
In JavaScript: Inherited values and custom properties, which still reach inside

Second, the author can change the inside without breaking your page. Web Awesome's docs say that once a part is in a component's API, it stays until the next major version. The inside, they add, will likely change as components evolve. That's why the button's old base part still works but is marked deprecated. It was renamed button, and the old name stays until the next major version.

Cory LaViska, who created Shoelace, puts it this way: every part you expose is an API you commit to supporting. So a library should add parts with care. Why it matters: when a component doesn't offer the style you need, ask its authors for a part or a custom property, or choose another component. Stage 4 shows the other side, building these doors into your own components.

Give the tea menu a day and an evening theme

First, sort each of the owner's wishes by the door it goes through:

Custom property, part, or out of reach?
  • Make every brand button green
  • Use Georgia in every component
  • Make the brewing guide wider
  • Put the guide's title in italics
  • Square the corners of the Add to order button
  • Color the guide's close button
  • Underline one span inside the button's label
  • Make the third star of a rating bigger
Try it yourself
0 of 8 correct

Sort each change to Ravi's menu page by the way you'd make it from outside the component.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

Now Ravi's menu card, with both themes. By day it's matcha: tea green, on a light page. In the evening it's roasted oolong: warm orange, on a dark page.

Leaf and Kettle's menu card, with a day and an evening themePop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  :root {    --wa-font-family-body: Georgia, serif;    --wa-border-radius-scale: 2;  }  wa-dialog { --width: 22rem; }  wa-dialog::part(title) { font-style: italic; }</style> <wa-card>  <strong slot="header">Green tea</strong>  Brew for 3 minutes. Price: 4  <wa-button slot="footer" variant="brand" data-dialog="open guide">Brewing guide</wa-button>  <wa-button slot="footer" id="theme">Evening menu</wa-button></wa-card> <wa-dialog id="guide" label="Brewing guide">  Use water at 80 degrees.  <wa-button slot="footer" data-dialog="close">Close</wa-button></wa-dialog> <script>  const root = document.documentElement;  root.classList.add("wa-brand-green");  document.querySelector("#theme").addEventListener("click", () => {    const evening = root.classList.toggle("wa-dark");    root.classList.toggle("wa-brand-green", !evening);    root.classList.toggle("wa-brand-orange", evening);  });</script>
ResultLoading…
  • Lines 5 to 8 set two theme tokens for every component: the Georgia font, and corners twice as round.
  • Line 9 makes the guide narrower, with the dialog's own --width, set on the dialog.
  • Line 10 puts the guide's title in italics, through its title part.
  • Lines 13 to 18 are the card. Line 16 opens the guide. Line 17 is the theme button.
  • Lines 20 to 23 are the guide.
  • Line 27 starts the page green, with wa-brand-green on <html>.
  • Lines 28 to 32 switch themes on a click. Line 29 toggles wa-dark and keeps whether the page is now dark in evening. Lines 30 and 31 swap green for orange in the evening, and back.

The card starts green, in Georgia, with round corners. Click Evening menu. The page and the card go dark, and the brand button turns orange. Click it again for the day theme. Open the guide in either theme. Its title is italic, and its preferred width is 22rem, not the dialog's default 31rem.

Ravi wrote no selector that reaches inside a component. Every change went through a door Web Awesome built: two tokens, one component custom property, one part, and three classes.

Common mix-ups

  • "Page CSS never reaches inside a shadow root." No. Inherited properties like color and font-family pass in, and so does every custom property.
  • "wa-button .button styles the inner button." No. Page selectors stop at the shadow root. Use the part: wa-button::part(button).
  • "A custom property works wherever you set it." No. If the component sets it on :host, a value on :root never arrives. Set it on the component.
  • "::part() can style what's inside the part." No. ::part(label) span matches nothing. Only the part itself.
  • "You can chain parts, like ::part(close-button)::part(button)." No. That never matches. Use the exported name, close-button__button.
  • "/deep/ or >>> gets you inside." No. Browsers removed both.
  • ":host-context(.dark) is a good way to theme." No. Only Chrome has it, and the CSS Working Group dropped it.

Practice exercises

Answer first, then use the hints. Every result runs live, so you can check yourself.

Exercise 1 · Warm-upWhich color reaches the name?

What color is Green tea?

<style>  body { color: chocolate; }  .name { color: crimson; }</style> <tea-card></tea-card> <script src="tea-card.js"></script>
ResultLoading…

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · Warm-upSquare corners

    Line 5 should square the button's corners, but it doesn't. Fix it. Which selector works?

    <link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  wa-button .button { border-radius: 0; }</style> <wa-button variant="brand">Add to order</wa-button>
    ResultLoading…

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeGreen stars

      Line 5 should make the lit stars green, but they stay yellow. Which selector should hold the custom property?

      <link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  :root { --symbol-color-active: #00883c; }</style> <wa-rating value="4"></wa-rating>
      ResultLoading…

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeThe evening switch

        Replace the comment on line 9, so each click switches the page between light and dark. Which class does it toggle?

        <link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <wa-card>Brew for 3 minutes.</wa-card><wa-button id="evening">Evening menu</wa-button> <script>  document.querySelector("#evening").addEventListener("click", () => {    // Switch the whole page between light and dark here  });</script>
        ResultLoading…

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeThe close button

          Line 5 should color the guide's × crimson. Open the guide: the × stays gray. Fix line 5. Which part name works?

          <link rel="stylesheet" href="https://ka-f.webawesome.com/webawesome@3.15.0/styles/webawesome.css"><script type="module" src="https://ka-f.webawesome.com/webawesome@3.15.0/webawesome.loader.js"></script> <style>  wa-dialog::part(close-button)::part(button) { color: crimson; }</style> <wa-dialog id="guide" label="Brewing guide">Use water at 80 degrees.</wa-dialog><wa-button data-dialog="open guide">Brewing guide</wa-button>
          ResultLoading…

          Answer, then press Check. Spacing and letter case don’t matter.

            Exercise 6 · ChallengeA note for the evening

            In the evening box, the dark green note is hard to read. Change the tea-card.js tab so the note is light green there and stays dark green by day. Which function did you use?

            <style>  .day { color-scheme: light; }  .evening { color-scheme: dark; }  .day, .evening { background: Canvas; color: CanvasText; padding: 4px 12px; }</style> <div class="day"><tea-card></tea-card></div><div class="evening"><tea-card></tea-card></div> <script src="tea-card.js"></script>
            ResultLoading…

            Answer, then press Check. Spacing and letter case don’t matter.

              Check your understanding

              For each question, ask: is it inherited, is it a named part, or is it a selector that stops at the shadow root?

              The theming components quiz · 8 questionsScore: first tries count
              1. Question 1 of 8Which page rule changes the text inside <tea-card>'s shadow root?

                Choose an answer to see the explanation.

              2. Question 2 of 8What color is the line Brew for 3 minutes on this page?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <style>
                  tea-card { --tea-accent: green; }
                  .brew { color: crimson; }
                </style>
                
                <tea-card></tea-card>
                
                <script src="tea-card.js"></script>

                Choose an answer to see the explanation.

              3. Question 3 of 8A page sets :root { --symbol-color-active: crimson; }. What color are a wa-rating's lit stars?

                Choose an answer to see the explanation.

              4. Question 4 of 8What does wa-button::part(label) span style?

                Choose an answer to see the explanation.

              5. Question 5 of 8How do you style the button inside wa-dialog's close button?

                Choose an answer to see the explanation.

              6. Question 6 of 8What does class="wa-dark" on <html> change?

                Choose an answer to see the explanation.

              7. Question 7 of 8Why should you avoid theming a component with :host-context(.dark)?

                Choose an answer to see the explanation.

              8. Question 8 of 8Why can't page selectors reach inside a shadow root?

                Choose an answer to see the explanation.

              Key takeaways

              • You style a component from outside only through the doors its author built.
              • Inherited properties, like color and font-family, and every custom property pass into shadow roots.
              • Inheritance only fills gaps. Set a component's own custom properties on the component, not on :root.
              • Web Awesome's theme is custom properties, the --wa- tokens. Set them on :root, or use classes like wa-brand-green.
              • ::part(name) styles one named element, with any CSS property. :hover may follow it. Selectors inside it, and a second ::part(), never match.
              • A part of an inner component reaches the page only when it's exported, like close-button__button.
              • wa-dark on <html> switches the theme. color-scheme and light-dark() let your own components follow. Avoid :host-context().

              Remember the one-liner.
              Custom properties flow in, parts are named doors, and everything else stays inside.

              Sources

              The facts on this page come from these docs, specifications, and posts:

              Coming next: Styling undefined elements with :defined. Tags show up before their JavaScript arrives. Learn to hide them or size them, so the page doesn't jump.

              CompleteFrontend Clear concepts. Working examples.