Three weight categories, compared left-to-right: 0,0,0
1,0,0 always beats 0,500,500
* and :where(<any-selector-here>) add 0,0,0
:is, :not, and :has use the higher specificity in the list of selectors, ignoring if the higher specificity selector is actually on the element
<p> and :is(p, #foo.bar.hello) = 1,2,0h1 { color: silver; background: black } equals:
h1 { color: silver }; = 0,0,1h1 { background: black }; = 0,0,1#pencil = 1,0,0!importance does not add to specificity
Descendants only inherit with the exception of a few properties defined on <body> that are not defined on <html> (e.g., background, overflow)
Cascading Style Sheets
style = element attachedPrecedence order:
Ex:
p { color: black; } (author)p { color: black; } (reader)p { color: black !important; } (author)p { color: black !important; } (reader)Introduced in 2021.
@layer at-rule.@layer defined and highest precedence@layer foo, bar; defines the layers foo and bar, in that order.@layer bar { ... } then @layer foo { ... } doesn’t redefine the order.@layer can be unnamed
Ex:
@layer site { p { color: red; } }@layer page { p { color: blue; } } wins (last)p { color: teal; } wins (implicit = highest precedence)@layer site { p { color: red; } }@layer page { p { color: blue; } }p { color: teal; } wins (implicit = highest precedence; unnamed !== implicit)@layer { p { color: red; } }@layer { p { color: blue; } }Redefine explicit layer order
@layer page, site;@layer site { p { color: red; } } wins (site is last in the definition)@layer page { p { color: blue; } }| Another good read: [Cascade Layers Guide | CSS Tricks](/css-cascade-layers/#test-your-knowledge-which-style-wins) |