الانتقال إلى المحتوى
FrontendHTML و CSS· 6 دقائق للقراءة

Specificity Isn't a Points Total: How the Cascade Really Picks a Winner

Specificity is compared column by column, and it's only one of the questions the cascade asks.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

The design system's primary button shows grey text, but only in the sidebar. The culprit is an old rule, #sidebar a { color: #555; }. The developer adds classes until the selector reads .app .sidebar .btn.btn-primary. Still grey. A dozen chained classes: still grey. Then color: white !important, and the button is finally white. Next sprint, someone needs a grey disabled button in the same sidebar and reaches for a second !important.

The common misconception

The belief: specificity is a points total, often taught as 100 per ID, 10 per class and 1 per element. Stack enough classes and you'll outscore an ID. And when a rule loses anyway, !important is the fix.

It's an understandable model, because it gives the right answer in most everyday fights: an ID beats two classes, a class beats an element, and the more specific selector wins. !important also really does win in the moment, so it feels like a fix. The model only breaks at the edges, and that's exactly where the bugs live.

What actually happens

Specificity is three columns, not a sum

The of a selector is written ID-CLASS-TYPE. The first column counts IDs. The second counts classes, attribute selectors and pseudo-classes. The third counts element types and pseudo-elements. Some examples:

CSS specificity.css
#sidebar a { }                      /* 1-0-1 */.app .sidebar .btn.btn-primary { }  /* 0-4-0 */nav > a:hover::before { }           /* 0-1-3 */:where(#sidebar) a { }              /* 0-0-1 */:is(#sidebar, .nav) a { }           /* 1-0-1 */

The browser compares the columns from left to right, and a larger number in a column wins no matter what the columns after it hold. Nothing carries over from one column to the next, so #sidebar a beats .app .sidebar .btn.btn-primary. The points model predicts that twelve classes (120 points) finally beat one ID and an element (101 points). Rendered in headless Chromium, they don't: #sidebar a still beats twelve chained classes (0-12-0).

Some selectors add nothing: the universal selector *, combinators such as > and the space, and :where() with everything inside it. :is(), :not() and :has() count as their most specific argument, so :is(#sidebar, .nav) a weighs 1-0-1 even on an element that only matches .nav.

Specificity is the fourth question, not the first

For ordinary page styles, the cascade answers these questions in order, and moves to the next one only on a tie:

  1. Origin and importance: browser defaults, user styles and your styles, with !important reversing that order.
  2. Inline styles: a style attribute beats any selector of the same importance.
  3. Cascade layers: which @layer the rule lives in.
  4. Specificity: the column comparison above.
  5. Order of appearance: the last declaration wins.

Newer @scope rules add one check, scoping proximity, just before the last step. The point stands: by the time specificity is asked, a lot has already been decided.

Specificity only compares rules that match the element

Inherited values don't compete at all. With #header { color: red; } and a { color: blue; }, a link inside the header is blue: the ID rule matches the header, not the link, and MDN states that a style aimed at the element always beats an inherited one, whatever the inherited rule's specificity.

Layers outrank specificity

A statement like @layer legacy, components; sets the order of . For normal declarations, a later layer wins over an earlier one before specificity is even compared, and styles outside any layer beat every layered style. We checked the extreme case in Chromium: an unlayered a { color: #555; } (0-0-1) beat a layered #sidebar .btn-primary (1-1-0). Inside a layer, specificity still settles ties between rules of that same layer, exactly as before.

That gives the sidebar a real fix:

CSS buttons.css
/* legacy.css, loaded earlier */#sidebar a {  color: #555; /* 1-0-1 */}/* "more classes will beat it": 0-4-0, still grey */.app .sidebar .btn.btn-primary {  color: white;}/* "fixed", until the next override needs one too */.btn-primary {  color: white !important;}

Rendered in headless Chromium, the 0-4-0 rule loses and only the !important one turns the button white. In the layered version, plain .btn-primary wins against the ID rule with no !important at all. If you can't move the old rule into a layer yet, rewriting it as :where(#sidebar) a drops it to 0-0-1, and .btn-primary wins that way too.

MythReality
Specificity is a points totalThree columns compared left to right, with no carrying
Enough classes beat an IDOne more ID beats any number of classes
The most specific selector always winsImportance, inline styles and layers are decided first
A specific rule on a parent beats a generic rule on the childInherited values never beat a rule that matches the element
!important fixes a losing ruleIt moves the fight to another tier, where the next override needs one too

How to apply it in your project

  1. Declare the order oncePut one statement at the top of your entry stylesheet, such as @layer reset, legacy, components, utilities;. Later names win, so utilities override components without any extra specificity.
  2. Move what you don't control downImport resets, vendor CSS and legacy styles into the early layers. Your own component styles then win by layer, not by selector length.
  3. Stop styling with IDsUse classes for styling and keep IDs for anchors and labels. Where an ID selector must stay, wrap it in :where() so it weighs nothing.
  4. Audit every !importantSearch for !important. Each one either becomes a layer decision or gets a comment explaining why it must stay, which is what MDN recommends when it can't be avoided.

Key takeaways

  • Specificity is ID-CLASS-TYPE, compared column by column: one more ID beats any number of classes.
  • The cascade checks importance, inline styles and layers before specificity, and order of appearance breaks the last tie.
  • Inherited values never compete with a rule that matches the element itself.
  • Later layers beat earlier ones regardless of specificity, and unlayered styles beat every layer.
  • !important reverses those orders; reach for layers and :where() instead of escalating.

📚 المصادر

  1. Specificity — MDN
  2. Introduction to the CSS cascade — MDN
  3. @layer — MDN
  4. CSS Cascading and Inheritance Level 5 — W3C

التعليقات (0)