CSS specificity is the algorithm the browser uses to decide which rule wins when multiple CSS rules target the same element and set the same property — it scores each selector by counting IDs, classes, and element types, and the highest score wins. Understanding it means you stop fighting the cascade with !important and start using specificity deliberately.

You wrote a CSS rule. It should work. But the element stubbornly refuses to change. You add !important, and it works — but now you have a different problem. Next month, another rule needs to override the !important one, and you are stuck in an escalation spiral.

How Specificity Is Calculated

Every CSS selector has a specificity score. When two rules apply to the same element and set the same property, the rule with the higher specificity wins.

Specificity is calculated as three components, often written as (A, B, C):

Component What Counts Example
A ID selectors #header, #nav
B Class selectors, attribute selectors, pseudo-classes .active, [type="text"], :hover
C Type selectors, pseudo-elements div, p, ::before

Examples

Selector Specificity (A, B, C)
p (0, 0, 1)
.card (0, 1, 0)
p.card (0, 1, 1)
#header (1, 0, 0)
#header .nav a (1, 1, 1)
#header .nav .active a:hover (1, 3, 1)
div p span (0, 0, 3)
.card .title (0, 2, 0)
[data-theme="dark"] (0, 1, 0)

Comparison Rules

Specificity is compared component by component, from left to right:

  1. Compare A values. Higher wins.
  2. If A values are equal, compare B values. Higher wins.
  3. If B values are also equal, compare C values. Higher wins.
  4. If all components are equal, the rule that appears later in the stylesheet wins (source order).

This means one ID selector (1, 0, 0) always beats any number of class selectors (0, N, 0). And one class selector always beats any number of type selectors.

/* Specificity: (0, 0, 3) — three type selectors */
div section p {
  color: blue;
}

/* Specificity: (0, 1, 0) — one class selector */
.intro {
  color: red;  /* This wins */
}

The class selector wins despite having fewer total selectors, because class (B) outranks type (C).

What Does NOT Affect Specificity

The Universal Selector (*)

The universal selector contributes nothing to specificity: (0, 0, 0).

* { margin: 0; }  /* Specificity: (0, 0, 0) */

Combinators

Combinators (>, +, ~, ) do not affect specificity. They describe relationships but do not add weight.

/* Both have specificity (0, 0, 2) */
div p { color: blue; }
div > p { color: red; }  /* Wins by source order */

The :where() Pseudo-Class

:where() always has zero specificity, regardless of its contents. This is incredibly useful for writing default styles that are easy to override:

/* Specificity: (0, 0, 0) */
:where(.card) {
  padding: 1rem;
  border: 1px solid #e5e7eb;
}

/* Any rule can override without fighting specificity */
.special-card {
  padding: 2rem;  /* Wins easily */
}

The :is() and :not() Pseudo-Classes

:is() and :not() take the specificity of their most specific argument:

/* Specificity: (1, 0, 0) — takes specificity of #header */
:is(#header, .nav, p) {
  color: blue;
}

/* Specificity: (0, 1, 1) — :not(.active) adds class specificity */
a:not(.active) {
  color: gray;
}

The Cascade Order

Specificity is only one part of how the browser resolves style conflicts. The full cascade order (simplified) is:

  1. Origin and importance: User agent styles < author styles < author !important < user agent !important
  2. CSS layers (@layer): Rules in earlier layers lose to rules in later layers
  3. Specificity: Higher specificity wins
  4. Source order: Later rules win among equal specificity

!important

!important jumps a declaration to a higher level in the cascade. It does not change specificity — it changes the origin importance.

.card {
  color: red !important;  /* Higher cascade origin */
}

#main .card {
  color: blue;  /* Higher specificity, but loses to !important */
}

When !important is appropriate:

  • Utility classes that must always apply: .hidden { display: none !important; }
  • User stylesheets for accessibility overrides
  • Overriding third-party CSS that you cannot modify

When !important is a code smell:

  • When you use it to fix a specificity problem you do not understand
  • When you find yourself adding !important to override another !important
  • In component styles that other developers will need to customize

CSS Layers

@layer provides explicit control over cascade priority, independent of specificity:

@layer base, components, utilities;

@layer base {
  a { color: blue; }
}

@layer components {
  .nav a { color: white; }  /* Wins over base, regardless of specificity */
}

@layer utilities {
  .text-red { color: red; }  /* Wins over components */
}

Layers are a modern alternative to fighting specificity wars. Rules in later layers always beat rules in earlier layers, regardless of selector specificity.

Practical Specificity Strategies

Keep Specificity Low

Use class selectors as your primary styling mechanism. Avoid IDs in CSS. Avoid deep nesting.

/* Bad: high specificity, hard to override */
#header nav ul li a.active {
  color: white;
}

/* Good: flat, low specificity */
.nav-link--active {
  color: white;
}

Use BEM or Similar Conventions

BEM (Block Element Modifier) keeps specificity flat by using single class selectors:

.card { }
.card__title { }
.card__title--large { }
.card__body { }

Every selector has the same specificity: (0, 1, 0). Overrides happen through more specific class names or source order, not deeper nesting.

Leverage :where() for Defaults

Write your base styles with :where() so any component style can override them without specificity battles:

/* Base styles — zero specificity */
:where(h1) { font-size: 2rem; margin-bottom: 1rem; }
:where(p) { line-height: 1.6; margin-bottom: 1rem; }
:where(a) { color: #3b82f6; text-decoration: underline; }

/* Component styles override easily */
.hero-title { font-size: 3rem; }  /* No fight needed */

Structure Your Stylesheet

Organize your CSS from low to high specificity:

  1. Reset/normalize — lowest specificity
  2. Base styles — type selectors or :where()
  3. Layout — class selectors
  4. Components — class selectors
  5. Utilities — class selectors, possibly with !important

This mirrors the @layer approach and ensures that later, more specific styles naturally override earlier, more general ones.

Debugging Specificity Issues

Browser DevTools

The browser DevTools are your best friend for specificity debugging:

  1. Inspect the element
  2. Look at the “Styles” panel
  3. Rules are listed in specificity order — higher specificity at the top
  4. Overridden declarations are shown with a strikethrough
  5. Hover over a selector to see its specificity

Common Symptoms

  • Style not applying: Check if a higher-specificity rule is overriding it. Look for strikethroughs in DevTools.
  • Style applying unexpectedly: Check if the selector matches more elements than intended.
  • !important not working: Another !important rule with higher specificity or later source order is winning.
  • Styles from a library are hard to override: The library uses high-specificity selectors. Use :where() in the library, or increase your specificity to match.

For a comprehensive reference on selector syntax while debugging, check out our CSS selectors reference. It covers combinators, pseudo-classes, and pseudo-elements with specificity information.

Quick Debugging Checklist

  1. Is the selector actually matching the element? (Check in DevTools)
  2. Is another rule with higher specificity overriding it? (Check for strikethroughs)
  3. Is !important involved anywhere in the chain?
  4. Is source order the issue? (Try moving the rule later)
  5. Is inline style overriding everything? (Check the style attribute)
  6. Are CSS layers affecting the cascade order?

Specificity Misconceptions

“Inline styles have infinite specificity”

Inline styles do not have infinite specificity. They have a specificity layer above IDs, but !important in a stylesheet still overrides them. Think of specificity as (inline, A, B, C) where inline is 0 or 1.

“More selectors always means higher specificity”

No. div div div div div (0, 0, 5) loses to .card (0, 1, 0). The type of selector matters more than the count.

“Specificity and inheritance are the same”

They are different. Inherited styles have no specificity — any directly applied rule overrides them. If a div inherits color: red from body, even a * selector setting color: blue on the div wins, because direct application beats inheritance.

Summary

Specificity determines which CSS rule wins when multiple rules target the same element and property. IDs beat classes, classes beat types, and source order breaks ties. Keep your selectors flat, prefer classes over IDs, use :where() for overridable defaults, and reach for CSS @layer instead of !important when you need cascade control. When a style does not apply, the DevTools specificity view will show you exactly why — and the fix is almost always reducing the specificity of the conflicting rule, not increasing the specificity of yours.