🎨 What Is Specificity in CSS?

CSS stands for Cascading Style Sheets, and the word cascading is very important.

Sometimes multiple CSS rules target the same HTML element.

When this happens, the browser must decide: β€œWhich CSS rule should be applied?”

The system used to determine this priority is called specificity.

🧩 Understanding CSS Specificity

Every CSS selector receives a priority score.

The browser compares these scores and applies the rule with the highest specificity.

Specificity is usually represented as a four-part value:

CSS
a b c d

πŸ“Š Specificity Score Breakdown

Value Selector Type Example Weight
a Inline styles style="color:red;" 1000
b ID selectors #menu 0100
c Classes, attributes, pseudo-classes .box, [type], :hover 0010
d Elements and pseudo-elements div, p, ::after 0001

πŸ”’ Specificity Examples

CSS Rule Specificity Score
* {} 0,0,0,0 0000
div {} 0,0,0,1 0001
.box {} 0,0,1,0 0010
div.box {} 0,0,1,1 0011
#menu {} 0,1,0,0 0100
div#menu {} 0,1,0,1 0101
style="color:red;" 1,0,0,0 1000

🧠 How the Browser Chooses a Rule

When multiple CSS rules target the same element, the browser follows this priority order:

  • 1. Inline styles
  • 2. ID selectors
  • 3. Class, attribute, and pseudo-class selectors
  • 4. Element and pseudo-element selectors

The higher specificity wins.

πŸ’‘ Example of Specificity

πŸ“˜ HTML + CSS Example

HTML

<!DOCTYPE html>
<html>
<head>
    <style>
        p { color: blue; }              /* 0001 */
        .colored { color: green; }      /* 0010 */
        #important { color: red; }      /* 0100 */
    </style>
</head>
<body>
    <p   id="important"
            class="colored"
            style="color: orange;">
            Hello CSS!
    </p>
</body>
</html>
                

πŸ” What Happens Here?

The same <p> element receives four different color rules.

πŸ“Š Specificity Calculation

Rule Specificity
p { color: blue; } 0001
.colored { color: green; } 0010
#important { color: red; } 0100
style="color: orange;" 1000

βœ… The inline style has the highest specificity (1000), so the text becomes orange.

πŸ“‹ Specificity Priority Table

Priority Selector Type Score
πŸ”₯ Highest Inline styles 1000
High ID selectors 0100
Medium Classes, attributes, pseudo-classes 0010
Low Elements and pseudo-elements 0001

⚠️ Important Notes

1. If Specificity Is Equal

If two selectors have the same specificity value, the browser applies: The rule written later in the CSS file.

πŸ“˜ Example:

CSS

p {
  color: blue;
}

p {
  color: red;
}
                

βœ… Result: The text becomes red because the second rule comes later.

2. Using !important

The !important keyword forces a CSS rule to override others.

πŸ“˜ Example:

CSS

p {
  color: blue !important;
}
                

⚠️ This rule can override many other styles, even selectors with higher specificity.

🚨 Why You Should Avoid Overusing !important

Using !important too often can make CSS difficult to maintain and debug.

It should only be used when absolutely necessary.

  • 🧠 Quick Summary

  • Specificity determines which CSS rule is applied
  • Inline styles have the highest normal priority
  • ID selectors are stronger than classes
  • Classes are stronger than element selectors
  • If specificity is equal, the last rule wins
  • !important overrides normal specificity rules