DEV Community

Cover image for CSS Specificity: A Key to Writing Efficient CSS Code
Mohammed Awad
Mohammed Awad

Posted on

CSS Specificity: A Key to Writing Efficient CSS Code

As web developers, we all know that CSS is a fundamental language for styling web pages. But with the increasing complexity of modern web applications, writing efficient and maintainable CSS code has become more important than ever. One of the key concepts to understand in order to achieve this is CSS specificity.

CSS specificity is a set of rules that determines which style rules will apply to an element when multiple styles are defined for that element. In other words, it's a way of resolving conflicts between different style declarations for the same element. The higher the specificity of a selector, the more priority it will be given when applying styles.

There are four different components that determine the specificity of a CSS selector:

  1. Element selectors have a specificity of 1
  2. Class selectors have a specificity of 10
  3. ID selectors have a specificity of 100
  4. Inline styles have a specificity of 1000

for Example :

<!DOCTYPE html>
<html>
<head>
    <title>Example 1</title>
    <style>
        /* Specificity: 1 */
        body {
            background-color: #f0f0f0;
        }

        /* Specificity: 10 */
        .header {
            background-color: #ccc;
        }

        /* Specificity: 100 */
        #main-header {
            background-color: #666;
        }
    </style>
</head>
<body>
    <header class="header" id="main-header"style="background-color: aqua;">
        <h1>Hello World!</h1>
    </header>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

When comparing the specificity of two or more selectors, the component with the highest value is given priority. For example, if one selector uses an ID and another uses a class, the ID selector will have a higher specificity and its styles will be applied.

However, there are some additional rules to consider when calculating specificity. For example, if two selectors have the same specificity, the one that appears later in the CSS file will take precedence. And if a selector contains multiple instances of the same component (e.g. two class selectors), the count is cumulative.

Understanding CSS specificity is important because it helps you to write more efficient and maintainable CSS code. By carefully considering the specificity of your selectors, you can ensure that your styles are applied correctly and avoid conflicts and unexpected behavior.

you can use tools like Specificity Calculator to simplify the process you can calculate the specificity of selectors all the way up to CSS Selectors level 4

In conclusion,
CSS specificity is a fundamental concept that every web developer should understand. By following the rules of specificity and being mindful of the selectors you use, you can write efficient and maintainable CSS code that provides a great user experience.

Top comments (1)

Collapse
 
xmohammedawad profile image
Mohammed Awad

any note?