Specificity was a reasonable rule until every team owned a slice of the CSS and "just make this selector stronger" became the culture. Cascade layers (@layer) add an author-controlled order that beats specificity between layers. A .btn in a later layer wins over a .header .nav .btn in an earlier layer, without a specificity arms race.
Unlayered CSS still wins
Declarations not in a layer are treated as a final implicit layer — they beat layered styles. That is the trap when you mix a layered design system with a random <style> in a template. Put everything in a named layer, or you will debug "why didn't my layer apply" for an afternoon.
@layer reset, tokens, third-party, components, utilities, overrides;
@layer reset { /* box-sizing, margins */ }
@layer components {
.button { padding: 0.5rem 1rem; }
}
@layer utilities {
.p-0 { padding: 0; }
}Utilities in a later layer can zero padding on a button without !important. Overrides are for the rare app-level exception, not for every page.
Third-party sheets
Import Bootstrap or a date-picker into @layer third-party so your components layer can override it with ordinary class names. Without a layer, vendor selectors with IDs will keep winning.
!important still exists and still inverts the cascade in painful ways. Layers reduce how often you reach for it; they do not make important go away inside a layer.
Migration
Start by wrapping existing files in @layer leftover and declare leftover last among app layers. New code goes into the proper layer. Shrink leftover over time. Shadow DOM and Tailwind's own layer system (@layer base/components/utilities) need a documented map so you do not nest competing orders.
If a PR adds a selector with three classes "for specificity," that is the signal to use a layer (or a utility) instead. The cascade should be an architecture, not a high-score list.
A layer map that survives a design system
Write the layer order in one file that every entrypoint imports first. The names are a contract: tokens never override components; utilities may override components; overrides are last and rare. When a new package lands, it gets a layer assignment in that file, not a comment in a Slack thread.
Shadow DOM still has its own cascade. A layered page stylesheet does not automatically win inside a closed shadow root. Document which tokens pierce (:host, CSS variables on :root) and which component styles stay encapsulated. Teams that treat @layer as a substitute for shadow boundaries end up with parts styled twice.
Tailwind’s @layer base, components, utilities is a second order. Nesting Tailwind inside your utilities layer, or importing it unlayered, will scramble which .p-0 wins. Pick one: either Tailwind owns the utility layer and your components sit earlier, or you disable Tailwind layers and emit utilities into your named map. Both work. Two maps do not.
Failure modes
Unlayered leftovers. A CMS “custom CSS” field, a Storybook decorator, or a style attribute on a layout component is unlayered and beats everything. style="" is not a layer problem — inline styles are a different cascade origin — but unlayered author CSS is. Hunt <style> tags in templates during the migration.
!important inside an early layer. Important declarations compare across layers in reverse order: important in reset can beat important in overrides. If someone “fixes” a token with !important in reset, you will spend a day learning that rule. Ban important except in the documented override layer, and even then prefer a later layer without important.
Import order versus @layer names. @import "vendor.css" layer(third-party) is the reliable wrap. Importing vendor CSS without the layer() function, then hoping a later @layer third-party wraps it, does not rewind the unlayered sheet.
When layers are the wrong tool
A single-author stylesheet of a few hundred lines does not need seven layers. Specificity and source order are enough. Layers pay off when multiple teams and vendors share one document. They also do not replace cascade origins (user agent, user, author) or @scope. If the bug is “this component should not see page styles,” encapsulation or a shadow root is the fix, not a later layer.
Review checklist
- Layer order is declared once; new files only fill named layers.
- No unlayered author CSS in app templates; leftover is a named, shrinking layer.
- Vendor CSS enters through
layer(). - A PR that adds three classes “to win” is sent back to change layer or use a utility.
A worked failure mode
@layer is adopted but a third-party stylesheet is unlayered and always wins. !important is still used inside layers, undoing the point. Order of layers is declared differently in two bundles. The failure is layers without a total order. Define layer order once, import third parties into a layer, and ban important except overrides you document.
When this is the wrong tool
Cascade layers are the wrong tool for a 20-line page. They will not fix inline styles from a CMS. Do not mix layers and a CSS-in-JS soup without a plan. Use layers when many sources compete.
A worked anti-pattern: the team ships the architecture, then staffs it like a toy. "CSS Cascade Layers: Ending Specificity Wars Without !important" needs boring operations—backups, timeouts, ownership, and a budget for the tax the idea always charges (compaction, replay, dual writes, extra latency, extra types). Unstaffed taxes come due at 2am. Put the tax in the design doc's cost section. If leadership wants the benefit without the tax, the honest answer is a smaller idea, not a heroic on-call rotation.