CSS Specificity
In one line
CSS specificity is a scoring system that determines which CSS rule wins when multiple rules target the same element.
In simple words
When multiple CSS rules apply to the same element, the browser needs to decide which one wins. It uses a scoring system called "specificity" — the rule with the highest score takes precedence.
Specificity is calculated as a three-part score (a-b-c): IDs count as (1-0-0), classes/attributes/pseudo-classes count as (0-1-0), and elements/pseudo-elements count as (0-0-1). For example, #header .title has specificity (1-1-0), while div.title has (0-1-1). The higher score wins.
Inline styles (style="...") override everything except !important. The !important flag overrides normal specificity but should be used sparingly. Understanding specificity is essential for debugging why styles are not applying as expected.
Example
/* Specificity: 0-0-1 */
p { color: red; }
/* Specificity: 0-1-0 — wins over p */
.text { color: blue; }
/* Specificity: 1-0-0 — wins over .text */
#intro { color: green; }Three rules with increasing specificity. The ID selector wins and the paragraph is green.
Related terms
Related Resources
Related Lessons
- CSS Selectors
Selectors and specificity.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.