Have you ever wondered why sometimes your CSS doesn’t apply to an element the way you expect it to? It might seem like other CSS properties are overriding your styles. This happens because of the CSS specificity algorithm. In this article, we’ll explore how the CSS specificity algorithm works so you can better understand why your CSS behaves the way it does.
What is CSS Specificityâť“
CSS specificity is the algorithm browsers use to determine which CSS properties to apply to an element. It calculates the weight of CSS selectors to identify which selector has higher precedence when multiple selectors target the same element. The selector with the higher weight overrides the properties of the lower-weight selectors.
Here's an example to explain CSS specificity clearly :
<div id="header" class="menu">
Welcome to My Website!
</div>
div {
color: blue;
}
.menu {
color: green;
}
#header {
color: red;
}
The browser applies the color: red;
property to the <div>
because #header
has the highest specificity.
How CSS Specificity is Calculated âť“
The browser is calculating the CSS specificity by counting the weight of the selectors using a point-based system.
Each Type of selectors has a specific weight :
SELECTOR | EXAMPLE | PRIORITY POINT |
Inline Styles | <h1 style="color: pink;"> | 1000 Points |
ID Selectors | #header | 100 Points |
Classes, Attributes and pseudo-classes | .menu , [attribute=value] , :hover | 10 Points |
Elements and pseudo-elements | h1 , ::before , ::after | 1 Point |
Now, let’s revisit the above example to see why #header
has the highest specificity.
More Specificity Rules Examples 👇
Equal Specificity
When selectors have equal specificity,then the rule defined last in the CSS code takes precedence.ID vs Class
Selectors with IDs have higher specificity than class selectors.Inline Styles
Inline styles have the highest specificity, overriding all external and internal CSS rules.Universal Selector
The universal selector (*
) has the lowest specificity and is easily overridden.Combining Selectors
When we combining multiple selectors then the specificity also increases.
ID + Class Combination
Combing ID to a Class based selector also increases specificity.Attribute Selectors
Attribute selectors have the same specificity as a class selector. So latest css rules comes has high precedence.
!important
Exception
When CSS properties marked as !important
it overrides the any other declaration it targets, regardless of specificity. However, using !important
is generally considered bad practice as it breaks the natural cascading behavior of CSS and makes stylesheets harder to maintain. It should only be used as a last resort when you absolutely need to override styles that you cannot modify directly.
Best Practices To Avoid Specificty Issues
Keep simple selectors i.e. use class selectors instead of IDs or overly specific selectors.
/* Avoid */ div.container ul li a { color: blue; } /* Better */ .link { color: blue; }
Avoid using id selectors for styling, use id’s for javascript.
/* Avoid */ #header { background-color: red; } /* Better */ .header { background-color: red; }
Avoid inline styles because it have the highest specificity and can make your CSS harder to override and maintain.
<!-- Avoid --> <div style="color: red;"></div> <!-- Better --> <div class="text-red"></div>
Avoid overusing
!important
, as it overrides all specificity rules and makes debugging difficult./* Avoid */ .container { color: blue !important; } /* Better */ .container { color: blue; }
Use a CSS reset or normalize stylesheet to ensure a consistent starting point for all browsers.
* { margin: 0; padding: 0; box-sizing: border-box; }
Summary
In this article, we explored how CSS specificity works and how the browser determines which CSS rules are applied when multiple rules target the same element. We discussed the specificity algorithm and the point-based system that assigns weights to different types of selectors, such as element selectors, class selectors, and ID selectors. Understanding how specificity is calculated helps prevent common styling issues and makes your CSS more maintainable.
We also covered best practices to avoid specificity problems, including keeping selectors simple, avoiding inline styles, and minimizing the use of !important
. By following these strategies, you can ensure your CSS is both effective and scalable. Additionally, we provided examples to demonstrate how equal specificity, inline styles, and ID selectors affect the application of CSS rules.
In conclusion, mastering CSS specificity is crucial for writing clean, efficient styles that work predictably across different projects.