Full answer
Specificity wars start when one rule needs to beat another and the only tool in reach is a longer selector, an id or !important. Each override raises the bar for the next one. The fix is to design so that most selectors have the same, low specificity, and so that override order is deliberate.
Practices that work in large projects:
- Style with single classes. A naming scheme such as BEM (
.card,.card__title,.card--featured) gives every part a flat selector of one class. - Avoid ids and avoid qualifying selectors by tag (
div.card). Both raise specificity without benefit. - Keep defaults and resets at zero specificity with
:where(), so any class overrides them. - Order stylesheets from generic to specific: tokens and resets, base elements, components, then utilities. Cascade layers make this order explicit and independent of file order.
- Use custom properties as the configuration surface, so a variant changes a variable, not a whole set of declarations.
- Limit
!importantto a few utility classes where it is the point, and lint for specificity and id selectors with a tool such as Stylelint. - Delete dead CSS and keep component styles near the component.
The danger button changes one variable, so it becomes red without a new set of rules. The base link colour sits behind :where() and is overridden by .link-override without any force, even though the override comes from an unlayered rule. The utility mt-0 wins over base margins because its layer is later.
When a conflict appears, resist the urge to lengthen the selector. Ask which of the two rules should rank higher by design, and then move that rule to a layer or reorder instead.
<style>
@layer base, components, utilities;
@layer base {
:where(p, li) { line-height: 1.5; margin: 0 0 0.5rem; }
:where(.prose) a { color: #0a6cff; }
}
@layer components {
.btn {
--btn-bg: #0a6cff;
padding: 0.5rem 1rem; border: 0; border-radius: 4px;
background: var(--btn-bg); color: #fff;
}
.btn--danger { --btn-bg: crimson; }
.btn--small { padding: 0.25rem 0.5rem; }
}
@layer utilities {
.mt-0 { margin-top: 0; }
}
</style>
<div class="prose">
<p id="p">A paragraph with a <a href="#" id="link">link</a>.</p>
<a href="#" class="link-override" id="link2">Another link</a>
</div>
<button class="btn btn--danger btn--small mt-0" id="btn">Delete</button>
<style>
.link-override { color: green; }
</style>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Prefer flat, single-class selectors with a naming scheme such as BEM.
- Keep resets at zero specificity with :where().
- Use layers to define override order on purpose.
- Use custom properties so variants change a value, not a whole rule.
- Limit !important and lint for ids and high specificity.
Common mistakes
- styling a component by its position in the page (.sidebar .card h3). The same component then looks different elsewhere, and overrides need longer selectors.
- copying one-off fixes into the global file. They collect over time and no one knows which are still needed.
Follow-ups
- BEM, utility classes or CSS modules?
- BEM, utility classes or CSS modules: how do you choose?
- How do you remove dead CSS safely?
Tips
- Treat !important as a debt. If you need it to beat your own CSS, the structure needs fixing.
- Say that you decide which rule should win by design, then move it, rather than lengthening a selector.
- Choose one convention per project and enforce it with a linter.