
Specificity Isn't a Points Total: How the Cascade Really Picks a Winner
- كاتب موثّق
محتويات المقال
#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
!important is the fix.
!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
#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 */#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).
*, 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
Origin and importance: browser defaults, user styles and your styles, with !importantreversing that order.Inline styles: a styleattribute beats any selector of the same importance.Cascade layers: which @layerthe rule lives in.Specificity: the column comparison above. Order of appearance: the last declaration wins.
@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
#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
@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.
/* 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;}@layer legacy, components;@layer legacy { #sidebar a { color: #555; }}@layer components { .btn-primary { color: white; /* 0-1-0, wins: later layer */ }}!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.
!important fixes a losing rule |
How to apply it in your project
Declare the order once Put 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.Move what you don't control down Import resets, vendor CSS and legacy styles into the early layers. Your own component styles then win by layer, not by selector length. Stop styling with IDs Use classes for styling and keep IDs for anchors and labels. Where an ID selector must stay, wrap it in :where()so it weighs nothing.Audit every !important Search 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. !importantreverses those orders; reach for layers and:where()instead of escalating.
التعليقات (0)