completefrontend
Open lab My progress
THE WEB COMPONENTS FIELD GUIDE

Styles inside a shadow root

Learn how to add CSS to shadow DOM with a style or link element, which properties a shadow root inherits from the page, and how to reset them.

By the end, you can
  • 01
    Put styles inside a shadow rootUse a <style> or a <link rel="stylesheet"> in a shadow root, and know why a link can show the card plain for a moment.
  • 02
    Say what crosses the boundaryTell inherited properties like color and font-family, which pass in from the page, from ones like border, which don't.
  • 03
    Reset what comes inStart fresh with all: initial, give inner buttons the page's font with font: inherit, and keep selectors short.

How do you add CSS to shadow DOM?

It's a quiet morning at Leaf and Kettle. Asha is giving the menu page a new look. She sets the page's font to Georgia. She makes every tea name crimson, with a .name rule. Then she reloads.

The tea cards change, but only halfway. Their text is in Georgia now. But each card's tea name stays sea green, not crimson. And the card's Add button keeps its small, plain font. Some of Asha's styles reached into the card. Others didn't. Why?

A shadow root carries its own CSS, in a <style> or <link rel="stylesheet"> element inside it. Those rules only match elements in that root. The page's rules only match the page. What does cross is inherited values, like color and font-family. They flow from the page into the component, unless the component resets them.

Here is Asha's page, cut down to one card:

A page rule and a card rule for the same classPop out in the code editor (opens in a new tab)HTML
<style>  body { font-family: Georgia, serif; }  .name { color: crimson; }</style><p class="name">Today's teas</p><tea-card></tea-card><script src="tea-card.js"></script><script>  const inside = document.querySelector("tea-card").shadowRoot;  for (const name of [document.querySelector(".name"), inside.querySelector(".name")]) {    const style = getComputedStyle(name);    console.log(name.textContent, style.color, style.fontFamily);  }</script>
ResultLoading…
  • Lines 2 and 3 are Asha's page rules. One sets a font for the whole page. The other makes anything with the class name crimson.
  • Line 5 is a page paragraph with that class. Line 6 is the card.
  • In the tea-card.js tab, lines 6 to 8 put a <style> element inside the card's shadow root. Its rule, on line 7, makes .name sea green. Line 9 is the card's own .name paragraph.
  • Back in the page, lines 10 to 13 read both names with getComputedStyle(). It gives an element's final styles, after every rule is applied. Styles and classes covers it.

It prints Today's teas rgb(220, 20, 60) Georgia, serif, then Green tea rgb(46, 139, 87) Georgia, serif. The page's name is crimson. The card's name is sea green. But both are in Georgia.

So two things happened at once. The page's .name rule never matched the card's paragraph. Selectors stay in their own tree. But the font got in anyway. Asha never selected the card's paragraph. She set a font on body, and the paragraph inherited it.

A window called page holds two page rules: Georgia font, and .name: crimson. Inside the page, a smaller window called shadow root holds the card's own rule, .name: sea green, and the card's name, Green tea. An arrow from Georgia font crosses into the shadow root and reaches Green tea. An arrow from .name: crimson stops at the shadow root's edge.pagepage rulesGeorgia font.name: crimsonshadow root.name: sea greenGreen teainherited: passes instops here
The page's font flows into the card, because font-family is inherited. The page's .name rule stops at the edge. Inside, only the card's own .name rule matches Green tea.
Real-life analogyThe tea shop's front window

Leaf and Kettle keeps its best cups on a shelf behind the front window. Sunlight and the warmth of the street pass through the glass, and the cups catch the light. But a customer can't reach through the glass to move a cup. Only the staff inside arrange the shelf.

In real life: Sunlight and warmth through the glass
In JavaScript: Inherited values, like color and font-family
In real life: A customer's hand
In JavaScript: A page selector, like .name
In real life: The shelf behind the glass
In JavaScript: The card's shadow root
In real life: The staff who arrange the cups
In JavaScript: The root's own <style> rules
The main idea

Put a component's CSS inside its shadow root, in a <style> or a <link rel="stylesheet">. Its rules style only that root, and the page's rules style only the page. Inherited values, like the text color and the font, still flow in from the page, unless the component resets them.

Lesson 6.3 showed this boundary with a single rule. This lesson teaches you to work with it: the two ways to ship styles, what inherits, how to reset it, and why short selectors are safe.

Which CSS properties are inherited into shadow DOM?

Asha's font got into the card without selecting anything. That's inheritance. When an element has no value of its own for certain properties, it takes its parent's value. Only some properties work this way. The CSS specs mark each one as inherited or not.

Lesson 6.3 showed a color passing into a card. Here is one page rule with two properties, one of each kind:

A color and a border on the cardPop out in the code editor (opens in a new tab)HTML
<style>  tea-card { color: saddlebrown; border: 2px solid crimson; }</style><tea-card></tea-card><script src="tea-card.js"></script><script>  const name = document.querySelector("tea-card").shadowRoot.querySelector("p");  const style = getComputedStyle(name);  console.log("color:", style.color);  console.log("border:", style.borderTopWidth);</script>
ResultLoading…
  • Line 2 is the page's rule for the card: a saddlebrown color and a crimson border.
  • The tea-card.js tab gives the card a shadow root with one paragraph, Green tea, and no styles.
  • Line 7 finds that paragraph. Line 8 gets its final styles.
  • Line 9 prints its color. Line 10 prints the width of its top border.

It prints color: rgb(139, 69, 19) and border: 0px. The paragraph is saddlebrown, like the card. But it has no border. In the result, the crimson border is drawn once, around the whole card.

Two rows: tea-card on top, and p Green tea, inside the card's shadow root, below. An arrow leads from tea-card down to the p. Beside them, two columns. The color column says saddlebrown on both rows, with an arrow down labelled inherited. The border column says 2px crimson on tea-card and 0px on the p, labelled not inherited.colorborder<tea-card>saddlebrown2px crimsoninheritednot inherited<p> Green teasaddlebrown0pxin the shadow root
The page's rule sets both properties on the card. color is inherited, so the name inside gets saddlebrown too. border isn't, so the name's border is 0px.

Why does the paragraph inherit from the card? In the DOM, the card isn't its parent: the paragraph lives in the card's shadow tree. The CSS Cascade spec says inheritance follows the flattened element tree instead. That's the tree the browser draws, where a shadow root's contents take the place of the host's children. In that tree, the paragraph's parent is the card. Lesson 8.2 showed the same tree carrying a color through a slot.

Now test the other properties yourself. Pick one for the page's rule:

Playground: which properties cross into the shadow root?
One page rule, read on the card and inside itPop out in the code editor (opens in a new tab)HTML
<style>  tea-card { color: saddlebrown; }</style><tea-card></tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  const name = card.shadowRoot.querySelector("p");  console.log("Card:", getComputedStyle(card).color);  console.log("Name:", getComputedStyle(name).color);</script>
ResultLoading…
Try it yourself
It crosses. Card: rgb(139, 69, 19). Name: rgb(139, 69, 19).

color is inherited. The name has no color of its own, so it takes the card's: saddlebrown.

Each choice puts a different declaration in the page's rule on line 2, and lines 9 and 10 read that property on the card and on the name inside it. The tea-card.js tab holds one paragraph and no styles.
  • These cross: color, font-family, line-height, text-align, and cursor. So do font-size, letter-spacing, and visibility.
  • These stop: background-color, border, and padding. Each one belongs to a single box.

Look closely at background-color. The name's own background prints rgba(0, 0, 0, 0), which is transparent. Yet the result shows wheat behind it. Nothing was inherited. The card's box is painted wheat, and the see-through name lets it show.

Real-life analogyA family's language, and a family's house

Children grow up speaking the language of their home. Nobody hands each child a copy: they pick it up from the people around them. But they don't inherit the walls or the paint. Those belong to the house alone.

In real life: The home
In JavaScript: The card, the shadow host
In real life: The children
In JavaScript: The elements inside its shadow root
In real life: The language they pick up
In JavaScript: Inherited properties: color, font-family, line-height
In real life: The walls and the paint
In JavaScript: border, background-color, padding: one box's own properties

Why it matters: inheritance is why components fit their page with no work. The card's text matches the page's text. It's also why a page can change a component's look without asking. The next section shows how to stop it.

Custom properties, like --tea-accent, inherit too. Lesson 2.3 used them, and lesson 12.1 turns them into a styling API.

How do you reset inherited styles in shadow DOM?

Sometimes a component needs a clean start. Say it must look the same on every page that uses it, whatever font or color the page picked. web.dev's shadow DOM guide suggests one line for this: all: initial on the host.

all is a shorthand for every CSS property at once. It only takes the keywords every property accepts, like initial. And initial means the property's starting value, the one the CSS spec gives it before any style sheet or parent changes it.

A card that starts fresh with all: initialPop out in the code editor (opens in a new tab)HTML
<style>  body { font: italic 20px Georgia, serif; color: saddlebrown; }</style><tea-card></tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  const style = getComputedStyle(card.shadowRoot.querySelector("p"));  console.log("Name:", style.color, style.fontSize, style.fontStyle);  console.log("Card:", getComputedStyle(card).display);</script>
ResultLoading…
  • Line 2 gives the page an italic 20px Georgia font, in saddlebrown.
  • In the tea-card.js tab, line 7 is the reset. :host selects the card itself, from inside its shadow root. The next lesson covers it in full. all: initial resets every property on the card. Then display: block sets one back, because a custom element is inline by default.
  • Line 8 gives the name a white background. The reset also drops the page's light or dark setting, so the text is black. White keeps it readable in any theme.
  • Back in the page, line 9 prints the name's color, size, and style. Line 10 prints the card's display.

It prints Name: rgb(0, 0, 0) 16px normal and Card: block. Black, 16px, and upright: the starting values. The card got them first, and the name inherited them. Nothing from the page got through.

Real-life analogyA barista's uniform

A new barista arrives at Leaf and Kettle in their own clothes. Before the shift, they change into the plain shop uniform. Then they pin on the name badge. Whatever they wore on the street, every barista starts the shift looking the same.

In real life: The clothes they arrived in
In JavaScript: The values the card would inherit from the page
In real life: Changing into the plain uniform
In JavaScript: all: initial
In real life: Pinning on the badge afterwards
In JavaScript: The declarations after all, like display: block

The badge goes on after the uniform. CSS works the same way: inside one rule, a later declaration beats an earlier one. So write all first. Try the other order, and four other resets, here:

Playground: which reset stops inheritance?
An italic page, and a card that may reset itPop out in the code editor (opens in a new tab)HTML
<style>  body { font: italic 20px Georgia, serif; color: saddlebrown; }</style><tea-card></tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  const style = getComputedStyle(card.shadowRoot.querySelector("p"));  console.log("Name:", style.color, style.fontSize, style.fontStyle);  console.log("Card:", getComputedStyle(card).display);</script>
ResultLoading…
Try it yourself
A fresh start.

Name: rgb(0, 0, 0) 16px normal. Card: block. all: initial resets every property on the card to its starting value. The name inherits those, so nothing from the page gets through. Then display: block sets one property back.

The page stays the same. Each choice changes only line 7 of the tea-card.js tab, the card's :host rule. Open that tab to see it.
  • No reset: the name takes the page's color, size, and italic style.
  • font: initial resets only the font. The name is 16px and upright, but still saddlebrown.
  • all: initial resets everything, as before.
  • all: revert looks like a reset, but the page's styles still come in.
  • display before all: all: initial comes second, so it resets display too. The card prints inline.

Why does all: revert keep the page's styles? The spec says revert acts as if your own rules for that property weren't there. Then the browser's own styles apply. The browser has no color or font rule for a custom element, so the card inherits them, just as before. Read the table across to compare the four keywords:

What each keyword does in a rule inside a shadow root
When you writeFor an inherited property, like colorFor other properties, like border
initialThe property's starting value. The page's value is dropped.The property's starting value.
inheritThe parent's value, which is what happens anyway.The parent's value, even though it normally isn't passed down.
unsetThe parent's value, as if you wrote inherit.The starting value, as if you wrote initial.
revertThe browser's own style, if it has one. Otherwise the parent's value.The browser's own style, if it has one. Otherwise the starting value.

What does the all property leave alone?

all skips a few things on purpose. The CSS Cascade spec says it resets every property except direction and unicode-bidi, and never touches custom properties. This page puts the reset card in a right-to-left block, with a custom property on the page:

What all: initial keepsPop out in the code editor (opens in a new tab)HTML
<style>  body { color: saddlebrown; font-size: 20px; --tea-accent: crimson; }</style><div dir="rtl">  <tea-card></tea-card></div><script src="tea-card.js"></script><script>  const name = document.querySelector("tea-card").shadowRoot.querySelector("p");  const style = getComputedStyle(name);  console.log("Reset:", style.color, style.fontSize);  console.log("Kept:", style.getPropertyValue("--tea-accent"), style.direction);</script>
ResultLoading…
  • Line 2 sets a color, a size, and a custom property, --tea-accent, on the page.
  • Lines 4 to 6 put the card in a <div dir="rtl">. That's a right-to-left block, as for Arabic or Hebrew.
  • The tea-card.js tab is the reset card from before.
  • Line 11 prints the name's color and size. Line 12 prints --tea-accent and the direction.

It prints Reset: rgb(0, 0, 0) 16px and Kept: crimson rtl. The color and size were reset. The custom property and the direction came through.

The rule :host { all: initial; display: block; } at the top. Left half, under reset: two crossed-out cards, color: saddlebrown and font-size: 20px. Right half, under kept: two cards, --tea-accent: crimson and direction: rtl.:host { all: initial; display: block; }resetcolor: saddlebrownfont-size: 20pxkept--tea-accent: crimsondirection: rtl
With all: initial on the card, the page's color and size stop at the card. A custom property and the direction still come through: all never resets them.

Both gaps are on purpose. The spec says direction belongs in the markup, with the dir attribute, not in a style sheet. And custom properties are how a page can still talk to a reset component. color: var(--tea-accent) inside the card would still pick up crimson. Lesson 12.1 builds on that.

Where should the reset go?

A reset on :host has one weak spot. A page rule written for tea-card itself beats a :host rule. Lesson 11.4 explains that order. So if the page says tea-card { color: darkorange; }, the name turns dark orange, reset or not. A reset on an element inside the root has no such problem:

A reset on a wrapper inside the rootPop out in the code editor (opens in a new tab)HTML
<style>  body { color: saddlebrown; }  tea-card { color: darkorange; }</style><tea-card><span slot="note">Fresh today</span></tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  const name = card.shadowRoot.querySelector("p");  const note = card.querySelector("span");  for (const element of [card, name, note]) {    console.log(element.localName, getComputedStyle(element).color);  }</script>
ResultLoading…
  • Line 3 gives the card itself a dark orange color.
  • Line 5 slots a note from the page, Fresh today, into the card.
  • In the tea-card.js tab, line 7 resets a wrapper, <article class="card">, instead of the host. Lines 9 to 12 put the name and the note's slot inside it.
  • Lines 11 to 13 of the page print the color of the card, the name, and the note.

It prints tea-card rgb(255, 140, 0), then p rgb(0, 0, 0) and span rgb(0, 0, 0). The card itself is dark orange: the page's rule styles it. But inside, the wrapper started fresh. With the same reset on :host instead, all three lines print dark orange.

Look at the note. It's the page's own <span>, a child of the dark orange card. Yet it's black. In the flat tree, a slotted element inherits from its slot, and this slot sits inside the reset wrapper. Why it matters: a reset inside the root covers slotted content too.

Why doesn't a button in shadow DOM use the page font?

Back to Asha's Add button. Buttons, inputs, and other form controls don't inherit their font. The browser's own style sheet gives them a small control font. Chrome's styling guide calls this "the single most common miss" for components that wrap a native control.

A page can fix its own buttons with a reset like button { font: inherit; }. But as lesson 6.4 showed, a page reset can't reach inside a shadow root. So the component needs the line itself:

A button that uses the page fontPop out in the code editor (opens in a new tab)HTML
<style>  body { font: 20px Georgia, serif; }</style><tea-card></tea-card><script src="tea-card.js"></script><script>  const add = document.querySelector("tea-card").shadowRoot.querySelector("button");  console.log(add.textContent, getComputedStyle(add).fontSize);</script>
ResultLoading…
  • Line 2 gives the page a 20px Georgia font.
  • In the tea-card.js tab, line 7 is the fix: the card's buttons inherit the font. Line 10 is the Add button.
  • Line 8 of the page prints the button's text and font size.

It prints Add 20px. Now delete line 7 in the tea-card.js tab. It prints Add 13.3333px, the browser's size for form controls. font: inherit takes the whole font from the parent: the family, the size, and the line height. A button's text color doesn't inherit either, so add color: inherit when you want that too.

Why can CSS selectors stay simple in shadow DOM?

On a big site, many people's CSS ends up on one page. Two teams both write .name, and one team's rule changes the other's headings. One fix is a long prefix on every class. BEM, a naming method, writes names like .tea-card__name. Monica Dinculescu points out that naming methods like this grew up because CSS had no way to keep styles apart.

A shadow root is that way. Selectors are matched one tree at a time. This page and its card use the same class and the same id:

The same class and id on the page and in the cardPop out in the code editor (opens in a new tab)HTML
<style>  .name { color: crimson; }  #price { color: darkorange; }</style><p class="name">Today's teas</p><p id="price">Prices include tax</p><tea-card></tea-card><script src="tea-card.js"></script><script>  const inside = document.querySelector("tea-card").shadowRoot;  for (const p of [...document.querySelectorAll("p"), ...inside.querySelectorAll("p")]) {    console.log(p.textContent, getComputedStyle(p).color);  }</script>
ResultLoading…
  • Lines 2 and 3 are page rules for .name and #price. Lines 5 and 6 are the page's paragraphs.
  • In the tea-card.js tab, lines 7 and 8 are the card's rules for the same .name and #price. Lines 10 and 11 are its paragraphs.
  • Lines 11 to 13 of the page print the color of all four paragraphs.

It prints Today's teas rgb(220, 20, 60) and Prices include tax rgb(255, 140, 0): crimson and dark orange, from the page's rules. Then Green tea rgb(46, 139, 87) and Price: 4 rgb(70, 130, 180): sea green and steel blue, from the card's rules. No rule crossed, in either direction. Each tree even has its own #price, as lesson 6.3 showed for ids.

So inside a shadow root, short names are enough:

Prefixed names on a shared page, short names in a shadow rootCSS
/* On a page that many people share */
.tea-card__name { color: seagreen; }
.tea-card__price--sale { color: crimson; }

/* Inside the tea card's shadow root */
.name { color: seagreen; }
.price.sale { color: crimson; }
  • Lines 2 and 3 are what a shared page needs: the component's name in every class.
  • Lines 6 and 7 do the same job inside the card's shadow root.

web.dev's guide calls simpler selectors "a best practice inside Shadow DOM". Why it matters: short selectors are easier to read and to change, and no page can break them by accident.

Real-life analogyLabels in your own kitchen

In an office kitchen, everyone writes their name on their jars: "Asha's sugar", "Ravi's sugar". Otherwise somebody takes the wrong one. At home, a jar that says "sugar" is enough. Nobody else's jars are on your shelf.

In real life: The shared office kitchen
In JavaScript: One page where every team's CSS meets
In real life: Names written on every jar
In JavaScript: Long prefixed classes, like .tea-card__name
In real life: Your own kitchen at home
In JavaScript: The card's shadow root
In real life: A jar that just says "sugar"
In JavaScript: A short selector, like .name or button

Now sort each page rule. Does it reach inside the card, or stop at its shadow root?

Sort the page rules
  • body { color: saddlebrown; }
  • body { font-family: Georgia, serif; }
  • tea-card { line-height: 1.6; }
  • body { --tea-accent: crimson; }
  • .name { color: crimson; }
  • p { margin: 0; }
  • button { font: inherit; }
  • * { box-sizing: border-box; }
  • #price { color: darkorange; }
Try it yourself
0 of 9 correct

Drag each page rule to where it ends up: inside the card, or stopped at its shadow root.

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

What happened to /deep/, >>> and ::shadow?

Older guides style the inside of shadow roots from the page with /deep/, >>>, or ::shadow. /deep/ and ::shadow came from the first version of shadow DOM, and >>> was a later name for /deep/. All three are gone, as lesson 6.3 said. Chrome 63, in 2017, made /deep/ act like a plain space and ::shadow match nothing. Chrome removed the last of the early shadow DOM code in Chrome 89, in 2021. Today Chrome drops any CSS rule that uses them, and querySelector() throws a SyntaxError.

The CSS Shadow spec explains the choice: >>> showed too much of a component's inside. A page now has three ways in, each with its own lesson: inherited values, custom properties (lesson 12.1), and parts a component names itself (lesson 12.2).

Giving the tea card's buttons the page font

Asha's last problem is in the kit itself. The card's Add button sits in its shadow root. So do the - and + buttons of the quantity part from lesson 9.2. Asha's page already has button { font: inherit; }. It fixes her own buttons, but not the kit's.

This lesson's kit adds one line to the end of each component's <style>: button { font: inherit; } in tea-card.js, and the same in tea-quantity.js. Switch between the two kits:

Playground: the kit's buttons, before and after
A Georgia page with a kit cardPop out in the code editor (opens in a new tab)HTML
<style>  body { font: 20px Georgia, serif; }  button { font: inherit; }</style><button>Ask Ravi</button><tea-card name="Green tea" price="4"></tea-card><script type="module">  import "./tea-kit/index.js";   const add = document.querySelector("tea-card").shadowRoot.getElementById("add");  for (const button of [document.querySelector("button"), add]) {    console.log(button.textContent, getComputedStyle(button).fontSize);  }</script>
ResultLoading…
Try it yourself
The page's reset can't reach the card's buttons.

Ask Ravi 20px. Add 1 13.3333px. The page's button { font: inherit; } gives Ask Ravi the page font. The card's Add button keeps the browser's small control font, 13.3333px.

The page stays the same. Only the kit changes: open the tea-kit/tea-card.js and tea-kit/tea-quantity.js tabs and look at the end of each style.
  • Line 2 is Asha's 20px Georgia font. Line 3 is her button reset.
  • Line 5 is a page button. Line 6 is a kit card.
  • Line 8 loads the kit. Line 10 finds the card's Add button.
  • Lines 11 to 13 print each button's text and font size.

With the kit from lesson 10.2, it prints Ask Ravi 20px and Add 1 13.3333px. With this lesson's kit, both print 20px, and Add matches Ask Ravi. The - and + buttons change too: look at the result.

Why only buttons? Everything else in the card is text, and text already inherits the page font. That's the habit to keep: let a component's text inherit, and give each form control inside it font: inherit.

Common mix-ups

  • "Page CSS never reaches a component." No. Page selectors stop at a shadow root. But inherited values, like color and font-family, and custom properties flow in.
  • "Backgrounds are inherited, because the page's background shows behind the card." No. background-color isn't inherited. The inner elements are transparent, so what's behind them shows through.
  • "all: revert is a stronger all: initial." No. revert falls back to the browser's own styles, and inherited properties keep coming from the page.
  • "all: initial resets everything." No. It never resets custom properties, direction, or unicode-bidi.
  • "A link in a shadow root waits for its file, like one in the head." No. It doesn't hold back the paint, so the card can show plain until the file arrives.
  • "A relative href in a component's link is read against the component's file." No. It's read against the page's address. Use new URL("tea-card.css", import.meta.url).
  • "/deep/ and >>> still reach into shadow roots." No. Chrome drops rules that use them, and querySelector() throws a SyntaxError.

Practice exercises

Answer first, then use the hints. The pages run live, so you can check yourself.

Exercise 1 · Warm-upA color and a padding

The page gives the card a color and a padding. What does the console print for the name inside the card?

<style>  tea-card { color: saddlebrown; padding: 8px; }</style><tea-card></tea-card><script src="tea-card.js"></script><script>  const name = document.querySelector("tea-card").shadowRoot.querySelector("p");  const style = getComputedStyle(name);  console.log(style.color, style.paddingTop);</script>
ResultLoading…

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

    Exercise 2 · Warm-upOld selectors in a new browser

    The style sheet has three rules. How many does the browser keep?

    <style>  tea-card /deep/ p { color: crimson; }  tea-card::shadow p { color: crimson; }  p { color: saddlebrown; }</style><script>  console.log(document.querySelector("style").sheet.cssRules.length);</script>
    ResultLoading…

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

      Exercise 3 · PracticeAn input that matches the page

      The card's note box ignores the page's 20px Georgia font. Fix it in the tea-card.js tab. What does your fixed page print?

      <style>  body { font: 20px Georgia, serif; }</style><tea-card></tea-card><script src="tea-card.js"></script><script>  const box = document.querySelector("tea-card").shadowRoot.querySelector("input");  console.log(getComputedStyle(box).fontSize);</script>
      ResultLoading…

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

        Exercise 4 · PracticeThe reset that ate the border

        The card should be a block with a sea green border. It shows neither. What does the starter print? Then fix line 7 in the tea-card.js tab.

        <p>Our teas</p><tea-card></tea-card><script src="tea-card.js"></script><script>  console.log(getComputedStyle(document.querySelector("tea-card")).display);</script>
        ResultLoading…

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

          Exercise 5 · ChallengeA reset the page can't beat

          The card resets everything, yet its name is dark orange. Make the reset win without touching the page. What does your fixed page print?

          <style>  tea-card { color: darkorange; }</style><tea-card></tea-card><script src="tea-card.js"></script><script>  const name = document.querySelector("tea-card").shadowRoot.querySelector("p");  console.log(getComputedStyle(name).color);</script>
          ResultLoading…

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

            Exercise 6 · ChallengeMove the styles into a file

            Keep the card looking the same, but load its CSS from tea-card.css with a link instead of a <style>.

            <template id="card-template">  <style>    .name { color: seagreen; font-weight: bold; }  </style>  <p class="name">Green tea</p></template><tea-card></tea-card><script src="tea-card.js"></script>
            ResultLoading…

              Check your understanding

              For each question, ask yourself: does a selector have to reach in, or does a value flow in by inheritance?

              The shadow root styles quiz · 7 questionsScore: first tries count
              1. Question 1 of 7The page has tea-card { color: saddlebrown; border: 2px solid crimson; }. What does a paragraph inside the card's shadow root get?

                Choose an answer to see the explanation.

              2. Question 2 of 7Which page rule can change how the card's inner .name paragraph looks?

                Choose an answer to see the explanation.

              3. Question 3 of 7What does this all: revert card print?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <style>
                  body { color: saddlebrown; }
                </style>
                <tea-card></tea-card>
                <script>
                  const root = document.querySelector("tea-card").attachShadow({ mode: "open" });
                  root.innerHTML = "<style>:host { all: revert; }</style><p>Green tea</p>";
                  console.log(getComputedStyle(root.querySelector("p")).color);
                </script>

                Choose an answer to see the explanation.

              4. Question 4 of 7A site's stylesheet has button { font: inherit; }. Why does the Add button inside the tea card still use a small, plain font?

                Choose an answer to see the explanation.

              5. Question 5 of 7The card's style has :host { all: initial; display: block; }. What still comes in from the page?

                Choose an answer to see the explanation.

              6. Question 6 of 7A component adds <link rel="stylesheet" href="tea-card.css"> to its shadow root. Does the browser wait for the file before it draws the card?

                Choose an answer to see the explanation.

              7. Question 7 of 7Inside a shadow root, is .name a safe class name?

                Choose an answer to see the explanation.

              Key takeaways

              • Put a component's CSS in its shadow root: a <style>, or a <link rel="stylesheet"> to a file. shadowRoot.styleSheets lists them.
              • Rules in a shadow root match only that root, and page rules match only the page. Short selectors are safe.
              • A link in a shadow root doesn't hold back the paint, and a relative href is read against the page's address.
              • Inherited properties, like color, font-family, and line-height, flow in along the flat tree. border, background-color, and padding don't.
              • all: initial starts fresh, but keeps custom properties and the direction. Write it first. all: revert doesn't stop inheritance.
              • Buttons and inputs don't inherit the font. Give them font: inherit inside the component.

              Remember the one-liner.
              A shadow root brings its own CSS and keeps it to itself. Only inherited values cross, and all: initial stops even those.

              Sources

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

              Coming next: Styling the host. It styles the card itself with :host, the selector this lesson's resets used, and reacts to its attributes with :host().

              CompleteFrontend Clear concepts. Working examples.