CSS Specificity

Specificity is the scoring system browsers use to decide which CSS rule wins when several rules target the same element. More specific selectors override less specific ones.

Web

Roughly, inline styles beat ids, ids beat classes, and classes beat element selectors. When two rules have equal specificity, the one written later in the CSS wins.

Understanding specificity explains why a style 'won't apply' — usually a more specific rule is overriding it. Keeping selectors simple avoids specificity wars.

Examples

CSS

p { color: gray; }       /* low */
.note { color: blue; }   /* higher */
#alert { color: red; }   /* highest */

Output:

A paragraph with id 'alert' and class 'note' renders red, because the id selector wins.

Learn it in a lesson

See also

Back to the Dictionary