How to Override a CSS Rule with a More Specific Selector
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.
| Selector | Example | Weight (ID - CLASS - TYPE) |
|---|---|---|
| Tag name | p | 0-0-1 |
| Class | .note | 0-1-0 |
| ID | #warning | 1-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:

<!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 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 theprule matches it. - The note is blue,
rgb(15, 108, 189). Both.noteandpmatch it, and.note, at 0-1-0, beatsp, at 0-0-1, although theprule 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
| Mistake | What happens | Do this instead |
|---|---|---|
| Moving a rule further down to make it win | Order decides only between equal selectors: the p rule after .note still lost | Use a more specific selector |
Reaching for !important first | It beats every normal rule, even an id, so the rule no longer follows specificity at all | Fix the selectors first. MDN's page asks for a comment with every !important that explains why it's needed |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A rule lower in the file doesn't apply | A more specific rule sets the same property | Make your selector at least as specific, for example with a class |
| A rule with an id doesn't apply | An inline style attribute or an !important declaration wins | Remove 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
- Specificity, on MDN.
