Cascade layers, specificity and the @layer at-rule
Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.
What you will be able to do
#- Order the cascade by layer rather than by specificity wars.
- Explain how layers interact with origin and importance.
Overview
#The cascade decides which declaration wins. Its order is origin and importance first, then cascade layers, then specificity, then order of appearance. @layer lets you group rules into named layers and control their precedence explicitly, so you stop escalating specificity to override earlier CSS.
@layer reset, base, components, utilities;
@layer base {
a { color: #0072f5; }
}
@layer components {
.button { color: #fff; }
}
@layer utilities {
.text-muted { color: #737373; }
}
Layers are ordered by their first declaration. Here utilities wins over components, which wins over base, regardless of selector specificity. A single class in a later layer beats a deeply nested selector in an earlier layer. That property is what makes layers valuable: design-system CSS can be overridden by application CSS without !important or selector gymnastics.
Unlayered styles have higher precedence than any layered style. That is deliberate: it gives you an escape hatch for one-off overrides. But it also means a stray unlayered rule can silently beat your entire layered system, so keep unlayered CSS minimal.
/* unlayered: beats all layers */
.legacy-widget { color: red; }
Important declarations reverse the layer order: an !important in an earlier layer beats an !important in a later layer. This is the opposite of normal declarations. Treat !important as a last resort and prefer layer ordering, because layers express intent while !important only wins.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Declare three layers in precedence order reset, base, components, and put a base link color in base and a component button color in components.
starter.css/* add the layer order and rules */Reveal the reference answer
reference.css@layer reset, base, components; @layer base { a { color: #0072f5; } } @layer components { .button { color: #fff; } }Authored expected output: A layer order is established and rules are grouped into base and components layers.
The first line orders the layers; later-layer rules win over earlier-layer rules regardless of specificity.
Exercise 2Predict
Given @layer a, b; with a .x selector in layer a and a #id selector in layer b, which color wins for an element matching both?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.css@layer a, b; @layer a { .x { color: red; } } @layer b { #id { color: blue; } }
Common misconceptions
#- Specificity still decides between rules in different layers.
- Unlayered styles lose to layered styles.
Follow-up practice
#- html-css-28-custom-properties-tokens
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.