CSS Cascade Layers
Control CSS specificity using cascade layers to organize stylesheets and prevent specificity wars in large projects.
Code
@layer base, components, utilities;
@layer base {
h1 { font-size: 2rem; }
}
@layer components {
.btn { padding: 0.5rem 1rem; }
}
@layer utilities {
.mt-4 { margin-top: 1rem; }
}CSS Cascade Layers
Control CSS specificity using cascade layers to organize stylesheets and prevent specificity wars in large projects.
Code
@layer base, components, utilities;
@layer base {
h1 { font-size: 2rem; }
}
@layer components {
.btn { padding: 0.5rem 1rem; }
}
@layer utilities {
.mt-4 { margin-top: 1rem; }
}
Explanation
@layer groups rules into named layers resolved in declaration order, independent of selector specificity. Expected behavior: a later layer always beats an earlier one, so utility layers can override base layers without !important. Caveat: unlayered styles win over every layered style, so third-party rules left outside a layer can still surprise you.
Interactive Tools for This Topic
CSS Specificity CalculatorCalculate CSS selector specificity scores using the standard (a, b, c) notation. Enter any selector and see its inline, ID, class, and element contributions instantly.Aspect Ratio CalculatorSimplify a width-to-height ratio, show its decimal value, and derive a matching height for a new width.Border Radius PreviewPreview four-corner border-radius values on a live box and copy the generated shorthand border-radius CSS declaration.Box Shadow GeneratorBuild CSS box-shadow values with offset, blur, spread, color, opacity, and inset controls, with a live preview of the result.
Report a problem with this exampleOpens a pre-filled issue on GitHub.