Understanding CSS Specificity
CSS specificity determines which CSS rules are applied when multiple rules target the same element. Mastering specificity is essential for writing maintainable, predictable stylesheets.
What is CSS Specificity?
CSS specificity is a set of rules that browsers use to determine which style declarations are most relevant to an element when multiple conflicting rules apply. It's calculated based on the types of selectors used in a CSS rule.
When two or more CSS rules target the same element, the browser uses specificity to decide which rule takes precedence. Higher specificity wins.
How Specificity is Calculated
Specificity is calculated using a four-part value system:
- Inline styles: 1,0,0,0 (highest priority)
- IDs: 0,1,0,0
- Classes, attributes, pseudo-classes: 0,0,1,0
- Elements and pseudo-elements: 0,0,0,1
When comparing specificity, start from the left and compare each value. The first non-equal value determines the winner.
Specificity Examples
Here are examples of how specificity is calculated:
/* Specificity: 0,0,0,1 (one element) */
p { color: red; }
/* Specificity: 0,0,1,0 (one class) */
.intro { color: blue; }
/* Specificity: 0,1,0,0 (one ID) */
#header { color: green; }
/* Specificity: 0,0,1,1 (one class + one element) */
p.intro { color: purple; }
/* Specificity: 0,1,0,1 (one ID + one element) */
#header p { color: orange; }
/* Specificity: 0,0,2,0 (two classes) */
.intro.highlight { color: yellow; }In the example above, if all these rules target the same paragraph, the one with highest specificity wins.
The !important Rule
The !important declaration overrides all other specificity rules, except for other !important declarations with higher specificity.
p {
color: red !important;
}
#header p {
color: blue;
}
/* The paragraph will be red because of !important */While !important can solve specificity issues, it's generally considered bad practice because it makes styles harder to maintain and override. Use it sparingly, if at all.
Common Specificity Scenarios
Understanding specificity helps in common scenarios:
- Component styling: Using classes to style components without conflicts
- Override behavior: Knowing when to increase specificity vs using !important
- Framework integration: Working with CSS frameworks that have their own specificity
- Maintainability: Writing CSS that's easy to understand and modify
Best Practices
To write maintainable CSS with predictable specificity:
- Use classes over IDs: Classes are easier to override and reuse
- Avoid inline styles: They have the highest specificity and are hard to override
- Keep selectors simple: Complex selectors increase specificity unnecessarily
- Use BEM or similar methodologies: They help manage specificity consistently
- Minimize nesting: Deep nesting in preprocessors can create high specificity
Specificity in Modern CSS
Modern CSS features like CSS Grid, Flexbox, and CSS Custom Properties don't change specificity rules, but they do change how we structure our styles. Understanding specificity remains crucial for:
- Working with CSS-in-JS solutions
- Using CSS modules and scoped styles
- Integrating with component-based frameworks
- Debugging style conflicts in complex applications
Debugging Specificity Issues
When styles aren't applying as expected:
- Check browser DevTools to see which rules are being applied
- Calculate the specificity of conflicting rules
- Look for
!importantdeclarations that might be overriding - Verify the selector actually matches the element
- Check for typos in class or ID names
Test Your Knowledge
Ready to test your CSS knowledge? Take our web basics quiz to master CSS specificity and other fundamental web development concepts.
Take Web Basics Quiz

