CSS Specificity and the Cascade: Why Your Style Does Not Apply
Learn how CSS specificity is scored, how :is, :not and :where count, and how origins, layers and !important decide which declaration wins.

Every front-end developer has written a rule, refreshed the page and seen nothing change. DevTools shows the declaration struck through, and the question becomes why. The answer is the cascade, and in most cases specificity. This guide explains how both work in modern CSS and how to investigate a stubborn rule with the CSS Specificity Calculator and Cascade Explainer.
How specificity is scored
Specificity is a score of three numbers, written (A, B, C):
- A counts ID selectors such as
#header. - B counts classes (
.btn), attribute selectors ([type=checkbox]) and pseudo-classes (:hover,:focus-visible). - C counts element names (
p,button) and pseudo-elements (::before).
The universal selector * and the combinators (space, >, +, ~) add nothing. Scores are compared column by column: the higher A wins; if A is equal, the higher B wins; and so on. That is why #nav a at (1, 0, 1) beats .menu .item .link.active at (0, 4, 0). One ID outweighs any number of classes.
The modern pseudo-classes
Selectors Level 4 added functions that older calculators often score wrongly:
:is(),:not()and:has()take the specificity of their most specific argument.:is(#app, .shell) .cardcounts as (1, 1, 0) even when the element matched through.shell.:where()always counts zero, whatever is inside. Libraries use it for defaults that any single class can override.:nth-child(2n of .featured)counts as a pseudo-class plus the selector afterof.
In the calculator tab, paste your selectors one per line. Each result shows the score and a row of chips, one for each part, with what that part adds. Tick the sort option to see the strongest selector at the top. Comma lists are split, because each selector in a list is scored separately.
Seeing the shape of a whole stylesheet
Specificity problems are rarely one rule; they are a habit. The stylesheet tab lists every selector with its line number and plots a combined score in source order. Ideally the line rises gently: resets and element styles first, then components, then utilities. Spikes mark selectors such as #main #content .post .meta span that force every later override to be even stronger. Red dots show selectors with IDs. Export the table of the most specific selectors as CSV and you have a refactoring list.
The full cascade, not just specificity
Specificity is only one step. Before it, the browser compares:
- Origin and importance. Transitions win over everything. Then come !important declarations, where browser defaults beat user styles, which beat inline styles, which beat author stylesheets. Then animations, then normal inline styles, then normal author rules.
- Cascade layers. Within author styles, rules in later
@layerblocks win over earlier ones, and unlayered rules win over all layers. For !important declarations this order is reversed, so the earliest layer wins. - Specificity.
- Source order. The later declaration wins.
The cascade tab lets you enter competing declarations for one property with origin, layer, !important, selector and source order, plus your layer order (for example reset, base, components, utilities). The tool sorts them and explains why each one lost.
A worked example
A button refuses to turn white. The rules are #app .btn { color: navy } in the components layer, .text-white { color: white } in utilities, button { color: black } unlayered, and button { color: red !important } in base. Entered into the cascade tab, the red declaration wins: importance is checked before anything else. Remove that !important, and the unlayered button rule wins despite its tiny (0, 0, 1) score, because unlayered styles beat every layer. Move that rule into the base layer and .text-white finally wins, since utilities is the last layer.
Habits that keep specificity low
- Style with single classes; avoid IDs in CSS.
- Wrap resets and library defaults in
:where()or an early layer. - Use layers to express intent instead of escalating selectors.
- Treat !important as a tool for utilities only, and know that it flips layer order.
The calculator does not match selectors against your HTML, so it cannot tell whether a rule applies to an element at all; use the browser's Elements panel for that. What it gives you is the reasoning: which step of the cascade decides, and by how much.



