Careeroza
Specificity
Calculating Specificity
Specificity is a score with three parts (a, b, c). Compare left to right; the first bigger number wins.
| Selector | Counts as | Example score |
|---|---|---|
inline style | beats all selectors | — |
| ID | a | #nav → (1, 0, 0) |
| class, attribute, pseudo-class | b | .btn:hover → (0, 2, 0) |
| element, pseudo-element | c | ul li::marker → (0, 0, 3) |
*, combinators, :where() | nothing | (0, 0, 0) |
Example
Example
The paragraph is green: the ID selector (1,0,0) beats the class (0,1,0) and the element (0,0,1), even though the element rule comes last:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
#intro { color: #04AA6D; }
.lead { color: tomato; }
p { color: #1971c2; }
</style>
</head>
<body>
<p id="intro" class="lead">Which color am I?</p>
</body>
</html>Result