sodcalculator

10 Tech How-To

CSS Specificity: How It Works & Common Pitfalls

Guide 10-54693 · Updated · Step by step

CSS specificity determines which style rule wins when multiple rules target the same element. You calculate it by counting the number of ID, class, and element selectors in a selector, and the rule with the highest total is applied. This article explains the exact calculation, shows real examples, and gives practical ways to avoid the most frustrating specificity bugs.

Why Specificity Matters

Specificity is one of the three pillars of the CSS cascade, along with importance and source order. When you write a rule, you expect it to apply, but if another rule with higher specificity targets the same element, your rule is ignored. This is the root of many “why isn’t my CSS working?” moments. Understanding specificity helps you write cleaner stylesheets, avoid unnecessary !important, and debug conflicts faster.

How to Choose Your Selectors

To keep specificity low and predictable, follow these habits:

  • Use classes for styling, not IDs. IDs are for unique page anchors or JavaScript hooks.
  • Avoid over-qualifying selectors like div.container when .container is enough.
  • Keep nesting shallow in preprocessors (Sass/LESS) to avoid compound selectors.
  • Prefer utility classes or component classes over long descendant chains.
  • If you need to override a third-party style, match or exceed its specificity deliberately.

This approach makes your CSS easier to maintain and reduces the chance of accidental overrides.

Common Problems and Fixes

Here are the most frequent specificity issues and how to solve them:

  • Using IDs for styling – Replace #header with .header unless you have a strong reason.
  • Deep nesting in Sass – .nav ul li a has higher specificity than .nav a. Flatten your selectors.
  • Overriding with !important – This is a band-aid. Remove the higher-specificity rule instead.
  • Inline styles – They override any selector (except !important). Avoid them for styling; use classes.
  • Conflicting with frameworks – Bootstrap or Tailwind often use low-specificity classes. If you need to override, use a class that appears later or increase specificity slightly (e.g., .btn.btn-primary).

If you’re debugging, use the browser’s DevTools to see which rules are being applied and their specificity values.

Specificity vs. Source Order

Many beginners think the last rule always wins. That’s only true when specificity is equal. For example:

p { color: red; }
.text { color: blue; }

If you have <p class="text">, the class rule wins because (0,1,0) beats (0,0,1), even if the class rule is written first. Source order only breaks ties. This is a major source of confusion, so always check specificity before moving rules around.

How to Test and Debug Specificity

In the browser, right-click an element and choose “Inspect”. The Styles panel shows which rules are applied and which are overridden (struck through). You can also temporarily add a rule with a high specificity like body .my-class to see if it wins. For complex projects, consider using a linter that warns about high-specificity selectors.

Frequently Asked Questions

What is the specificity of a class vs. an ID?

An ID has specificity (1,0,0), while a class has (0,1,0). So an ID is always more specific, regardless of how many classes you add. For example, #nav beats .nav .item because 1 > 0 in the ID column.

Does the order of rules matter?

Only when specificity is equal. If two rules have the same (a,b,c), the one that appears later in the stylesheet wins. If they are in different stylesheets, the one loaded later wins.

How do pseudo-classes like :hover affect specificity?

Pseudo-classes count as classes. So a:hover has specificity (0,1,1), which beats a (0,0,1). Pseudo-elements like ::before count as elements, so p::before is (0,0,2).

Is !important a good way to override?

It works, but it’s a last resort. !important flips the cascade and makes that rule win over normal rules, even inline styles. Overusing it makes your CSS hard to maintain. Try to fix the specificity conflict first.

What is the specificity of the universal selector *?

It has (0,0,0) – zero specificity. So any other selector will override it. That’s why reset styles using * are easily overridden by any class or element rule.

How do attribute selectors like [type="text"] count?

They count as classes. So [type="text"] has specificity (0,1,0), same as a class. This is useful to know when you’re trying to override a rule that uses an attribute selector.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →