Understanding Specificity: The Point System
You'll learn: How CSS assigns point values to selectors to determine which styles win when multiple rules target the same element.
The CSS Scoring System
When multiple CSS rules apply to the same element, the browser needs a tiebreaker. CSS uses a point system to calculate each selector's specificity — think of it like a ranking that determines which rule is "stronger."
The point system works on a simple hierarchy with four categories (from most to least powerful):
- Inline styles (written directly in HTML): Worth 1,0,0,0 points
- ID selectors (
#header): Worth 0,1,0,0 points each - Class selectors, attribute selectors, pseudo-classes (
.button,[type="text"],:hover): Worth 0,0,1,0 points each - Type selectors and pseudo-elements (
div,p,::before): Worth 0,0,0,1 points each
The universal selector (*) and combinators (>, +, ~) have zero points — they don't add to specificity.
Calculating Specificity
Add up points in each category. For example:
#nav .menu a= 0,1,1,1 (one ID, one class, one type).header p= 0,0,1,1 (one class, one type)div= 0,0,0,1 (one type)
The selector with higher points in the leftmost category wins. If tied, move right to compare the next category. Same specificity? The last rule written in your CSS wins.
Why This Matters
Understanding this point system helps you predict which styles will apply and avoid writing overly-specific selectors just to override existing rules.
Key Takeaway: CSS specificity uses a four-category point system where IDs beat classes, classes beat types, and inline styles beat everything — helping the browser decide which conflicting rule wins.