CSS Specificity Explained: How Conflicts Are Resolved
Specificity decides which CSS rule wins when several match. Learn the scoring, the cascade layers, and how to avoid specificity wars.
By Velloreth
What specificity is
When several CSS rules match the same element, specificity decides which one wins.
p { color: blue; } /* 0,0,0,1 */
.intro { color: green; } /* 0,0,1,0 */
#main .intro { color: red; } /* 0,1,1,0 */
An element with class="intro" inside id="main" gets red, because #main .intro has the highest specificity.
The score
Specificity is a four-part score, compared left to right.
| Part | Counts |
|---|---|
| Inline style | style="..." |
| ID | #header |
| Class, attribute, pseudo-class | .btn, [type="text"], :hover |
| Element, pseudo-element | div, ::before |
Written as (inline, id, class, element):
p /* 0,0,0,1 */
.nav /* 0,0,1,0 */
.nav a /* 0,0,1,1 */
#header .nav a /* 0,1,1,1 */
#header .nav a:hover /* 0,1,2,1 */
style="color: red" /* 1,0,0,0 */
The comparison is left to right, and the first difference decides. A single ID beats any number of classes:
#main { color: red; } /* 0,1,0,0 */
.a.b.c.d.e.f.g.h.i.j { color: blue; } /* 0,0,10,0 */
Red wins. Ten classes do not beat one ID.
Source order
When specificity ties, the later rule wins.
.btn { color: blue; }
.btn { color: red; } /* wins — same specificity, later */
This is why import order matters. A stylesheet imported later overrides an earlier one with equal specificity.
@import 'reset.css';
@import 'components.css'; /* wins on ties */
!important
!important overrides normal declarations, regardless of specificity.
.btn { color: blue !important; }
#main .btn { color: red; } /* loses to !important */
Two !important declarations are compared by specificity, then source order.
!important is justified in a few cases:
- Utility classes that must always win, like
.hidden { display: none !important; }. - Overriding third-party styles you cannot edit.
- Accessibility overrides, like forcing reduced motion.
- Print stylesheets, where you need to override screen styles.
It is not justified as a fix for a specificity conflict. Adding !important to win an argument makes the next conflict harder, and the escalation continues.
Cascade layers
Layers let you control precedence explicitly, which removes most specificity conflicts.
@layer reset, base, components, utilities;
@layer reset {
* { margin: 0; }
}
@layer base {
p { color: black; }
}
@layer utilities {
.text-red { color: red; }
}
A later layer wins over an earlier one, regardless of specificity.
@layer base {
#main p { color: black; } /* high specificity */
}
@layer utilities {
.text-red { color: red; } /* wins — later layer */
}
This is the modern solution to specificity wars. You decide the order of precedence once, at the top of the stylesheet, and then write simple selectors.
Unlayered styles win over layered ones, which is useful for overrides.
:is() and :where()
These pseudo-classes change how specificity is calculated.
/* :is() takes the specificity of its most specific argument */
:is(#main, .content) p { } /* specificity of #main p */
/* :where() has zero specificity */
:where(#main, .content) p { } /* specificity of p */
:where() is useful for resets and defaults, because it never adds specificity.
:where(h1, h2, h3) {
margin-block: 0;
}
This resets heading margins without making the reset hard to override.
The specificity war
The pattern: a rule does not apply, so you add a more specific selector. Then another rule does not apply, so you add an ID. Then you add !important. The stylesheet becomes a ladder of escalating selectors.
/* The escalation */
.card { padding: 1rem; }
.sidebar .card { padding: 0.5rem; }
#main .sidebar .card { padding: 0.25rem; }
#main .sidebar .card { padding: 0.25rem !important; }
The fix is not a better selector. It is a different approach.
Keep selectors flat. One class per element, no descendant chains.
.card { padding: 1rem; }
.card--compact { padding: 0.5rem; }
Use a naming convention. BEM, or any consistent scheme, makes each rule target one thing.
Use layers to control precedence explicitly.
Avoid IDs in CSS. They are hard to override and usually unnecessary.
Avoid !important except in the justified cases.
Debugging specificity
DevTools shows which rule won and which were overridden.
- Right-click the element and choose Inspect.
- Open the Styles panel.
- Winning declarations are shown normally; overridden ones are struck through.
- Hover a rule to see the selector’s specificity.
The Computed tab shows the final value and which rule set it, which is the fastest way to answer “why is this not applying?”.
Inline styles and the style attribute
Inline styles sit at the top of the specificity scale, above any selector.
<p style="color: red">This wins over any stylesheet rule</p>
They are hard to override, which is why they are usually a mistake in application code. The exceptions are genuinely dynamic values that come from JavaScript, such as a computed position or a progress percentage.
// Legitimate: a value only known at runtime
element.style.setProperty('--progress', `${percent}%`);
Note the difference between setting a custom property and setting a concrete style. Setting --progress leaves the actual styling in the stylesheet, where it belongs, and the inline style only supplies the value. This keeps the cascade intact.
If you must override an inline style from CSS, !important is the only way, which is a sign the inline style should not have been there.
Specificity in component systems
Component-based frameworks add a layer of complexity, because styles are often scoped or generated.
CSS Modules hash class names, so specificity is almost always a single class. Conflicts are rare, and the cascade is shallow.
Scoped styles in Vue and Svelte add an attribute selector, which raises specificity by one class-level unit. A scoped .button becomes .button[data-v-abc123], which is 0,0,2,0.
Utility frameworks keep every rule at a single class, so specificity is uniform and source order decides. This is why utility classes are usually ordered deliberately in the build.
CSS-in-JS often injects styles at runtime, and the injection order determines the winner on ties. This is a common source of “why is my style not applying” in styled-components and emotion.
The common thread: keep specificity flat and let order or layers decide. A component system that relies on escalating specificity is fighting its own architecture.
Practical guidance
- Keep selectors flat — one class per element.
- Avoid IDs in CSS.
- Use cascade layers to control precedence.
- Use
:where()for resets so they are easy to override. - Reserve
!importantfor utilities and third-party overrides. - Use a naming convention so each rule targets one thing.
- Check the Computed tab when a style does not apply.
- Do not escalate specificity to win a conflict — restructure instead.
For the related guides, see css-pseudo-classes-guide and css-custom-properties. For the broader context, css-flexbox-complete-guide and css-grid-complete-guide.
Quotable summary
Specificity is a four-part score — inline styles, ids, classes and attributes and pseudo-classes, then elements and pseudo-elements — compared left to right, and the higher score wins regardless of source order. When specificity ties, the later rule in the source wins, which is why import order matters. !important overrides normal declarations and should be reserved for utility classes, third-party overrides, and accessibility rules, not used to win specificity conflicts. Cascade layers let you control precedence explicitly, so a later layer wins regardless of specificity, which removes most of the need for specificity hacks. The practical fix for specificity wars is to keep selectors flat, avoid IDs in CSS, and use a naming convention rather than escalating.