Skip to content

CSS Specificity War

Quick summary

Your CSS rule is being overridden by a more specific rule, and you cannot figure out why your styles are not applying.

Why this happens

  • An ID selector (1-0-0) overrides your class selector (0-1-0)

  • Inline styles override your stylesheet rules

  • A framework or library uses highly specific selectors

  • Nested selectors create unintended high specificity

Minimal example

✗ Broken code
/* Framework: specificity 1-0-0 */
#header { color: black; }
/* Your rule: specificity 0-1-0 — loses */
.header-text { color: blue; }
✓ Fixed code
/* Match specificity: 1-1-0 */
#header .header-text { color: blue; }
/* Or better: remove the ID selector */
.header { color: blue; } /* If you control the HTML */

The ID selector (1-0-0) beats the class selector (0-1-0). Either increase your specificity or, better, replace the ID with a class.

How to diagnose

  • Inspect the element in DevTools — look for struck-through rules

  • Check the specificity of competing rules

  • Look for inline styles on the element

How to fix

  • Increase the specificity of your selector to match or exceed the competing rule

  • Remove unnecessary ID selectors in favor of classes

  • Use a CSS methodology like BEM to keep specificity low and consistent

How to prevent

  • Use BEM or another CSS methodology to avoid specificity issues

  • Prefer classes over IDs for styling

  • Avoid inline styles except for dynamic values from JavaScript

Related Resources

Related Glossary

Related Lessons

← Back to language