On this page11 sections
Lecture 027: Cascade Layers
| Category | 6: CSS in Depth |
| Lecture | 027 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
On a big project, styles come from many places: a reset, a framework, your components, quick utilities. Cascade layers (
@layer) let you decide which group wins, whatever the specificity of the selectors inside. That ends most specificity battles.
Learning Objectives
- Put rules into layers with
@layer - Set the order of layers in one line
- Understand how layers beat specificity
- Import a third-party stylesheet into a layer
1. The Problem
A framework uses a very specific selector, and your simple class can't beat it:
/* framework.css */
.card .content p.text { color: grey; }
/* your CSS: loses, because the selector above is more specific */
.intro { color: navy; }2. Layers to the Rescue
Later layers win, whatever the specificity inside them.
Example
<style>
@layer framework, components; /* the order: components beats framework */
@layer framework {
.card .content p.text { color: grey; }
}
@layer components {
.intro { color: navy; font-weight: bold; }
}
</style>
<div class="card"><div class="content">
<p class="text intro">Navy and bold: the components layer wins despite its simpler selector.</p>
</div></div>Within a single layer, normal specificity rules still apply.
3. Declaring the Order First
Write the order once, at the very top. Then you can add rules to any layer, anywhere:
@layer reset, base, components, utilities;
@layer utilities { .hidden { display: none; } } /* written first, but still wins */
@layer reset { * { margin: 0; } }A common order:
| Layer | Contains |
|---|---|
reset | Removing browser defaults |
base | Element styles: body, h1, a |
components | .card, .button, .navbar |
utilities | Small helpers such as .text-center, .hidden |
4. Unlayered Styles Win
Styles not in any layer beat all layered styles.
Example
<style>
@layer base { p { color: grey; } }
p { color: crimson; } /* not in a layer, so it wins */
</style>
<p>Crimson.</p>That's handy: put third-party CSS in layers, and your own plain CSS automatically wins.
5. Importing Into a Layer
@import url("framework.css") layer(framework);Everything from that file now lives in the framework layer.
6. !important Reverses the Order
For !important declarations, earlier layers win. This lets a reset layer protect a few
critical rules. Like !important in general, it's rarely needed.
Common Beginner Mistakes
1. Not declaring the order. Layers are ordered by when they're first mentioned, which can be surprising. Declare the order at the top.
2. Expecting a layered rule to beat unlayered CSS. Unlayered styles always win over layered ones.
3. Too many layers. Four or five is plenty.
Practice Questions
- Which keyword creates a cascade layer?
- Write the line that orders three layers:
reset,base,components. - In that order, which layer wins a conflict?
- Does specificity matter between layers?
- A rule outside any layer conflicts with one in
components. Which wins? - How do you put an imported stylesheet into a layer?
Practice Answers
@layer@layer reset, base, components;components, the last one.- No. Layer order is decided first; specificity only matters within the same layer.
- The rule outside any layer.
@import url("file.css") layer(name);
Key Takeaways
@layer a, b, c;sets the order: later layers win.- Layer order beats specificity; specificity still works inside a layer.
- Unlayered styles beat all layers.
- Put frameworks in a layer so your own CSS wins easily.