CSS Specificity

CSS Specificity is a set of rules used by browsers to determine which CSS rule should be applied when multiple rules target the same HTML element.

1. Introduction to CSS Specificity

Sometimes multiple CSS selectors apply different styles to the same element. CSS Specificity decides which style has higher priority and will be displayed in the browser.

Why is Specificity Important?
  • Resolves CSS conflicts.
  • Determines which rule is applied.
  • Makes CSS easier to manage.
  • Helps avoid unexpected styling.
  • Improves maintainability of large projects.

p{

    color:blue;

}

.text{

    color:red;

}

Since the class selector has higher specificity than the element selector, the text color will be red.

CSS Specificity determines which CSS rule wins when multiple rules target the same element.

2. What is CSS Specificity?

CSS Specificity is a priority system used by browsers to decide which CSS declaration should be applied when more than one rule matches an element.

Selector Type Priority
Inline Style Highest
ID Selector High
Class, Attribute, Pseudo-class Medium
Element & Pseudo-element Low
Universal Selector (*) Lowest

#title{

    color:green;

}

.heading{

    color:red;

}

h1{

    color:blue;

}
The ID selector has higher specificity than both the class selector and the element selector.

3. CSS Specificity Hierarchy

CSS follows a hierarchy of selectors. The selector with the higher priority overrides selectors with lower priority.

Priority Selector
1 Inline Style
2 ID Selector (#id)
3 Class (.class), Attribute, Pseudo-class
4 Element (p, h1, div), Pseudo-element
5 Universal Selector (*)

*{

    color:black;

}

p{

    color:blue;

}

.note{

    color:red;

}

#message{

    color:green;

}
Inline styles have the highest priority, while the universal selector has the lowest.

4. How Specificity is Calculated

Browsers calculate specificity using four values. The higher the value, the higher the priority.

Selector Specificity Value
Inline Style 1000
#id 100
.class, [attribute], :hover 10
p, div, h1, ::before 1
* 0

#header .menu li{

    color:red;

}

Specificity Calculation: 100 + 10 + 1 = 111

The browser compares specificity values before applying CSS rules.

5. Examples of CSS Specificity

Example 1

p{

    color:blue;

}

.text{

    color:red;

}

The class selector wins because its specificity is higher.


Example 2

.text{

    color:red;

}

#title{

    color:green;

}

The ID selector overrides the class selector.


Example 3

Welcome

The inline style overrides external and internal CSS because it has the highest specificity.

Understanding specificity helps you avoid CSS conflicts and write cleaner stylesheets.

6. Specificity Comparison Examples

When multiple CSS rules match the same element, the browser compares their specificity values and applies the rule with the highest specificity.

Example 1 : Element vs Class

p{

    color:blue;

}

.text{

    color:red;

}

Result: The class selector wins because its specificity (10) is higher than the element selector (1).


Example 2 : Class vs ID

.text{

    color:red;

}

#title{

    color:green;

}

Result: The ID selector wins because its specificity (100) is greater than the class selector (10).


Example 3 : Multiple Selectors

#header .menu li{

    color:blue;

}

.menu li{

    color:red;

}

Result: #header .menu li wins because its specificity is 111.

Always compare specificity before assuming which CSS rule will be applied.

7. The !important Rule

The !important rule overrides almost every other CSS declaration regardless of specificity.


p{

    color:blue;

}

.text{

    color:red !important;

}
Rule Priority
Normal CSS Lower
!important Higher
Avoid excessive use of !important. It makes CSS difficult to maintain and debug.

8. Inline Styles vs Internal vs External CSS

CSS can be applied in three different ways. Their priorities are different when styling the same element.

CSS Type Priority
Inline CSS Highest
Internal CSS Depends on specificity
External CSS Depends on specificity

Welcome

Inline styles usually override internal and external CSS because they have the highest specificity.

9. Best Practices for CSS Specificity

  • Prefer classes over IDs for styling.
  • Avoid deeply nested selectors.
  • Use meaningful class names.
  • Minimize the use of !important.
  • Keep selectors simple and readable.
  • Organize CSS into reusable components.
  • Test styles after adding new selectors.
Writing low-specificity CSS makes your stylesheets easier to maintain and update.

10. CSS Specificity Comparison Table

Selector Specificity Priority
Inline Style 1000 Highest
#id 100 High
.class, [attribute], :hover 10 Medium
div, p, h1, ::before 1 Low
* 0 Lowest

#content .menu li{

    color:red;

}

.menu li{

    color:blue;

}

Result: The first selector wins because its specificity (111) is greater than the second selector (11).

Understanding specificity helps you write predictable, organized, and conflict-free CSS.

11. Common CSS Specificity Mistakes

Beginners often create CSS conflicts by writing selectors with unnecessarily high specificity. Avoiding these mistakes makes your CSS easier to maintain.

Mistake Better Practice
Using too many ID selectors. Prefer reusable class selectors.
Deeply nested selectors. Keep selectors short and simple.
Using !important everywhere. Use proper specificity instead.
Mixing inline styles with external CSS. Keep styles in external stylesheets.
Repeating unnecessary selectors. Write clean and organized CSS.

❌ Poor

#header #menu .nav ul li a{

    color:red !important;

}

✔ Better

.nav-link{

    color:red;

}
Lower specificity makes CSS easier to reuse, override, and maintain.

12. Browser Support

CSS Specificity is a core feature of CSS and is fully supported by all modern web browsers.

Browser Support
Google Chrome ✔ Supported
Mozilla Firefox ✔ Supported
Microsoft Edge ✔ Supported
Safari ✔ Supported
Opera ✔ Supported
CSS Specificity works consistently across all modern browsers.

13. Real-World Use Cases

Situation Recommended Selector
Website Navigation .navbar a
Buttons .btn-primary
Cards .card-title
Forms .form-control
Unique Element #header

.navbar a{

    color:white;

}

.btn-primary{

    background:#0d6efd;

}
Using appropriate selector specificity helps build scalable and maintainable websites.

14. CSS Specificity Interview Questions

  1. What is CSS Specificity?
  2. Why is CSS Specificity important?
  3. Which selector has the highest specificity?
  4. How is specificity calculated?
  5. What is the specificity value of an ID selector?
  6. What is the specificity value of a class selector?
  7. When should you avoid using !important?
  8. What is the difference between ID and class selectors?
  9. Does an inline style override external CSS?
  10. How can CSS conflicts be reduced?
These interview questions cover the most important CSS Specificity concepts.

15. Lesson Summary

In this lesson, you learned:
  • ✔ Introduction to CSS Specificity.
  • ✔ CSS Specificity hierarchy.
  • ✔ How specificity is calculated.
  • ✔ Examples of specificity.
  • ✔ The !important rule.
  • ✔ Inline, Internal, and External CSS priorities.
  • ✔ Best practices.
  • ✔ Common mistakes.
  • ✔ Browser support.
  • ✔ Real-world examples and interview questions.
Congratulations! You have successfully completed the CSS Specificity lesson. You can now confidently resolve CSS conflicts and write cleaner, more maintainable stylesheets.

Quick Quiz

Which CSS style has the highest specificity?