

How does CSS specificity work?
CSS specificity determines which rule applies when multiple rules target the same element. It is calculated based on selector types: inline styles (highest), IDs, classes/attributes, and elements (lowest). Understanding specificity helps resolve conflicts and maintain clean styles.
Introduction to CSS specificity®
What is Specificity?
CSS specificity is a set of rules that browsers use to determine which CSS property values are the most relevant to an element and, therefore, will be applied. When multiple CSS rules target the same element, the rule with the highest specificity "wins" and is applied.
Why is Specificity Important?
Understanding specificity is crucial for:
Avoiding styling conflicts.
Writing maintainable and predictable CSS.
Debugging why certain styles are not being applied as expected.
2. How Specificity Works
The Cascade in CSS
The "C" in CSS stands for "Cascading," which refers to the process of combining different stylesheets and resolving conflicts between them. The cascade works in three layers:
Importance: Whether a rule is marked as !important.
Specificity: The specificity of the selectors.
Source Order: The order in which rules appear in the stylesheet.
Specificity Hierarchy
Specificity is calculated based on the components of a CSS selector. These components are ranked in the following hierarchy:
Inline Styles: Styles applied directly to an element using the style attribute.
ID Selectors: Selectors that target an element by its id attribute.
Class, Attribute, and Pseudo-Class Selectors: Selectors that target elements by class, attribute, or pseudo-class.
3. Calculating Specificity
Specificity Values
Specificity is often represented as a tuple of four numbers: (a, b, c, d), where:
a: Inline styles (1 or 0).
b: Number of ID selectors.
c: Number of class, attribute, and pseudo-class selectors.
d: Number of element and pseudo-element selectors.
For example:
h1 has a specificity of (0, 0, 0, 1).
.nav-item has a specificity of (0, 0, 1, 0).
#header has a specificity of (0, 1, 0, 0).
style="color: red;" has a specificity of (1, 0, 0, 0).
Specificity Rules
Higher specificity wins: A selector with a higher specificity value will override a selector with a lower specificity value.
Equal specificity: If two selectors have the same specificity, the one that appears later in the stylesheet wins.
Universal selector (*): Has no specificity value (0, 0, 0, 0).
Examples of Specificity Calculations

4. Specificity in Practice
Common Scenarios
Class vs. Element Selector:
The .title class selector wins because it has higher specificity.
The #header ID selector wins because it has higher specificity.
Multiple Selectors:
The .nav .nav-item selector wins because it has higher specificity.
Debugging Specificity Issues
When styles are not being applied as expected:
Use browser developer tools to inspect the element and see which styles are being applied.
Check the specificity of the conflicting selectors.
Avoid using !important unless absolutely necessary.
Best Practices
Use Classes: Prefer class selectors over ID selectors for better reusability and lower specificity.
Avoid Over-Nesting: Deeply nested selectors can lead to high specificity and make your CSS harder to maintain.
5. Advanced Topics
The !important Rule
The !important rule can override specificity. However, it should be used sparingly because it can make debugging difficult and lead to maintenance issues.
Example:
Inline Styles and Specificity
Inline styles have the highest specificity (1, 0, 0, 0) and will override most other styles.
Example:
<h1 style="color: green;" Hello world </h1>
Specificity in CSS Frameworks
CSS frameworks like Bootstrap often use high-specificity selectors. To override them, you may need to use equally high or higher specificity
6. Tools and Resources
Specificity Calculators
Browser Developer Tools
Modern browsers like Chrome, Firefox, and Edge have built-in developer tools that show the specificity of applied styles.
7. Conclusion
Summary
CSS specificity is a fundamental concept that determines which styles are applied to an element when multiple rules conflict. By understanding how specificity works and following best practices, you can write more maintainable and predictable CSS.
Final Thoughts
Mastering specificity is key to becoming proficient in CSS. Practice calculating specificity, use tools to debug issues, and always strive to write clean, low-specificity CSS.
Diagrams and Visual Aids
Specificity Hierarchy Diagram
Specificity Calculation Example
In this example, .nav .nav-item has higher specificity and will be applied.
By following this guide, you should have a solid understanding of CSS specificity and how to use it effectively in your projects. Happy coding!