On this page11 sections
  1. Learning Objectives
  2. 1. The Problem
  3. 2. Layers to the Rescue
  4. 3. Declaring the Order First
  5. 4. Unlayered Styles Win
  6. 5. Importing Into a Layer
  7. 6. !important Reverses the Order
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 027: Cascade Layers

Category6: CSS in Depth
Lecture027 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelIntermediate

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:

css
/* 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

html
<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:

css
@layer reset, base, components, utilities;

@layer utilities { .hidden { display: none; } }   /* written first, but still wins */
@layer reset     { * { margin: 0; } }

A common order:

LayerContains
resetRemoving browser defaults
baseElement styles: body, h1, a
components.card, .button, .navbar
utilitiesSmall helpers such as .text-center, .hidden

4. Unlayered Styles Win

Styles not in any layer beat all layered styles.

Example

html
<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

css
@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

  1. Which keyword creates a cascade layer?
  2. Write the line that orders three layers: reset, base, components.
  3. In that order, which layer wins a conflict?
  4. Does specificity matter between layers?
  5. A rule outside any layer conflicts with one in components. Which wins?
  6. How do you put an imported stylesheet into a layer?

Practice Answers

  1. @layer
  2. @layer reset, base, components;
  3. components, the last one.
  4. No. Layer order is decided first; specificity only matters within the same layer.
  5. The rule outside any layer.
  6. @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.