CSS specificity decides which style rule wins when multiple selectors target the same
element. Understanding specificity helps you debug CSS conflicts, avoid unnecessary
!important, write cleaner selectors, and build maintainable stylesheets.
What Is CSS Specificity?
CSS specificity is the weight or priority given to a selector. When two or more CSS rules
apply to the same element and property, the browser uses specificity, cascade order,
importance, and origin to decide which rule should be applied.
p {
color: black;
}
.article-text {
color: blue;
}
In this example, the class selector .article-text has higher specificity
than the element selector p, so the paragraph becomes blue.
Why CSS Specificity Is Important
Helps you understand why one style overrides another.
Prevents unnecessary use of !important.
Makes CSS easier to debug and maintain.
Improves consistency in large projects and design systems.
Helps avoid overly complex selectors.
Supports scalable CSS architecture.
Reduces style conflicts between components.
CSS Specificity Cheatsheet
The following table shows common selector types from lowest to highest priority.
Selector Type
Example
Specificity Weight
Universal selector
*
0-0-0
Element selector
p
0-0-1
Pseudo-element
p::first-line
0-0-2
Class selector
.card
0-1-0
Attribute selector
[type="email"]
0-1-0
Pseudo-class
:hover
0-1-0
ID selector
#header
1-0-0
Inline style
style="color: red;"
Higher than normal author CSS
!important
color: red !important;
Overrides normal declarations in the same origin/layer context
How CSS Specificity Score Works
Specificity is often explained as a three-part score:
ID - Class/Attribute/Pseudo-class - Element/Pseudo-element.
The final background color is orange because the second rule appears later.
Inline Styles and Specificity
Inline styles usually override normal stylesheet rules.
<p class="text" style="color: red;">
This text is red.
</p>
.text {
color: blue;
}
The text remains red because the inline style has higher priority than the class rule.
CSS !important and Specificity
The !important flag increases declaration priority and can override normal
rules. It should be used carefully because it can make CSS harder to maintain.
:where() is useful when you want base styles that are easy to override.
How to Debug CSS Specificity
Inspect the element in browser DevTools.
Check which CSS rules are crossed out.
Compare selector specificity scores.
Look for inline styles or !important.
Check whether the later rule has equal specificity.
Look for cascade layers, media queries, or component scope.
Simplify selectors when possible.
CSS Specificity and Accessibility
Specificity problems can accidentally hide focus styles, reduce contrast, or override
accessible component states.
Do not override focus outlines without a clear replacement.
Keep focus styles easy to apply and hard to accidentally remove.
Avoid using !important to force inaccessible colors.
Test hover, focus, active, disabled, valid, and invalid states.
Use consistent component selectors for accessible states.
CSS Specificity and SEO
CSS specificity does not directly affect search rankings, but cleaner CSS helps maintain
readable layouts, accessible content, responsive behavior, and reliable user experience.
Maintainable CSS makes page updates easier.
Reliable styles reduce broken layouts.
Accessible visual states improve usability.
Consistent design improves content readability.
Fewer CSS conflicts support long-term site quality.
Common CSS Specificity Mistakes
Using IDs for styling when classes would be easier to maintain.
Overusing !important.
Writing deeply nested selectors.
Using inline styles for normal component styling.
Not understanding why later rules override earlier rules.
Forgetting that pseudo-classes count like classes.
Accidentally overriding focus or accessibility styles.
Fixing every conflict by increasing selector complexity.
CSS Specificity Best Practices
Prefer classes for styling components.
Keep selectors short and readable.
Avoid styling with IDs unless truly necessary.
Avoid !important except for utilities or emergency overrides.
Use naming systems like BEM or component classes for predictable CSS.
Use :where() for low-specificity base styles.
Place utilities and overrides intentionally in your CSS architecture.
Use DevTools to inspect specificity conflicts before changing code.
Document design system layers and component override rules.
Key Takeaways
CSS specificity decides which selector wins when rules conflict.
ID selectors are more specific than class selectors.
Class, attribute, and pseudo-class selectors share the same specificity level.
Element and pseudo-element selectors have lower specificity.
If specificity is equal, the later rule usually wins.
Inline styles and !important can override normal stylesheet rules.
Low-specificity CSS is easier to maintain and override.
Pro Tip
When CSS is not working, do not immediately add !important.
First inspect the element, compare specificity, check source order, and simplify
the selector if possible.
You now understand CSS specificity, selector priority, specificity scores,
inline styles, IDs, classes, attributes, pseudo-classes, elements,
pseudo-elements, source order, inheritance, !important,
modern selector functions, accessibility, SEO benefits, best practices,
and common mistakes.