Understanding Specificity
When multiple CSS rules target the same element, specificity determines which rule wins.
What is Specificity?
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
Combining Selectors
More specific selectors win:
Equal Specificity = Last Wins
When specificity is equal, the last rule in the CSS wins.
Class vs Multiple Classes
The !important Override
Use !important sparingly - it makes CSS harder to maintain!
Inheritance vs Specificity
Direct selectors always beat inherited styles, regardless of specificity.
Exercise: Override a Style
Override the ID selector color using a class with !important:
Up next · Lesson 14 of 27
Calculating Specificity
3 min read
13 of 27 lessons done

Ask Byte about this lesson
A private chat about this lesson. Byte has read the whole lesson, answers in seconds, and shows the section each answer comes from. Ask follow-ups in any language.
Questions & Answers
Public questions about this lesson. The AI Tutor answers right away in your language, the FreeAcademy team checks answers, and every learner can read them.

