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.
- 01Predict which rule winsKnow that the page beats
:hostand::slotted()for normal rules, and that!importantflips the order. - 02Score :host and ::slotted()Work out their specificity, and know that it only compares rules from the same tree.
- 03Order a component's own stylesUse
@layerinside a shadow root, and keep!importantfor must-rules likehidden.
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:
<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>- Line 2 is the designer's rule. It makes every card crimson.
- Line 4 is the card, with the class
newandkind="green". - In the
tea-card.jstab, line 7 is Asha's rule. It matches atea-cardhost 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:
tea-card rule and the card's :host() rule, step 2 decides. Specificity, step 5, never runs.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 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:
- 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. - Context. Which tree is it from: the page, or a shadow root?
- Style attribute. Is it written on the element itself, in
style=""? - Layers. Which
@layeris it in? - Specificity. How specific is its selector?
- 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
!importantdeclarations, 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:
<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>- Line 1 of the page is a panel. Lines 2 and 3 load the two classes.
- In the
tea-panel.jstab, line 7 makes everytea-cardin the panel's shadow root dark orange. Line 9 is a card in that root. - In the
tea-card.jstab, 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:
<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>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.
- 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.
| The page's rule | The card's rule | The card's color | Which 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.
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
:hostor::slotted()rule - In real life: A rule posted by the door
- In JavaScript: A
:hostrule with!important - In real life: A customer who insists
- In JavaScript: The page's
!important, which still loses to the posted rule
!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
pand::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:
:hostcounts 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])scores0-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)scores0-0-2.
Read each row across: a selector inside the card, its score, and what counts toward it.
| Selector in the shadow root | Specificity | What counts |
|---|---|---|
:host | 0-1-0 | One pseudo-class. |
:host(.new) | 0-2-0 | The pseudo-class, plus a class. |
:host([kind="green"]) | 0-2-0 | The pseudo-class, plus an attribute. |
:host(tea-card.new[kind="green"]) | 0-3-1 | The pseudo-class, plus a class, an attribute, and a tag. |
::slotted(p) | 0-0-2 | A pseudo-element, plus a tag. |
::slotted(.note) | 0-1-1 | A pseudo-element, plus a class. |
::slotted(p.note) | 0-1-2 | A pseudo-element, plus a tag and a class. |
slot:not([name])::slotted(p) | 0-1-3 | The 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:
<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>- Line 1 of the page is a card with
kind="green". Line 2 is a note with the classnote. - In the
tea-card.jstab, 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.
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.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:
<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>- Line 1 of the page gives the card a style attribute: crimson, with
!important. - In the
tea-card.jstab, 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:hostrule: crimson. Step 2 picks the page.style="color: crimson"against:hostwith!important: seagreen. Step 1.style="color: crimson !important"against a normal:hostrule: 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:
<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>- Line 2 of the page declares its layers:
theme, thenbase. - In the
tea-card.jstab, line 7 declares the card's layers the other way round:base, thentheme. - Line 8 puts a
:hostrule intheme: seagreen. Line 9 puts a more specific rule inbase: 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:
<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>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.
- @layer base, theme: seagreen.
themeis 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
!importantrules, the earlier layer wins. It's the same flip as step 2.
theme comes after base, so for normal rules a theme rule wins there, whatever the page declares. Unlayered rules come last in both.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">.
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; }
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.
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?
<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>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.
- 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
hiddenproperty 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:
<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-0wins, at step 2. - "The page's
!importantalways wins." No. Against the component's own!important, the component wins. - "An
!importantstyle attribute beats every rule." No. Not a shadow root's!importantrule for its host. - "My page's
@layerorder controls the component's layers." No. Each tree keeps its own layer order. - "If I put my page rule in an early layer,
:hostwins." No. The context step comes before layers, so the page still wins. - "
!importantin a component is always wrong." No. It fits must-rules, likehidden. Leave looks as defaults. - "A page's
::part()rule always wins." No. The component's!importantrule for that part still wins.
Practice exercises
Answer first, then use the hints. The pages run live, so you can check yourself.
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>Color: rgb(46, 139, 87), seagreen. Both rules are normal and in the same tree, so steps 1 to 4 tie. At step 5, :host(.new) scores higher, though it comes first.
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>Color: rgb(220, 20, 60), crimson. The page's rule sits in a layer, but layers come after the context step. Both rules are normal, so the page wins at step 2.
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><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>Now the old default sits in base, the first layer, and theme comes after it. For normal rules the later layer wins, whatever the specificity, so the page prints Color: rgb(46, 139, 87).
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><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>An !important declaration beats a normal one at step 1, whichever tree it comes from. So the card's hidden rule now beats the page's display, and the page prints display: none.
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><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>With both rules !important, the inner context, the card, always won. Without it, the page's !important beats the card's normal rule at step 1. The page prints Color: rgb(220, 20, 60).
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>Color: rgb(46, 139, 87), seagreen. The page's selector can't reach into the panel's shadow root. The panel's rule and the card's rule are both !important, so the inner context, the card's own root, wins.
Check your understanding
For each question, ask: is either rule !important? If not, which tree is each rule in?
Question 1 of 7What does this page print, with a zero-specificity page rule and a very specific
:host()rule?Read the code, then predict<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.
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.
Question 3 of 7In CSS Cascade 5's sorting order, where does the context step come?
Choose an answer to see the explanation.
Question 4 of 7What is the specificity of
:host([kind="green"])?Choose an answer to see the explanation.
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.
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.
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
!importantrules the inner one wins. - So the page beats
:hostand::slotted()for normal rules, whatever the specificity. :hostscores0-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
!importantones. - Each shadow root keeps its own layer order. The page's
@layerorder doesn't reach in. - Use
!importantin a component for must-rules likehidden, 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:
- CSS Cascading and Inheritance Level 5: Cascade sorting order, CSS Working Group.
- CSS Cascading and Inheritance Level 5: Context, CSS Working Group.
- CSS Shadow Module Level 1: Shadow trees and the cascade, CSS Working Group.
- Introduction to the CSS cascade, MDN.
- Styling web components, Chrome for Developers.
- Author-defined CSS names and the shadow DOM: how the rule should work with ::part, Noam Rosenthal, Chrome for Developers.
- CSS Shadow Module Level 1: the :host, :host(), and :host-context() pseudo-classes, CSS Working Group.
- CSS Shadow Module Level 1: the ::slotted() pseudo-element, CSS Working Group.
- Selectors Level 4: Calculating a selector's specificity, CSS Working Group.
- CSS Cascading and Inheritance Level 5: Element-attached styles, CSS Working Group.
- CSS Cascading and Inheritance Level 5: Layer ordering, CSS Working Group.
- Modern CSS Feature Support For Shadow DOM: Cascade layers, Adobe Spectrum Web Engineering.
- @layer, MDN.
- Web Awesome's host styles (host.styles.ts, v3.15.0), Web Awesome.
Coming next: Constructable stylesheets. It shares one style sheet between every copy of a component.