Sub topic 15
Keeping Specificity Low and Manageable
This chapter assumes you can read selectors, calculate specificity as an (IDs, classes, types) triple, and know how source order, !important, and @layer affect the cascade. Here you will use that knowledge to write CSS that stays easy to override.
Why specificity grows out of control
Every time a rule loses to a more specific one, the easy fix is to write an even more specific selector. Each fix raises the bar for the next one. Eventually someone reaches for !important, and the next fix needs another !important. A stylesheet in this state is hard to change, because any edit may be overridden by a rule you cannot easily find.
The goal is the opposite: most rules should sit at a similar, low specificity, so that source order decides ties and overrides are simple.
Practical habits
- Style with classes. A single class gives (0,1,0). It is specific enough to be useful and low enough to override. Avoid ID selectors for styling, since one ID outweighs any number of classes. Use IDs for fragment links and scripting, not CSS.
- Avoid long selector chains.
#main ul li.active > a[href]is tied to the page structure and is hard to beat. A class such as.nav-link-activesays the same thing with far less weight. - Do not qualify classes with types.
p.noteis (0,1,1)..noteis (0,1,0). The extra type adds weight and gains nothing unless you need it. - Order rules from general to specific. Put base styles first and variations later. Equal specificity then resolves by source order, which is easy to reason about.
- Treat
!importantas a last resort. It is reasonable for utility classes that must always apply, or for overriding third-party inline styles. It is a poor way to win an ordinary conflict.
Tools that keep weight down
:where() always contributes zero specificity, so it lets you write grouped selectors that are trivial to override.
:where(.card, .panel) a { color: blue; } /* (0,0,1) */
.card-link { color: crimson; } /* (0,1,0), wins */Use :is() and :not() with care. They take the weight of their most specific argument, so one ID inside them raises the whole selector.
Cascade layers move the decision away from specificity altogether. For normal declarations a later layer beats an earlier one, whatever the selectors look like.
@layer base, components, utilities;
@layer base {
#nav a { color: black; }
}
@layer components {
a { color: blue; }
}Here links in #nav are blue, even though #nav a is (1,0,1) and a is (0,0,1). Put third-party or reset styles in an early layer and your own rules in a later one. You then never have to out-weigh the library.
Refactoring example
This rule is hard to override:
#sidebar .menu ul li a.active { color: red; } /* (1,3,3) */Replace it with a class on the link:
.menu-link.active { color: red; } /* (0,2,0) */Any later rule with two classes, or any rule in a later layer, can now change it without extra weight.
Exercise
A button has the rule div#toolbar button.primary { background: navy; }. You want a .danger class to turn it red, using one class only. Why does .danger { background: red; } fail, and how would you restructure the CSS so it works without !important?
Show answer
The original selector is (1,1,2) and .danger is (0,1,0), so the ID makes the first rule win regardless of order. Rewrite the original as .primary { background: navy; } (0,1,0), then place .danger { background: red; } after it. The two now tie on specificity, and the later rule wins.