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.
- 01Theme with custom propertiesSay which page styles pass into a shadow root, and set a library's custom properties where they take effect.
- 02Style 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.
- 03Add light and dark themesSwitch Web Awesome between light and dark with one class, and make your own component follow with
color-schemeandlight-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:
<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>- 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
buttoninsidewa-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-50for its main color. Ravi gives it a tea green. - Line 7 styles the button's
labelpart 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:
::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.
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
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:
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-accentisn't set, it uses the fallback after the comma: gray. - Line 9 is the name, an
<h3>withclass="name"andpart="name". - Line 10 is the note.
The playground puts this card on a page with one rule. Pick a rule and watch the card:
<style> body { color: chocolate; }</style> <tea-card></tea-card> <script src="tea-card.js"></script>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.
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.
<tea-card>. color passes from the body down to the name, and --tea-accent reaches the note, where var() reads it.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:
<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>- 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, to95, the lightest. Buttons use step50. - 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:
<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>- 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:
<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>- Line 5 sets
--symbol-color-activeon: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> tea-card::part(name) { color: green; font-style: italic; }</style> <tea-card></tea-card> <script src="tea-card.js"></script>- Lines 2 to 5 style the card's
namepart: 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:
<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>- 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:
<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>The Button docs list a label part. Your rule styles that element, inside the shadow root, and nothing else.
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.
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:
<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
buttonpart becomesclose-button__button.
So the page styles it by that name:
<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>- 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.
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.
| What you want to know | A 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:
<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>With no class, Web Awesome uses its default theme: a light page and a blue brand color.
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.
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-darkon<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:
<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>- Line 7 asks the browser whether the reader prefers dark.
matchMedia()gives back an object. Itsmatchesistrueorfalse. - Line 8 makes a function.
classList.toggle()with a second value addswa-darkwhen it'strueand removes it when it'sfalse. Styles and classes coversclassList. - 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-schemetells the browser whether an element is light or dark. It's inherited, so it passes into shadow roots. Web Awesome'swa-darksets it todark.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:
<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>- 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.
Canvasis the page background, andCanvasTextis 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.
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:
/* 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, andwa-button .buttonnever 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 you write in the page | Does 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.
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:
- 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
Sort each change to Ravi's menu page by the way you'd make it from outside the component.
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.
<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>- 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
titlepart. - 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-greenon<html>. - Lines 28 to 32 switch themes on a click. Line 29 toggles
wa-darkand keeps whether the page is now dark inevening. 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
colorandfont-familypass in, and so does every custom property. - "
wa-button .buttonstyles 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:rootnever arrives. Set it on the component. - "
::part()can style what's inside the part." No.::part(label) spanmatches 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.
What color is Green tea?
<style> body { color: chocolate; } .name { color: crimson; }</style> <tea-card></tea-card> <script src="tea-card.js"></script>Chocolate. color on the body is inherited, and inheritance passes into the shadow root. The .name rule never matches, because page selectors don't look inside.
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><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(button) { border-radius: 0; }</style> <wa-button variant="brand">Add to order</wa-button>wa-button::part(button). Line 5 used a page selector, which can't see inside. A part is the door the author built.
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><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-rating { --symbol-color-active: #00883c; }</style> <wa-rating value="4"></wa-rating>wa-rating. A value on :root would only be inherited, and the rating sets its own on :host. A page rule on the element itself beats the :host rule, so the lit stars turn green.
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><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", () => { document.documentElement.classList.toggle("wa-dark"); });</script>wa-dark. document.documentElement.classList.toggle("wa-dark") adds the class on one click and removes it on the next. Every component follows the custom properties it changes.
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><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; }</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>close-button__button. The dialog exports the close button's button part under that name, so wa-dialog::part(close-button__button) reaches it.
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><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>light-dark(). The evening box sets color-scheme: dark, which passes into the card's shadow root. light-dark() then picks its second color, a light green.
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?
Question 1 of 8Which page rule changes the text inside
<tea-card>'s shadow root?Choose an answer to see the explanation.
Question 2 of 8What color is the line Brew for 3 minutes on this page?
Read the code, then predict<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.
Question 3 of 8A page sets
:root { --symbol-color-active: crimson; }. What color are awa-rating's lit stars?Choose an answer to see the explanation.
Question 4 of 8What does
wa-button::part(label) spanstyle?Choose an answer to see the explanation.
Question 5 of 8How do you style the button inside
wa-dialog's close button?Choose an answer to see the explanation.
Question 6 of 8What does
class="wa-dark"on<html>change?Choose an answer to see the explanation.
Question 7 of 8Why should you avoid theming a component with
:host-context(.dark)?Choose an answer to see the explanation.
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
colorandfont-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 likewa-brand-green. ::part(name)styles one named element, with any CSS property.:hovermay 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-darkon<html>switches the theme.color-schemeandlight-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:
- Customizing & Theming, Web Awesome.
- Using shadow DOM: Encapsulation from CSS, MDN.
- CSS Shadow Module Level 1: Flattening the DOM into an element tree, W3C CSS Working Group.
- Using CSS custom properties (variables), MDN.
- Options for styling web components, Nolan Lawson.
- CSS Cascading and Inheritance Level 5: Context, W3C CSS Working Group.
- Rating, Web Awesome.
- CSS Shadow Module Level 1: Exposing a shadow element, W3C CSS Working Group.
- ::part(), MDN.
- Dialog, Web Awesome.
- exportparts, MDN.
- Theming, Web Awesome.
- color-scheme, MDN.
- light-dark(), MDN.
- Dark mode in web components is about to get awesome, Burton Smith.
- :host-context(), MDN.
- Resolution to drop :host-context(), issue 1914, W3C CSS Working Group.
- When to create CSS parts, Cory LaViska.
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.