Full answer
Specificity is a score written as three numbers: ids, then classes (with attributes and pseudo-classes), then elements (with pseudo-elements). Compare the first number first, then the second, then the third. A bigger earlier number always wins, whatever the later numbers are. If the scores are equal, the later rule in the source wins. Inline styles beat any selector, and !important in a stylesheet beats a normal inline style.
Six cases, with the answer for each:
p { color: red }is (0,0,1) and.note { color: blue }is (0,1,0). Class beats element, so the text is blue.div.mis (0,1,1) and.m.nis (0,2,0). Two classes beat one class and one element, so the winner is the second rule, orange.#e3is (1,0,0) and a selector with eleven classes is (0,11,0). One id beats any number of classes, so purple wins. The numbers do not carry over.section:not(#zzz)counts the id inside:not(), so it is (1,0,1). It beats.rat (0,1,0), even though the id does not exist. The winner is brown.- Two rules with the same selector
.shave equal scores, so the later one wins, navy. - An inline
style="color: black"beats#e6 { color: red }. But#e7 { color: red !important }beats an inline style without!important.
Other points: the universal selector * and combinators add nothing. :is() takes its strongest argument, and :where() adds zero. Pseudo-elements count as elements. Check the number with the browser tools, which show the winning rule.
The browser colours match the answers above: blue, orange, purple, brown, navy, black and red.
Do the counting by hand first, and then check in the browser tools.
<style>
p { color: red; }
.note { color: blue; }
div.m { color: green; }
.m.n { color: orange; }
#e3 { color: purple; }
.q.q.q.q.q.q.q.q.q.q.q { color: teal; }
section:not(#zzz) { color: brown; }
.r { color: blue; }
.s { color: red; }
.s { color: navy; }
#e6 { color: red; }
#e7 { color: red !important; }
</style>
<p id="e1" class="note">1</p>
<div id="e2" class="m n">2</div>
<div id="e3" class="q">3</div>
<section id="e4" class="r">4</section>
<div id="e5" class="s">5</div>
<div id="e6" style="color: black">6</div>
<div id="e7" style="color: black">7</div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Specificity is (ids, classes, elements), compared left to right.
- One id beats any number of classes.
- Equal scores: the later rule wins.
- :not() and :is() count their strongest argument; :where() counts zero.
- Inline beats selectors; !important in a stylesheet beats normal inline.
Common mistakes
- Thinking that eleven classes beat one id.
- Forgetting that the argument of :not() counts.
- Forgetting that source order only matters when scores are equal.
Follow-ups
- What is the specificity of :where(#a) p?
- Does the universal selector add specificity?
Tips
- Say how to compare: left to right, the first difference decides.
- Work one example aloud, such as the :not(#id) case.