CSS specificity determines which style rule will apply when multiple rules target the same element. It is essential for understanding stylesheet conflicts.
Specificity is a numeric value that the browser uses to weight rules. Elements, classes, and IDs have different scores.
The following example shows how element, class, and ID selectors take effect.
Specificity values consist of different categories: inline style, ID, class, and element. The higher the score, the stronger the rule.
| Selector | Specificity value |
|---|---|
| Element selector (p, div) | 0-0-1 |
| Class selector (.class) | 0-1-0 |
| ID selector (#id) | 1-0-0 |
| Inline style | 1-0-0-0 (highest) |
The !important declaration overrides all specificity, so it must be used with caution. Use it only if there is truly no other solution.
Managing specificity helps keep the code clean and prevents display errors. Here are some practical tips:
✨ Ask Lara — your AI study partner
Unlock personalized learning support. Lara can explain lessons, summarize topics, and answer your study questions — available from the Go plan and above.
Lara helps you learn faster — exclusive to ReadyTools Go, Plus, and Max members.