🎨 The Style With Higher Specificity That Still Lost
Cascade layers introduce a ranking that is applied before specificity is ever considered at all – rules in a layer declared later always beat rules in a layer declared earlier, no matter how specific either selector is, and any unlayered rule beats every layered rule entirely. A single, broadly-scoped selector sitting in a layer declared after your component’s own layer can override a precise, deeply specific selector in that component without using an importance flag or even a particularly strong selector, because layer order already decided the outcome before specificity ever entered into it.
🔎 The Problem
@layer base, components, utilities;
@layer components {
.card__title {
color: #1e293b; /* high specificity, feels like it should win */
}
}
@layer utilities {
[class] {
color: inherit; /* almost no specificity, in a LATER layer */
}
}
/* The utilities layer was declared after components in the @layer
statement at the top, so EVERY rule in utilities outranks EVERY
rule in components for any property both set - the title's color
loses to the attribute selector's color, even though that selector
has far lower specificity on its own. */
✅ Fix: Treat Layer Order as Deciding Before Specificity Even Starts
- Treat the order inside the layer statement itself as the single most important decision in the cascade – list layers from least to most important and keep that order consistent across every file that declares or adds to those layers.
- Before debugging a specificity mismatch as though cascade layers did not exist, check the computed styles panel in your browser’s developer tools for which layer each competing rule belongs to – a browser that supports layers shows this directly, and it settles the question immediately instead of guessing from selector specificity alone.
- Keep broad, low-specificity utility selectors in a layer declared earlier than component-specific styles if you want component styles to override them normally – utilities meant to win over everything, as deliberate overrides, belong in a layer declared last, on purpose, not by accident of file order.
⚠️ Why This Is Easy to Miss
- Every specificity rule you already know keeps working exactly as expected within a single layer, so the behavior only looks wrong once two competing rules for the same property live in two different layers – an easy detail to lose track of once a codebase has more than a couple of layer blocks spread across files.
- The statement declaring the overall layer order is often written once, early, in a single base stylesheet – a rule added much later in a completely different file can land in an earlier-declared layer and lose to something that looks far less specific, with nothing at that later file’s own location suggesting why.
Specificity has always decided the tiebreaker inside one layer – a cascade layer decides which layer’s tiebreaker you are even allowed to have.




