DEV Community

Sanjaya Sahu
Sanjaya Sahu

Posted on

CSS Specificity Explained: Why Your Style Is Not Applying

You wrote the rule, DevTools shows it, and it is crossed out. Specificity is almost always why.

How it is scored

Think of three columns: IDs, classes/attributes/pseudo-classes, elements. #nav .item a is (1,1,1). Higher column wins; ties go to the later rule.

Things people get wrong

  • !important and inline styles sit outside the normal scoring
  • :where() has zero specificity; :is() takes its most specific argument
  • Universal selector * adds nothing
  • Source order only matters after specificity ties

Practical rules

  1. Prefer single-class selectors (0,1,0)
  2. Avoid IDs for styling
  3. Use cascade layers (@layer) so order beats specificity
  4. Never fight specificity with !important; lower the specificity of the rule you want to override

Check it

Paste a selector into the free CSS specificity calculator to see its score instantly. Runs fully in your browser.

More tools: webdevtoolbox.com.

Top comments (0)