Understand Css Specificity Algorithm. đź’ˇ

Understand Css Specificity Algorithm. đź’ˇ

·

4 min read

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 :

SELECTOREXAMPLEPRIORITY POINT
Inline Styles<h1 style="color: pink;">1000 Points
ID Selectors#header100 Points
Classes, Attributes and pseudo-classes.menu , [attribute=value] , :hover10 Points
Elements and pseudo-elementsh1 , ::before , ::after1 Point

Now, let’s revisit the above example to see why #header has the highest specificity.

More Specificity Rules Examples 👇

  1. Equal Specificity
    When selectors have equal specificity,then the rule defined last in the CSS code takes precedence.

  2. ID vs Class
    Selectors with IDs have higher specificity than class selectors.

  3. Inline Styles
    Inline styles have the highest specificity, overriding all external and internal CSS rules.

  4. Universal Selector
    The universal selector (*) has the lowest specificity and is easily overridden.

  5. Combining Selectors

    When we combining multiple selectors then the specificity also increases.

  6. ID + Class Combination
    Combing ID to a Class based selector also increases specificity.

  7. 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

  1. 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;
     }
    
  2. Avoid using id selectors for styling, use id’s for javascript.

     /* Avoid */
     #header {
       background-color: red;
     }
    
     /* Better */
     .header {
       background-color: red;
     }
    
  3. 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>
    
  4. Avoid overusing !important, as it overrides all specificity rules and makes debugging difficult.

     /* Avoid */
     .container {
       color: blue !important;
     }
    
     /* Better */
     .container {
       color: blue;
     }
    
  5. 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.

Â