Reading history

CSS Cascade Layers

5 min read 0 views

Learn CSS cascade layers with @layer: set the order of style groups, beat specificity, tame third-party CSS and understand !important.

In a big project, styles come from many places: a reset, your base styles, component styles, a third-party library and small utility classes. When two rules clash, the winner is decided by specificity, which often leads to long selectors and !important everywhere. Cascade layers, written with @layer, let you decide the order of these groups yourself. A later layer wins over an earlier one, however specific the earlier selector is.

In this lesson you will learn

  • Declaring layers and their order with @layer
  • Why layer order beats specificity
  • How unlayered styles fit in
  • Putting third-party CSS in its own layer
  • How !important behaves inside layers

You should know specificity and the cascade.

Layer Order Beats Specificity

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Cascade layers</title>
  <style>
    /* Declare the order once: later layers win, whatever the specificity */
    @layer reset, base, components, utilities;

    @layer components {
      #main .card .btn { background: #1e88e5; color: white; }   /* very specific */
    }
    @layer utilities {
      .bg-danger { background: #e53935; }                       /* simple class */
    }
    @layer base {
      body { font-family: sans-serif; margin: 12px; font-size: 14px; }
      .btn { padding: 8px 14px; border: 0; border-radius: 6px; margin-right: 6px; }
    }
    @layer reset {
      * { box-sizing: border-box; }
    }
    /* Styles outside any layer beat every layer */
    .unlayered-note { color: #6a1b9a; font-weight: bold; }
  </style>
</head>
<body>
  <div id="main">
    <div class="card">
      <button class="btn">Normal button</button>
      <button class="btn bg-danger">Danger button</button>
    </div>
  </div>
  <p>The danger button is red: the <code>utilities</code> layer comes after <code>components</code>, so its simple <code>.bg-danger</code> wins over a selector with an ID.</p>
  <p class="unlayered-note">Unlayered styles win over all layers.</p>
</body>
</html>

Result

  • The first line, @layer reset, base, components, utilities;, sets the order once. Later layers in this list win over earlier ones.
  • The blocks can then appear in any order in the file. base is written after utilities here, but it still loses to it.
  • In components, the selector #main .card .btn has an ID, so it is very specific. Normally a single class could never beat it.
  • But .bg-danger is in the utilities layer, which comes later. The layer decides first, so the danger button is red.
  • Specificity still matters inside a layer, just as before.
  • Styles that are not in any layer are treated as the last, strongest layer.
@layer reset, base, components, utilities;resetbasecomponentsutilitiesunlayered stylesstrongerweakeststrongestA later layer wins, even with a simpler selector.Specificity only decides between rules in the same layer.

Taming Third-Party CSS

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Taming a third-party stylesheet</title>
  <style>
    @layer vendor, site;

    /* Pretend this block came from a library you cannot edit */
    @layer vendor {
      .alert { padding: 12px; border: 2px solid #f9a825; background: #fff8e1; border-radius: 4px; }
      div.alert.alert-box p { color: #8d6e63; font-style: italic; }
    }

    /* Your own styles: one simple class is enough to override the library */
    @layer site {
      body { font-family: sans-serif; margin: 12px; font-size: 14px; }
      .alert p { color: #1b5e20; font-style: normal; }
      .alert { border-radius: 12px; }
    }

    /* !important reverses the order: an important rule in an EARLIER layer wins */
    @layer vendor { .lock { text-decoration: underline !important; } }
    @layer site   { .lock { text-decoration: none !important; } }
  </style>
</head>
<body>
  <div class="alert alert-box"><p>Library alert, restyled by the site layer.</p></div>
  <p class="lock">This text stays underlined: with !important, the earlier layer wins.</p>
</body>
</html>

Result

  • The library’s styles go into a vendor layer that comes before your site layer.
  • The library uses a heavy selector, div.alert.alert-box p. Your plain .alert p still wins, because site is the later layer. No long selectors, no !important.
  • You can also load a whole stylesheet into a layer: @import url("library.css") layer(vendor);.
  • !important reverses the order: an important rule in an earlier layer wins over an important rule in a later one. That lets a reset or library protect a rule that must never be overridden. The second line stays underlined for this reason.
LayerTypical contents
resetbox-sizing, margin resets
baseFonts, colours, default element styles
vendorThird-party libraries
componentsButtons, cards, navigation
utilitiesSmall single-purpose classes like .hidden

Common Mistakes

  1. Not declaring the order first. Without the opening @layer a, b, c; line, layers are ordered by where they first appear, which is easy to get wrong.
  2. Leaving some styles unlayered by accident. They beat every layer, which can look like layers “don’t work”.
  3. Expecting !important to work the normal way across layers. It reverses the order.
  4. Creating too many layers. Four or five clear groups are easier to reason about than twenty.

Practice Exercises

Exercise 1. With @layer base, theme;, the rule body main p.intro { color: black; } is in base and p { color: navy; } is in theme. What colour is <p class="intro"> inside main?

Show answer

Navy. theme is the later layer, so its rule wins even though its selector is much less specific.

Exercise 2. Load a file called bootstrap.css into a layer named framework that your own styles can easily override.

Show answer
@layer framework, site;
@import url("bootstrap.css") layer(framework);

@layer site {
  .btn { border-radius: 999px; }
}

Note that @import must come before other rules in a stylesheet, except @layer order statements.

Frequently Asked Questions

Can layers be nested?

Yes. You can write @layer components { @layer buttons { ... } }, and refer to it as components.buttons. Nested layers are ordered inside their parent layer.

Do browsers support cascade layers?

Yes. All major browsers have supported @layer since 2022, so it is safe to use today.

Summary

  • @layer a, b, c; sets an order; later layers win over earlier ones.
  • Layer order is checked before specificity, so simple selectors can win.
  • Unlayered styles beat all layers.
  • Put libraries in an early layer; !important reverses layer order.

Next: CSS Counters and List Styling.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *