Skip to content
IT glossary
FrontendUpdated December 3, 2026

CSS specificity

Специфичность CSS

A CSS selector has a weight called specificity. The browser compares these weights when matching rules set the same property on an element and earlier steps of the cascade haven’t picked a winner.

What specificity decides

A link can match a, .nav-link, and #site-nav a at the same time. If each rule sets color, the browser needs to choose one value. Specificity is the part of the CSS cascade that compares the selectors’ weights. The cascade resolves conflicts one property at a time: a rule that loses for color can still provide the link’s padding.

How to read the score

Write specificity as (IDs, classes, types) and compare the three counts from left to right:

  1. Count ID selectors such as #site-nav.
  2. Count class selectors, attribute selectors, and ordinary pseudo-classes such as .nav-link, [href], and :hover.
  3. Count type selectors and pseudo-elements such as a and ::after.

Stop at the first count that differs. One ID beats any number of classes; there is no carry from one column to the next. The universal selector * and combinators such as > add nothing. :where() always contributes zero, including its arguments. By contrast, :is(), :not(), and :has() take the weight of their most specific argument, even if that argument isn't the one that matched.

Example: why the later rule loses

Suppose <a class="nav-link" href="/help">Help</a> is inside #site-nav:

#site-nav a   { color: navy; }   /* (1, 0, 1) */
a.nav-link    { color: purple; } /* (0, 1, 1) */

Assuming these are ordinary author rules in the same layer, the link is navy. The ID settles the comparison before the browser considers classes or source order. If you change the first selector to :where(#site-nav) a, its weight drops to (0, 0, 1) and the purple rule wins. The first rule still matches the same link; it’s just easier to override.

Debug the cascade before adding weight

A stronger selector is not always the fix. The cascade checks origin, importance, and layer precedence before it compares specificity. For example, a normal rule in a higher-priority layer can beat a rule with an ID in a lower-priority layer. !important changes the declaration’s importance, not the selector’s score. An inline style attribute is not a selector either: normal inline declarations outrank normal declarations in author stylesheets. When specificity ties, @scope proximity can break the tie before source order does.

In browser DevTools, inspect the element and find the property in the Styles panel. Look at the winning declaration and the ones crossed out before changing the selector. For shared component defaults, prefer low-weight selectors so that variants and local overrides do not have to pile on more classes or IDs.

In an interview

If you're asked which declaration wins, first check that both selectors match the element and both declarations set the property in question. Then check whether an earlier cascade rule settles the conflict. If the decision comes down to specificity, give the two scores and point to the first column that differs. “It comes later” only works when the earlier criteria tie.