Skip to main content

How to Override a CSS Rule with a More Specific Selector

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

When two rules set the same property on an element, the browser picks the one with the more specific selector. An id beats a class, a class beats a tag name, and only between equals does the later rule win.

How Specificity Works​

Every selector gets a weight with three columns: the number of IDs, the number of classes, and the number of tag names in it. The browser compares the columns from left to right, and the first column that differs decides.

SelectorExampleWeight (ID - CLASS - TYPE)
Tag namep0-0-1
Class.note0-1-0
ID#warning1-0-0

Attribute selectors such as [type="radio"] and pseudo-classes such as :hover count in the CLASS column too. MDN's Specificity page explains the columns and every exception.

The Example​

The example is three paragraphs: a plain one, a note and a warning. Three rules color them, and they're deliberately in the "wrong" order: the p rule comes after the .note rule.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding three rules in this order, the class note with the color 0f6cbd, p with the color 616161 and the id warning with the color c50f1f, and three paragraphs, a plain one, one with the class note and one with the class note and the id warning.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Specificity</title>
<style>
.note {
color: #0f6cbd;
}

p {
color: #616161;
}

#warning {
color: #c50f1f;
}
</style>
</head>
<body>
<p>A plain paragraph.</p>
<p class="note">A note.</p>
<p class="note" id="warning">A warning.</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: A plain paragraph. in grey, A note. in blue and A warning. in red.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The plain paragraph is grey, rgb(97, 97, 97). Only the p rule matches it.
  • The note is blue, rgb(15, 108, 189). Both .note and p match it, and .note, at 0-1-0, beats p, at 0-0-1, although the p rule comes later.
  • The warning is red, rgb(197, 15, 31). Three rules match it, and #warning, at 1-0-0, beats the other two.

Common Mistakes​

MistakeWhat happensDo this instead
Moving a rule further down to make it winOrder decides only between equal selectors: the p rule after .note still lostUse a more specific selector
Reaching for !important firstIt beats every normal rule, even an id, so the rule no longer follows specificity at allFix the selectors first. MDN's page asks for a comment with every !important that explains why it's needed

Troubleshooting​

SymptomLikely causeFix
A rule lower in the file doesn't applyA more specific rule sets the same propertyMake your selector at least as specific, for example with a class
A rule with an id doesn't applyAn inline style attribute or an !important declaration winsRemove the inline style, or check for !important

FAQ​

What happens when two selectors are equally specific?​

The later one wins. With two p rules, blue first and grey second, the paragraph was grey. MDN's page says the same: with equal specificity, the last declared selector takes precedence.

Does !important beat an id?​

Yes. A paragraph with class="imp" and id="x" was red from .imp { color: #c50f1f !important; }, although #x set it to blue.

Does a style attribute beat the rules?​

Yes. A paragraph with style="color: #00838f" was teal, although a p rule colored paragraphs. MDN's page says inline styles always overwrite normal styles in author style sheets.

Conclusion​

To override a rule, make your selector more specific: a class beats a tag name, and an id beats a class. The order in the file only matters between equals.

Additional Resources​