Understanding Specificity
When multiple CSS rules target the same element, specificity determines which rule wins.
What is Specificity?
Loading CSS Playground...
The text is red because ID selectors have the highest specificity!
The Specificity Hierarchy
From lowest to highest:
- Element selectors (p, div, span) - lowest
- Class selectors (.class, [attr], :pseudo) - medium
- ID selectors (#id) - high
- Inline styles - highest
- !important - overrides all
Loading CSS Playground...
Combining Selectors
More specific selectors win:
Loading CSS Playground...
Equal Specificity = Last Wins
Loading CSS Playground...
When specificity is equal, the last rule in the CSS wins.
Class vs Multiple Classes
Loading CSS Playground...
The !important Override
Loading CSS Playground...
Use !important sparingly - it makes CSS harder to maintain!
Inheritance vs Specificity
Loading CSS Playground...
Direct selectors always beat inherited styles, regardless of specificity.
Exercise: Override a Style
Override the ID selector color using a class with !important:
Loading CSS Exercise...

