CSS Specificity
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.
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;
}
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;
}
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
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.
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.
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 |
!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
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.
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).
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;
}
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 |
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;
}
14. CSS Specificity Interview Questions
- What is CSS Specificity?
- Why is CSS Specificity important?
- Which selector has the highest specificity?
- How is specificity calculated?
- What is the specificity value of an ID selector?
- What is the specificity value of a class selector?
- When should you avoid using
!important? - What is the difference between ID and class selectors?
- Does an inline style override external CSS?
- How can CSS conflicts be reduced?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Specificity.
- ✔ CSS Specificity hierarchy.
- ✔ How specificity is calculated.
- ✔ Examples of specificity.
- ✔ The
!importantrule. - ✔ Inline, Internal, and External CSS priorities.
- ✔ Best practices.
- ✔ Common mistakes.
- ✔ Browser support.
- ✔ Real-world examples and interview questions.
Quick Quiz
Which CSS style has the highest specificity?