CSS specificity is the set of rules browsers use to decide which CSS declaration should be applied when multiple selectors match the same element. This becomes especially important in real projects because different stylesheets, component styles, utility classes, browser styles, and custom selectors can all target the same element.
One of the most common CSS questions is:
"Why is my CSS not working?"
In many cases, the property is valid, but another rule has higher specificity or wins later in the cascade.
Understanding specificity helps you debug these conflicts without repeatedly adding more selectors or using
!important.
The Basic Idea
When multiple CSS rules match the same element, the browser compares the competing declarations. Specificity is one of the factors used to determine which declaration wins.
Styles written directly on the HTML element have very high priority in normal author styling.
An ID selector contributes more specificity than class or element selectors.
Classes, attributes, and pseudo-classes contribute at the class specificity level.
Element names and pseudo-elements contribute at the type specificity level.
What Is CSS Specificity?
Specificity is a way of measuring how specifically a selector identifies an element. A selector targeting a very specific element generally has more specificity than a broad selector.
For example:
p {
color: black;
}
.article-text {
color: blue;
}
#main-text {
color: red;
}
If the same paragraph has both the class article-text and the ID main-text,
the ID selector is more specific than the class selector, so the text becomes red.
The CSS Specificity Hierarchy
A useful mental model is to compare selectors using four specificity components: inline styles, IDs, classes/attributes/pseudo-classes, and types/pseudo-elements. Specificity is compared as a tuple rather than by simply adding all selector parts into one ordinary number.
| Selector Type | Specificity Component | Example |
|---|---|---|
| Inline style | Highest normal author-style level | style="color:red" |
| ID selector | ID | #header |
| Class selector | Class | .button |
| Attribute selector | Class | [type="text"] |
| Pseudo-class | Class | :hover |
| Element / pseudo-element | Type | div, ::before |
How to Calculate Specificity
A practical notation is:
Inline | ID | Class/Attribute/Pseudo-class | Type/Pseudo-element
Consider this selector:
#app .card p {
color: red;
}
It contains:
- 1 ID selector:
#app - 1 class selector:
.card - 1 element selector:
p
So its specificity can be represented as: (0, 1, 1, 1) in the inline/ID/class/type model above.
Comparing Specificity
Suppose you have:
p {
color: black;
}
.card p {
color: blue;
}
#content p {
color: red;
}
All three rules target the same paragraph. Their specificity levels are different:
| Selector | Specificity | Result |
|---|---|---|
p |
Type | Lowest of these three |
.card p |
Class + Type | Stronger than p |
#content p |
ID + Type | Stronger than both |
Therefore, when all three match the same paragraph and no other cascade rule changes the result, the paragraph becomes red.
Same Specificity: Which Rule Wins?
Specificity is not the only factor involved in resolving CSS conflicts. When competing declarations have the same relevant precedence and specificity, the declaration that comes later in the cascade wins.
.button {
background-color: blue;
}
.button {
background-color: orange;
}
Both selectors have the same specificity. The second declaration wins because it appears later in the stylesheet or relevant cascade order.
Important: "The last CSS rule always wins" is incorrect. The later rule wins only when the competing declarations reach that stage with otherwise equal precedence and specificity.
CSS Specificity Example
Consider this HTML:
<div id="profile" class="card">
<p class="title">Developer Profile</p>
</div>
Now consider these rules:
p {
color: black;
}
.title {
color: blue;
}
.card .title {
color: green;
}
#profile .title {
color: red;
}
Every rule matches the paragraph except that #profile .title is the most specific among these selectors.
The final text color will therefore be red.
Specificity of IDs, Classes and Elements
The basic ordering to remember is:
This is a useful beginner mental model, but actual CSS conflict resolution also considers cascade origins, importance, layers, scoping, and source order.
Pseudo-Classes and Attribute Selectors
Class selectors, attribute selectors, and pseudo-classes contribute at the same specificity level.
.button {
color: blue;
}
[type="submit"] {
color: green;
}
.button:hover {
color: orange;
}
Each of these selectors contains class-level specificity.
A selector such as .button:hover contains two class-level components,
so it is more specific than either .button or [type="submit"].
Pseudo-Elements and Element Selectors
Element selectors such as div, p, and button,
as well as pseudo-elements such as ::before and ::after,
contribute at the type level.
p {
color: black;
}
article p {
color: blue;
}
article p::first-letter {
color: orange;
}
The selectors contain one, two, and three type-level components respectively.
The Universal Selector and Specificity
The universal selector * does not add normal selector specificity.
This is why a rule such as:
* {
box-sizing: border-box;
}
can be easily overridden by more specific selectors when they target the same property.
:where() and Specificity
The :where() pseudo-class is useful when you want to group complex selectors without increasing specificity.
The specificity contributed by the :where() function itself is zero.
:where(.card, .panel) p {
color: gray;
}
This technique is useful for creating styles that are intentionally easy to override. It can be particularly valuable in reusable component and design-system CSS.
:is() and Specificity
The :is() pseudo-class can simplify complex selector lists.
Unlike :where(), its specificity is based on the most specific selector in its argument list.
:is(#header, .navigation) a {
color: orange;
}
Understanding the difference between :is() and :where()
is useful when designing reusable CSS where specificity needs to be controlled carefully.
:not() and Specificity
The :not() pseudo-class itself does not add a separate specificity component;
the specificity of its argument contributes to the selector.
button:not(.secondary) {
background-color: orange;
}
Here, the .secondary argument contributes class-level specificity.
CSS Specificity and !important
The !important flag changes the precedence of a declaration.
It should not be treated as simply "making the selector more specific."
Important declarations participate in a different precedence level in the cascade.
.button {
color: blue !important;
}
#main .button {
color: red;
}
In this example, the !important declaration can win despite the second selector having greater ordinary specificity.
This is why adding !important should not be the first solution to a CSS conflict.
CSS Specificity and Inheritance
Specificity and inheritance are different concepts. Specificity resolves conflicts between matching declarations. Inheritance determines whether certain properties receive their value from an ancestor when the element does not have a specified value of its own.
body {
color: gray;
}
.card {
color: blue;
}
Text inside the card can inherit the color from .card unless another applicable rule on the child overrides it.
You should not think of inherited styles as competing with matching child selectors in exactly the same way as two direct declarations.
Specificity in Nested and Complex Selectors
Every selector should be evaluated by its actual components rather than by how long it looks. A long selector is not automatically stronger than a shorter selector containing an ID.
html body main section article p {
color: blue;
}
#content {
color: red;
}
The first selector contains many type selectors, but the ID selector in the second rule contributes more specificity at the ID level. Therefore, selector specificity should be compared component by component rather than by counting the total number of selector tokens.
CSS Cascade Layers and Specificity
Modern CSS also provides @layer for organizing styles into explicit cascade layers.
Cascade layers allow authors to control the ordering of groups of CSS rules without relying solely on selector specificity.
@layer base {
.button {
color: blue;
}
}
@layer components {
#app .button {
color: orange;
}
}
When declarations come from different cascade layers, layer ordering is considered before selector specificity within the relevant cascade comparison. This makes layers useful for controlling large stylesheets without creating increasingly specific selectors.
Why CSS Specificity Becomes a Problem
Specificity problems usually appear when CSS becomes difficult to override. A developer adds one selector to fix a component, another selector to override that fix, and eventually the stylesheet becomes a chain of increasingly specific rules.
Specificity Escalation
This pattern makes future overrides harder and harder. Good CSS aims to keep specificity predictable instead of continuously increasing it.
Common Specificity Mistakes
Assuming the Last Rule Always Wins
Source order matters when the relevant competing declarations have otherwise equal precedence and specificity. A later weak selector does not automatically override a stronger one.
Using IDs for Everything
IDs create high specificity and can make component styles difficult to override. Classes are usually easier to reuse and manage.
Solving Every Conflict with !important
Excessive use of !important makes the cascade harder to reason about and can create another layer of conflicts.
Writing Extremely Long Selectors
Deep selectors often create unnecessary specificity and strong coupling between markup structure and styles.
Ignoring the Cascade
Specificity is only one part of CSS conflict resolution. Origin, importance, layers, scope, and source order can also affect the winning declaration.
Best Practices for Managing CSS Specificity
- Prefer reusable class selectors for component styling.
- Keep selector chains short and predictable.
- Avoid unnecessary IDs in CSS.
- Do not use
!importantas the default solution. - Use cascade layers when a large project needs clear stylesheet precedence.
- Use
:where()when you intentionally want low-specificity reusable selectors. - Use browser DevTools to inspect which declaration is winning.
- Fix the source of a specificity conflict instead of continuously adding more selectors.
- Keep component styles independent from unnecessarily deep HTML structures.
How to Debug a CSS Specificity Problem
When a CSS property appears not to work, open the browser's developer tools and inspect the element. Look at the Styles panel to see which declarations match the element and which ones have been overridden.
Key Takeaways
CSS specificity determines how strongly a selector targets an element when multiple declarations compete.
IDs are more specific than classes, attributes, and pseudo-classes, while those are more specific than element selectors and pseudo-elements.
Specificity is compared component by component rather than by simply counting how many selectors appear in a rule.
When relevant declarations have equal precedence and specificity, source order can determine the winner.
!important changes cascade precedence; it is not simply an extra specificity point.
:where() contributes zero specificity, while :is() uses the specificity of its most specific argument.
Modern CSS provides cascade layers that can help control stylesheet precedence without constantly increasing selector specificity.
The best CSS keeps specificity low, predictable, and easy to override.