completefrontend
Open lab My progress
THE WEB COMPONENTS FIELD GUIDE

The shadow cascade and which styles win

Learn how shadow DOM CSS specificity really works: why page styles beat :host and ::slotted(), how !important flips the order, and where cascade layers fit.

By the end, you can
  • 01
    Predict which rule winsKnow that the page beats :host and ::slotted() for normal rules, and that !important flips the order.
  • 02
    Score :host and ::slotted()Work out their specificity, and know that it only compares rules from the same tree.
  • 03
    Order a component's own stylesUse @layer inside a shadow root, and keep !important for must-rules like hidden.

Why do page styles beat :host in shadow DOM?

Leaf and Kettle's menu page uses the tea kit. Asha builds the cards. The site's designer writes the page's style sheet. One morning, the designer adds a line: tea-card { color: crimson; }. Every card turns crimson.

Asha wanted seagreen, and her card's shadow root says so. So she makes her rule more specific. She puts a tag, a class, and an attribute in :host(). The card stays crimson. Why does the page's plain rule win? And can a component ever insist?

When the page and a shadow root style the same element, the CSS cascade asks which tree each rule comes from before it compares specificity. When both rules are normal, the page wins, whatever the specificity. When both are !important, the component wins. Specificity only ranks rules from the same tree.

Here is Asha's card:

A very specific :host() rule against the page's tea-card rulePop out in the code editor (opens in a new tab)HTML
<style>  tea-card { color: crimson; }</style><tea-card class="new" kind="green">Green tea</tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
ResultLoading…
  • Line 2 is the designer's rule. It makes every card crimson.
  • Line 4 is the card, with the class new and kind="green".
  • In the tea-card.js tab, line 7 is Asha's rule. It matches a tea-card host with that class and that attribute. It asks for seagreen.
  • Back in the page, line 8 prints the card's final color, from getComputedStyle().

It prints Color: rgb(220, 20, 60), which is crimson. The page won.

You have seen this three times now. In lesson 11.1, a page rule for tea-card beat a reset on :host. In lesson 11.2, the page's rule for the badge beat a more specific :host() rule. In lesson 11.3, the page's rules beat ::slotted(), one property at a time. Each time, something came before specificity. Here is the whole list.

When two rules set the same property on the same element, the browser asks six questions, in order. The first question that tells the two rules apart decides. Here are the six, for Asha's rule and the designer's:

The six steps of the cascade, top to bottom, for the page's tea-card rule against the card's :host() rule. The steps are origin and importance, context, style attribute, layers, specificity, and order of appearance. Step 1 is a tie because both are normal author rules. Step 2 picks the page, the outer context. Steps 3 to 6 are crossed out, never reached for these two rules.the page's tea-card rule against the card's :host() rule1. origin and importancea tie: both normal author rules2. contextthe page wins: outer context3. style attribute4. layers5. specificity6. order of appearancenever reached for these two
The browser asks each question in turn, and the first one that tells two rules apart decides. For the page's tea-card rule and the card's :host() rule, step 2 decides. Specificity, step 5, never runs.
Real-life analogyAlphabetical order in a phone book

In a phone book, Amir comes before Zoe. Nobody compares the rest of the two names. A comes before Z, so the first letter decides. You only look at the next letter when the first ones match, as in Ravi and Rosa.

In real life: The first letter of a name
In JavaScript: Step 1, origin and importance
In real life: The second letter
In JavaScript: Step 2, context: the page or a shadow root
In real life: A letter far along the name
In JavaScript: Step 5, specificity
In real life: Amir before Zoe, whatever comes after A and Z
In JavaScript: The page before the card, whatever the scores
The main idea

The cascade asks its questions in a fixed order, and the second one is about trees. When both rules are normal, the page beats the component. When both are !important, the component beats the page. An !important rule beats a normal one from either side. Specificity comes later, and only ranks rules from the same tree.

In what order does the CSS cascade compare rules?

The cascade works on declarations. A declaration is one property and its value, like color: crimson. CSS Cascade Level 5 sorts two declarations for the same property with these six steps:

  1. Origin and importance. Who wrote it: the browser, the reader, or the author. And does it end in !important? Lesson 11.2 met these origins. Your page and your shadow roots are both author styles.
  2. Context. Which tree is it from: the page, or a shadow root?
  3. Style attribute. Is it written on the element itself, in style=""?
  4. Layers. Which @layer is it in?
  5. Specificity. How specific is its selector?
  6. Order of appearance. Which one comes last?

The browser goes down the list. If one declaration is ahead at a step, it wins, and the rest of the list is skipped. Only a tie moves on to the next step. MDN's introduction to the cascade explains the steps well, but it leaves out step 2. It doesn't cover shadow DOM.

What is a context?

A context is the tree a rule comes from. The page is one context. Each shadow root is another one, inside it. So the page is the outer context, and the card's shadow root is an inner one. The spec's context step has two halves:

  • for normal declarations, the one from the outer context wins;
  • for !important declarations, the one from the inner context wins.

A rule's context is the tree its style sheet is in. The designer's tea-card rule is in the page. :host and ::slotted() rules are in the card's shadow root. That's why the page won in all three lessons. The CSS Shadow spec points to this same step for shadow trees.

The spec gives the reason in a note. A component's normal rules set defaults that the page can easily change. Its !important rules are requirements that the page can't change.

Steps 3 to 6 only ever compare declarations from the same tree. Once step 2 has picked a tree, nothing later can change the result. Not a style attribute, not a layer, and not a high score.

What if a component sits inside another component?

Contexts nest, the way the trees do. A card can sit inside another component's shadow root. Then that shadow root is the card's outer context:

A card inside a panel's shadow rootPop out in the code editor (opens in a new tab)HTML
<tea-panel></tea-panel><script src="tea-card.js"></script><script src="tea-panel.js"></script><script>  const panel = document.querySelector("tea-panel");  const card = panel.shadowRoot.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
ResultLoading…
  • Line 1 of the page is a panel. Lines 2 and 3 load the two classes.
  • In the tea-panel.js tab, line 7 makes every tea-card in the panel's shadow root dark orange. Line 9 is a card in that root.
  • In the tea-card.js tab, line 7 asks for seagreen with :host.
  • Back in the page, lines 5 and 6 find the card in the panel's shadow root. Line 7 prints its color.

It prints Color: rgb(255, 140, 0), dark orange. The panel's root holds the card, so it is the card's outer context. The page can't reach this card at all. Selectors stay in their own tree, as lesson 6.3 showed.

Why it matters: a component's normal styles are defaults for whoever holds it. That might be the page, or another component.

How does !important work with shadow DOM?

!important at the end of a declaration makes it important. Step 1 puts important declarations above normal ones. Then step 2 flips for important ones: the inner context wins. Try all four mixes, on the host and on a slotted paragraph:

Playground: the cascade explorer
A page rule and a card rule for the same elementPop out in the code editor (opens in a new tab)HTML
<style>  tea-card { color: crimson; }</style><tea-card>Green tea</tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
ResultLoading…
Try it yourself
Which element do both rules style?
The page's rule, line 2
The card's rule, line 7 of tea-card.js
The page's rule wins.

Color: rgb(220, 20, 60). Both rules are normal, so step 1 can't choose. Step 2 can: the page is the outer context, and for normal rules the outer one wins.

Change one setting at a time. The page's rule always sets crimson, and the card's rule always sets seagreen. Open the tea-card.js tab to see line 7.
  • Both normal: crimson. Step 2 picks the page.
  • The page's rule !important: crimson. Step 1 decides.
  • The card's rule !important: seagreen. Step 1 decides, for the card this time.
  • Both !important: seagreen. Step 2 flips, and the inner context wins.

Switch to the slotted paragraph. The four colors come out the same. A ::slotted() rule lives in the card's shadow root too. The page's p rule is in the outer context.

Read each row across: the two rules, the card's color, and the step that decided it.

Four mixes of normal and !important
The page's ruleThe card's ruleThe card's colorWhich step decides
tea-card { color: crimson; }:host { color: seagreen; }Crimson, rgb(220, 20, 60).Step 2. Both are normal, so the outer context, the page, wins.
tea-card { color: crimson !important; }:host { color: seagreen; }Crimson, rgb(220, 20, 60).Step 1. The !important rule beats the normal one.
tea-card { color: crimson; }:host { color: seagreen !important; }Seagreen, rgb(46, 139, 87).Step 1. The !important rule beats the normal one.
tea-card { color: crimson !important; }:host { color: seagreen !important; }Seagreen, rgb(46, 139, 87).Step 2. Both are !important, so the inner context, the card, wins.

Look at rows 2 and 3. Step 1 decides them, so the trees never matter. Only rows 1 and 4 reach the context step.

Real-life analogyHouse rules at Leaf and Kettle

Customers at Leaf and Kettle ask for their tea their way: less sugar, more milk, a bigger cup. The shop says yes, even when its usual way is different. But a few rules are posted by the door, like No pets in the kitchen. Those win, however hard a customer insists.

In real life: A customer's request
In JavaScript: The page's normal rule
In real life: The shop's usual way
In JavaScript: A normal :host or ::slotted() rule
In real life: A rule posted by the door
In JavaScript: A :host rule with !important
In real life: A customer who insists
In JavaScript: The page's !important, which still loses to the posted rule
Five cards stacked from strongest at the top to weakest at the bottom, with an arrow pointing up labelled stronger. From the top: :host !important, the card's must-rule; tea-card !important, the page's must-rule; tea-card, the page's normal rule; :host, the card's normal rule; and the browser's defaults.stronger:host !importantthe card's must-ruletea-card !importantthe page's must-ruletea-cardthe page's normal rule:hostthe card's normal rulebrowser defaultsthe browser's own sheet
Strongest first. Among !important rules, the card's comes first. Among normal rules, the page's comes first. The browser's own defaults sit under both.

Why it matters: a component's normal rules are defaults, and the page can change every one of them. An !important rule inside a component is one the page can never change, not even with its own !important. Chrome's styling guide says to keep it for structural fixes, not for looks. The kit uses it for exactly that, at the end of this lesson.

::part(), which lesson 2.3 met, follows the same rule. Chrome's article on CSS names lists the order. A page's ::part() rule beats the component's normal rule for that part, and loses to its !important one. Lesson 12.2, Styling parts with ::part and exportparts, covers parts.

What is the specificity of :host and ::slotted()?

Specificity is a score for a selector. The cascade uses it at step 5, for rules that tied at every step before. The Selectors spec writes it as three numbers, like 0-1-0:

  • the first counts IDs, like #menu;
  • the second counts classes, attributes, and pseudo-classes, like .new, [kind], and :hover;
  • the third counts tags and pseudo-elements, like p and ::before.

Scores compare like names in the phone book: the first number first, and the next one only on a tie. A pseudo-class is a selector that starts with one colon, like :hover. A pseudo-element starts with two, like ::before.

The CSS Shadow spec scores the shadow selectors this way:

  • :host counts as one pseudo-class: 0-1-0.
  • :host(...) counts as a pseudo-class, plus whatever is in the brackets. Lesson 11.2 used this: :host([hidden]) scores 0-2-0, so it beats :host.
  • ::slotted(...) counts as a pseudo-element, plus whatever is in the brackets. The spec says so too. ::slotted(p) scores 0-0-2.

Read each row across: a selector inside the card, its score, and what counts toward it.

Specificity of the shadow selectors
Selector in the shadow rootSpecificityWhat counts
:host0-1-0One pseudo-class.
:host(.new)0-2-0The pseudo-class, plus a class.
:host([kind="green"])0-2-0The pseudo-class, plus an attribute.
:host(tea-card.new[kind="green"])0-3-1The pseudo-class, plus a class, an attribute, and a tag.
::slotted(p)0-0-2A pseudo-element, plus a tag.
::slotted(.note)0-1-1A pseudo-element, plus a class.
::slotted(p.note)0-1-2A pseudo-element, plus a tag and a class.
slot:not([name])::slotted(p)0-1-3The slot tag and its :not([name]), plus the pseudo-element and a tag.

Take Asha's selector, :host(tea-card.new[kind="green"]), and count. :host itself is a pseudo-class: 0-1-0. In the brackets, the class .new and the attribute add two more to the middle number. The tag tea-card adds one to the last. Together, that's 0-3-1.

Scores never meet across a context

That 0-3-1 is far higher than the designer's tea-card, which scores 0-0-1. The page still won, at step 2. Change line 2 of the first example to :where(tea-card). :where() matches like its argument but scores 0-0-0. The card is still crimson.

Scores decide inside one context

Inside one shadow root, specificity works the way you know:

Two pairs of rules inside one shadow rootPop out in the code editor (opens in a new tab)HTML
<tea-card kind="green">  <p class="note">Grassy and fresh.</p></tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("Card:", getComputedStyle(card).color);  console.log("Note:", getComputedStyle(card.querySelector("p")).color);</script>
ResultLoading…
  • Line 1 of the page is a card with kind="green". Line 2 is a note with the class note.
  • In the tea-card.js tab, line 7 is :host([kind="green"]), 0-2-0. Line 8 is plain :host, 0-1-0.
  • Line 9 is ::slotted(p.note), 0-1-2. Line 10 is ::slotted(p), 0-0-2.
  • Back in the page, lines 7 and 8 print the card's color and the note's.

It prints Card: rgb(46, 139, 87) and Note: rgb(46, 139, 87). Lines 8 and 10 of tea-card.js come later, and they still lose. All four rules sit in one shadow root. So they tie at steps 1 to 4, and specificity decides.

Two windows. The page window holds the rule tea-card, with the score 0-0-1. The shadow root window holds two rules: :host([kind=green]) scoring 0-2-0, and :host scoring 0-1-0, labelled compared: 0-2-0 wins. Under both windows: scores are only compared inside one tree.pagetea-card0-0-1shadow root:host([kind="green"]) 0-2-0:host 0-1-0compared: 0-2-0 winsScores are only compared inside one tree.
Inside the card, 0-2-0 beats 0-1-0. The page's tea-card rule, 0-0-1, is never compared with them: step 2 has already put the page first.
Real-life analogyTicket numbers only count in one queue

At the bank, there are two queues: appointments and walk-ins. Appointments go first. Your ticket number only says where you stand in your own queue. Ticket 1 in the walk-in queue still waits for every appointment.

In real life: The queue for appointments
In JavaScript: The page's normal rules
In real life: The queue for walk-ins
In JavaScript: The card's normal rules
In real life: Your ticket number
In JavaScript: A selector's specificity
In real life: Two tickets in the same queue
In JavaScript: :host([kind]) and :host, both in the card

Why it matters: to make one of your own rules beat another, raise its specificity or move it later. To beat the page, specificity can't help.

Does an inline style attribute beat :host?

A style attribute puts declarations right on an element, like <tea-card style="color: crimson">. Step 3 says these beat rules of the same importance. But step 3 comes after step 2. So which context is the host's style attribute in?

The page's. The host element lives in the page's tree, and so does its attribute. Chrome's article on CSS names says the same: a style attribute belongs where its element belongs. Here is a page's strongest author style, an !important style attribute, against the card's !important:

An !important style attribute against an !important :host rulePop out in the code editor (opens in a new tab)HTML
<tea-card style="color: crimson !important">Green tea</tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
ResultLoading…
  • Line 1 of the page gives the card a style attribute: crimson, with !important.
  • In the tea-card.js tab, line 7 is :host { color: seagreen !important; }.
  • Back in the page, line 5 prints the card's color.

It prints Color: rgb(46, 139, 87), seagreen. Both are !important, so step 1 is a tie. Step 2 picks the inner context, the card. Step 3 never runs.

The other three mixes follow the same first two steps:

  • style="color: crimson" against a normal :host rule: crimson. Step 2 picks the page.
  • style="color: crimson" against :host with !important: seagreen. Step 1.
  • style="color: crimson !important" against a normal :host rule: crimson. Step 1.

In the house rules, a style attribute is a request written on the order itself. It is still a customer's request.

Inside one tree, step 3 works as you know. A style attribute beats the page's own rules, even #menu tea-card.new. And MDN says an !important style attribute beats every other author style. That's true inside one tree. Against a shadow root's own !important rule for its host, it loses. Chrome, Firefox, and Safari agree.

Why it matters: when a component marks a host style !important, no page can change it. Not with a class, an ID, or a style attribute.

Do cascade layers work inside shadow DOM?

Yes, and each shadow root keeps its own. A cascade layer is a named group of rules. You set the order of the groups with @layer: @layer base, theme; means base first, then theme. MDN's @layer page covers the rest:

  • for normal rules, a later layer beats an earlier one, whatever the specificity;
  • rules outside every layer come after all the layers.

Here the page and the card both name their layers, in opposite orders:

The card's own layer order, and the page'sPop out in the code editor (opens in a new tab)HTML
<style>  @layer theme, base;</style><tea-card kind="green">Green tea</tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
ResultLoading…
  • Line 2 of the page declares its layers: theme, then base.
  • In the tea-card.js tab, line 7 declares the card's layers the other way round: base, then theme.
  • Line 8 puts a :host rule in theme: seagreen. Line 9 puts a more specific rule in base: crimson.
  • Back in the page, line 8 prints the card's color.

It prints Color: rgb(46, 139, 87). Inside the card, theme is the later layer, so it wins, though line 9 is more specific. The page's line 2 changed nothing. CSS Cascade 5 says so in a note: layers belong to their origin and context. The page's layer order has no effect on layers with the same names in a shadow root.

Now change the card's three lines:

Playground: layers inside the card
A page with its own layer order, and a card with layersPop out in the code editor (opens in a new tab)HTML
<style>  @layer theme, base;</style><tea-card kind="green">Green tea</tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
ResultLoading…
Try it yourself
The later layer wins.

Color: rgb(46, 139, 87). Inside the card, theme comes after base, so theme wins, though line 9 is more specific. The page's own @layer theme, base; on line 2 changes nothing here.

The page stays the same, with its own @layer theme, base; on line 2. Each choice changes only lines 7 to 9 of the tea-card.js tab.
  • @layer base, theme: seagreen. theme is the later layer.
  • @layer theme, base: crimson. Only the card's own order counts.
  • An unlayered rule: crimson. A normal rule outside every layer beats the layered ones.
  • !important in both layers: crimson. For !important rules, the earlier layer wins. It's the same flip as step 2.
Two windows side by side. The page window declares @layer theme, base; and lists its order: 1. theme, 2. base, 3. unlayered. The shadow root window declares @layer base, theme; and lists its own order: 1. base, 2. theme, 3. unlayered. Under them: each tree sorts its own layers.page@layer theme, base;1. theme2. base3. unlayeredshadow root@layer base, theme;1. base2. theme3. unlayeredEach tree sorts its own layers.
The page and the card each keep their own list. In the card, theme comes after base, so for normal rules a theme rule wins there, whatever the page declares. Unlayered rules come last in both.
Real-life analogyTwo shops, two Specials shelves

Leaf and Kettle shares a street with a bakery. Both shops have a shelf called Specials. The bakery moves its Specials shelf to the front. The tea shop's Specials shelf stays where it was. Same name, different shop.

In real life: The bakery's shelves, in its order
In JavaScript: The page's @layer theme, base;
In real life: The tea shop's shelves, in its order
In JavaScript: The card's @layer base, theme;
In real life: Two shelves called Specials
In JavaScript: Two layers called theme, one in each tree

Can a layer help the page or the card win?

No. Steps 1 and 2 come before layers. A page rule in an early layer, like @layer base { tea-card { color: crimson; } }, still beats the card's unlayered :host rule. And a card's layered !important rule still beats the page's !important.

Why it matters: use layers to order a component's own rules, like a base layer of defaults and a theme layer on top. Then a theme rule wins over a base rule without a longer selector. Chrome's styling guide suggests exactly that. Adobe's tracker of shadow DOM features rates layers as supported per spec, with the tests passing in Chrome, Firefox, and Safari.

Putting the steps together

Sort each pair. In every one, the page and the card set the color of the same card, <tea-card class="new" kind="green">.

Who wins the card's color?
  • tea-card { color: crimson; } against :host(tea-card.new[kind="green"]) { color: seagreen; }
  • :where(tea-card) { color: crimson; } against :host { color: seagreen; }
  • tea-card { color: crimson !important; } against :host { color: seagreen; }
  • tea-card { color: crimson; } against :host { color: seagreen !important; }
  • tea-card { color: crimson !important; } against :host { color: seagreen !important; }
  • tea-card { color: crimson !important; } against @layer theme { :host { color: seagreen !important; } }
  • style="color: crimson !important" against :host { color: seagreen !important; }
  • style="color: crimson" against :host { color: seagreen; }
Try it yourself
0 of 8 correct

Sort each pair of rules by which one sets the card's color: the page's, on the left, or the card's, on the right.

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

Making hidden a must-rule in the tea kit

Lesson 11.2 gave the kit's badge :host([hidden]) { display: none; }, so hidden works though :host sets a display. The kit's tabs have had the same rule since lesson 10.1. Both are normal rules. So what happens when a page sets the badge's display itself?

Playground: a hidden badge, before and after
A hidden kit badge, and a page that sets its displayPop out in the code editor (opens in a new tab)HTML
<style>  tea-badge { display: inline-flex; }</style><p>Green tea <tea-badge kind="green" hidden>New</tea-badge></p><script type="module">  import "./tea-kit/index.js";   const badge = document.querySelector("tea-badge");  console.log("hidden:", badge.hidden, "display:", getComputedStyle(badge).display);</script>
ResultLoading…
Try it yourself
The hidden badge shows.

hidden: true display: inline-flex. The badge's :host([hidden]) rule is normal, and so is the page's tea-badge { display: inline-flex; }. So the page, the outer context, wins, and New shows.

The page stays the same. Only the kit changes: open the tea-kit/tea-badge.js tab to see the badge's rules.
  • Line 2 of the page gives every badge display: inline-flex. A designer might do this to line up an icon.
  • Line 4 is a hidden badge, New, after the tea's name.
  • Line 6 loads the kit. Line 9 prints the badge's hidden property and its display.

With the kit from lesson 11.3, it prints hidden: true display: inline-flex. New shows, though it's hidden. The page's normal rule beats the badge's normal rule at step 2.

This lesson's kit adds !important to that one rule:

The kit badge's styles (tea-kit/tea-badge.js, lines 3 to 9)JavaScript
  <style>
    :host { display: inline-block; padding: 0 6px; border-radius: 8px; background: dimgray; color: white; }
    :host([hidden]) { display: none !important; }
    :host([kind="green"]) { background: darkgreen; }
    :host([kind="oolong"]) { background: sienna; }
    :host([kind="black"]) { background: black; }
  </style>
  • Line 2 here is the badge's normal look, with its display.
  • Line 3 is the changed rule. A hidden badge gets display: none, now with !important.
  • Lines 4 to 6 still give each kind of tea its color.

Now it prints hidden: true display: none. The badge's rule is !important, so it beats the page's normal rule at step 1. A page's !important can't undo it either: for !important rules, the inner context wins. The kit's tea-tabs.js gets the same change.

Everything else stays a default. A page can still set the badge's display while it isn't hidden, or give it any background. Web Awesome's shared host styles have this same rule: :host([hidden]) { display: none !important; }. For colors, a custom property is the usual way to let the page choose. Lesson 12.1, Custom properties as a styling API, covers it.

Common mix-ups

  • "The more specific selector always wins." No. Specificity only ranks rules from the same tree. The context step runs first.
  • "The page wins because its rules come later." No. Order of appearance is the last step. Even a page rule that scores 0-0-0 wins, at step 2.
  • "The page's !important always wins." No. Against the component's own !important, the component wins.
  • "An !important style attribute beats every rule." No. Not a shadow root's !important rule for its host.
  • "My page's @layer order controls the component's layers." No. Each tree keeps its own layer order.
  • "If I put my page rule in an early layer, :host wins." No. The context step comes before layers, so the page still wins.
  • "!important in a component is always wrong." No. It fits must-rules, like hidden. Leave looks as defaults.
  • "A page's ::part() rule always wins." No. The component's !important rule for that part still wins.

Practice exercises

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

Exercise 1 · Warm-upTwo rules inside one card

The card has two rules in its shadow root, and the page has none. What does the console print?

<tea-card class="new">Green tea</tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
ResultLoading…

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

    Exercise 2 · Warm-upA page rule in a layer

    The page puts its rule in a layer called base. The card has a plain :host rule. What does the console print?

    <style>  @layer base { tea-card { color: crimson; } }</style><tea-card>Green tea</tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
    ResultLoading…

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

      Exercise 3 · PracticeLet the theme layer win

      The crimson rule is an old default. Make the theme rule win, in the tea-card.js tab, without deleting a rule or changing a selector. What does your fixed page print?

      <tea-card kind="green">Green tea</tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
      ResultLoading…

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

        Exercise 4 · PracticeA hidden card that shows

        The card is hidden, but it still shows. Fix it in the card, not in the page. What does your fixed page print?

        <style>  tea-card { display: flex; }</style><tea-card hidden>Green tea</tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("display:", getComputedStyle(card).display);</script>
        ResultLoading…

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

          Exercise 5 · ChallengeLet the page choose the color

          The page wants crimson cards, and even tried !important. The cards stay seagreen. Find the bug in the card and fix it. What does your fixed page print?

          <style>  tea-card { color: crimson !important; }</style><tea-card>Green tea</tea-card><script src="tea-card.js"></script><script>  const card = document.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
          ResultLoading…

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

            Exercise 6 · ChallengeA card inside a panel

            The page, the panel, and the card all ask for a color with !important. What does the console print?

            <style>  tea-card { color: crimson !important; }</style><tea-panel></tea-panel><script src="tea-card.js"></script><script src="tea-panel.js"></script><script>  const card = document.querySelector("tea-panel").shadowRoot.querySelector("tea-card");  console.log("Color:", getComputedStyle(card).color);</script>
            ResultLoading…

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

              Check your understanding

              For each question, ask: is either rule !important? If not, which tree is each rule in?

              The shadow cascade quiz · 7 questionsScore: first tries count
              1. Question 1 of 7What does this page print, with a zero-specificity page rule and a very specific :host() rule?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <style>
                  :where(tea-card) { color: crimson; }
                </style>
                <tea-card class="new" kind="green">Green tea</tea-card>
                <script>
                  const card = document.querySelector("tea-card");
                  const root = card.attachShadow({ mode: "open" });
                  root.innerHTML = '<style>:host(tea-card.new[kind="green"]) { color: seagreen; }</style><slot></slot>';
                  console.log(getComputedStyle(card).color);
                </script>

                Choose an answer to see the explanation.

              2. Question 2 of 7The page has tea-card { color: crimson !important; } and the card has :host { color: seagreen !important; }. Which color wins?

                Choose an answer to see the explanation.

              3. Question 3 of 7In CSS Cascade 5's sorting order, where does the context step come?

                Choose an answer to see the explanation.

              4. Question 4 of 7What is the specificity of :host([kind="green"])?

                Choose an answer to see the explanation.

              5. Question 5 of 7The page declares @layer theme, base;. The card's shadow root declares @layer base, theme;. Which order do the card's own layers follow?

                Choose an answer to see the explanation.

              6. Question 6 of 7A page writes <tea-card style="color: crimson !important">, and the card has :host { color: seagreen !important; }. Which wins?

                Choose an answer to see the explanation.

              7. Question 7 of 7Which rule in a component is a good use of !important?

                Choose an answer to see the explanation.

              Key takeaways

              • The cascade asks six questions in order: origin and importance, context, style attribute, layers, specificity, and order of appearance. The first one that tells two rules apart decides.
              • The page is the outer context, and each shadow root is an inner one. For normal rules the outer one wins. For !important rules the inner one wins.
              • So the page beats :host and ::slotted() for normal rules, whatever the specificity.
              • :host scores 0-1-0. :host() adds its argument, and ::slotted() is a pseudo-element plus its argument. Scores only compare rules from the same tree.
              • A host's style attribute is in the page's context. It beats the card's normal rules and loses to its !important ones.
              • Each shadow root keeps its own layer order. The page's @layer order doesn't reach in.
              • Use !important in a component for must-rules like hidden, and leave looks as defaults.

              Remember the one-liner.
              Outside wins normal rules, inside wins !important ones, and specificity only settles ties within one tree.

              Sources

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

              Coming next: Constructable stylesheets. It shares one style sheet between every copy of a component.

              CompleteFrontend Clear concepts. Working examples.