HTML & CSS
/Advanced
Specificity
Definition
The algorithm used by browsers to determine which CSS property value is the most relevant to an element and therefore applied when multiple conflicting rules exist.
Explain Like I'm New
The CSS Point System. If you tell an element to be Red, but another rule tells it to be Blue, who wins? The browser calculates points. IDs are worth 100 points. Classes are worth 10 points. Elements are worth 1 point. The rule with the highest score wins the fight.
Real World Example
You write `.btn { color: red; }` but the button stays blue. You inspect it and realize a third-party library wrote `#main-form .btn { color: blue; }`. Because they used an ID (100 pts), their rule overpowered yours (10 pts).
Common Use Cases
- •Debugging 'Why isn't my CSS working?!'
- •Writing maintainable code
Interactive Example
Interview Questions
basic
- Which has higher specificity: a Class (`.card`) or an ID (`#header`)?
intermediate
- What is `!important` and why should you avoid it?