Specificity is how the cascade ranks two declarations that tie on origin, context, inline attachment and layer. Selectors Level 4 computes it as a triple:
- A: ID selectors (
#nav). - B: class selectors (
.btn), attribute selectors ([type=text]) and pseudo-classes (:hover). - C: type selectors (
a) and pseudo-elements (::before).
The universal selector * and combinators ( , >, +, ~) count nothing. Compare A first; only on a tie compare B, then C. So #a (1,0,0) beats .a.b.c.d.e.f.g.h.i.j.k (0,11,0): there is no carrying from B into A. Repeating a selector counts again, so .btn.btn is (0,2,0).
The functional pseudo-classes are special:
:is(),:not()and:has()take the specificity of the most specific selector in their argument, whichever one matched.:is(#nav, .menu) ais (1,0,1) even on an element that only matches through.menu.:where()is always (0,0,0), whatever it contains. That makes it the tool for overridable defaults.:nth-child(An+B of S)counts as one pseudo-class plus the most specific selector inS.
A selector list on a rule is different: #x, p gives each element the specificity of the most specific selector that matched it, so a <p> gets (0,0,1).
The style attribute isn't compared by specificity. The 2013 CSS Style Attributes spec phrased it as "higher than any selector"; Cascade Level 5 makes it a separate cascade step, element-attached styles, decided before layers and specificity.
Specificity only breaks ties. A declaration in a later layer, an !important one, or one in a different origin wins without specificity being looked at.