- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Specificity
CSS Foundations
CSS Specificity
Read selector weight correctly, avoid specificity battles, and know when source order breaks a tie. The lesson explains selector weight, special selector functions, tie-breaking by source order, and why !important is rarely the right first fix.
Two CSS rules target the same element, and the one you wrote last does not win. Specificity is why. It is the browser's way of deciding which selector is more precise, and it works more like a three-digit score than a points total.
Most specificity trouble comes from long selectors that describe where something sits on the page, like .sidebar div ul li a. They are hard to override and break when the HTML moves. Keeping selectors short, usually a single class selector, avoids nearly all of it. Specificity is one step of the bigger Cascade & Inheritance process, and CSS Pseudo Classes count towards it too.
How the score works
Count three things in a selector:
- IDs, like
#checkout - Classes, attributes and pseudo-classes, like
.button,[type="text"]and:hover - Element names and pseudo-elements, like
buttonand::before
Write them as three numbers, such as 1-1-0. Compare from the left: the first column that differs decides. So one ID beats any number of classes, and one class beats any number of element names. Eleven classes do not beat one ID.
Example
css
#checkout .button { color: white; } /* 1-1-0 */
.panel .button { color: navy; } /* 0-2-0 */
button.primary { color: teal; } /* 0-1-1 */The first rule wins because it has an ID. If two selectors score exactly the same, the one that comes later in the CSS wins.
Selectors with special rules
:where()always counts as zero, which makes it perfect for default styles that should be easy to override.:is(),:not()and:has()count as their strongest selector inside the brackets.- The universal selector
*counts as zero. - Inline
styleattributes beat any selector in a stylesheet. !importantjumps above all normal rules. Then the only way to beat it is another!important, which is how stylesheets end up full of them.
Keep selectors easy to override
Example
css
/* Component selector: small and predictable */
.notice { border-color: #cbd5e1; }
.notice--warning { border-color: #d97706; }
/* Zero-weight context condition */
:where(.sidebar) .notice { font-size: 0.925rem; }Each component uses one class, and variants add one more class at the same weight, so the variant wins simply by coming later. :where(.sidebar) adds context without adding any weight.
Specificity Reference
| Selector part | Weight | Example |
|---|---|---|
| ID selector | 1-0-0 | #notice |
| Class, attribute, or pseudo-class | 0-1-0 | .card, [open], :hover |
| Type selector or pseudo-element | 0-0-1 | article, ::before |
| :where() | 0-0-0 | :where(nav a) |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Specificity example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
#checkout .button { color: white; } /* 1-1-0 */
.panel .button { color: navy; } /* 0-2-0 */
button.primary { color: teal; } /* 0-1-1 */
/* Component selector: small and predictable */
.notice { border-color: #cbd5e1; }
.notice--warning { border-color: #d97706; }
/* Zero-weight context condition */
:where(.sidebar) .notice { font-size: 0.925rem; }
</style>
</head>
<body>
<main class="sidebar"><section class="panel"><button class="button primary">Specificity test</button><div class="notice notice--warning">A low-specificity component notice</div></section></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| Selector specificity | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
When your style will not apply
Open DevTools, select the element, and look at the Styles panel. Your rule is there with a line through it, and the rule above it is the one winning. Before adding an ID or !important to fight back, ask why the other selector is so strong, and make that one simpler instead.
CSS Specificity exercises
Let the id win
The page already has a style element that makes every note gray. Make the first note red. An id is more specific than a class.
The HTML your code works on
<style>
.note { color: gray; }
#main a { color: purple; }
</style>
<div id="main" class="panel">
<p id="first" class="note warn">Check the oven.</p>
<p class="note">Wash your hands.</p>
<a class="btn" href="#">Start</a>
</div>Result
