frontend
CSS Selector Priorities (Specificity)
January 24, 2026
CSS Selector Priorities (Specificity)
Overview
CSS selector priority (specificity) determines which CSS rule is applied when multiple rules target the same element. Understanding specificity is crucial for writing maintainable CSS and debugging style conflicts.
Specificity Order
- !important - Highest priority (overrides everything)
- Inline styles - style attribute
- IDs - #myId
- Classes, attributes, pseudo-classes - .class, [attr], :hover
- Elements and pseudo-elements - div, ::before
Example
<style>
.box {
color: orange !important; /* PRIORITY 1 - Highest */
}
#myDiv {
color: red; /* PRIORITY 2 - ID selector */
}
.box {
color: blue; /* PRIORITY 3 - Class selector (duplicate, last one wins if same specificity) */
}
div > div {
color: black; /* PRIORITY 4 - Descendant selector */
}
div {
color: yellow; /* PRIORITY 5 - Element selector */
}
</style>
<div>
<div id="myDiv" class="box">This is a paragraph inside the div.</div>
</div>
Specificity Calculation
Point System
- Inline style: 1000 points
- ID: 100 points
- Class/Attribute/Pseudo-class: 10 points
- Element/Pseudo-element: 1 point
Examples
/* Specificity: 0,0,0,1 = 1 point */
div { }
/* Specificity: 0,0,1,0 = 10 points */
.class { }
/* Specificity: 0,1,0,0 = 100 points */
#id { }
/* Specificity: 0,0,1,1 = 11 points */
div.class { }
/* Specificity: 0,1,1,1 = 111 points */
#id div.class { }
/* Specificity: 1,0,0,0 = 1000 points (inline) */
<div style="color: red;">
Best Practices
- Avoid !important: Use sparingly, only when necessary
- Prefer Classes: Use classes over IDs for styling
- Keep Specificity Low: Easier to override and maintain
- Use BEM: Block Element Modifier methodology
- Avoid Over-nesting: Keep selectors shallow
- Document Complex Rules: Comment why high specificity is needed