Skip to main content

Modern CSS Authoring

Cascade Layers

Written by Updated Cascade Layers exercises

Organize style origins into an explicit priority order without inflating selector specificity. You will define a layer order, place first- and third-party styles, understand unlayered rules, and see why important declarations reverse the order.

Cascade layers let you decide, once, which group of CSS wins: resets lose to base styles, base styles lose to components, components lose to utilities. After that you stop fighting specificity with longer and longer selectors.

Without layers, the only tools for "this should win" are specificity and the order of your files, and both get messy as a project grows. Layers sit above both in the cascade. It helps to understand CSS Specificity first.

Name the layers first, in order

Example

css

@layer reset, base, components, utilities, overrides;

@layer base {
  a { color: #075985; }
}

@layer components {
  .button { color: white; background: #075985; }
}

The first line lists the layers from weakest to strongest. After that, a rule in components beats a rule in base, even if the base selector is far more specific. Writing the order up front matters: if you skip it, layers are ordered by when they first appear, which can change when files are bundled differently.

Three surprises

  • CSS outside any layer beats every layer. Unlayered styles count as the strongest group. That is handy for quick fixes, and confusing if you forget about it.
  • !important flips the order. For important declarations, earlier layers win. So an !important in your reset or base layer cannot be overridden by a later layer, which is deliberate: it protects safety rules.
  • Inside one layer, nothing changes. Specificity and order still decide between rules in the same layer.

Put other people's CSS in its own layer

Example

css

@import url("vendor.css") layer(vendor);

@layer vendor, components, utilities;

A framework or plugin stylesheet goes into a vendor layer listed first, so your own component styles always win over it without !important.

Layer Syntax

SyntaxPurposeExample
Layer order statementDeclares layer priority before rules are added.@layer reset, base, components
Layer blockAdds rules to a named layer.@layer components { ... }
Layered importImports a stylesheet into a layer.@import url("theme.css") layer(theme)
revert-layerRolls a property back to the previous layer.color: revert-layer

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Cascade Layers example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

@layer reset, base, components, utilities, overrides;

@layer base {
  a { color: #075985; }
}

@layer components {
  .button { color: white; background: #075985; }
}

@import url("vendor.css") layer(vendor);

@layer vendor, components, utilities;
  </style>
</head>
<body>
  <main><h1>Layer order</h1><p><a href="#">Base link</a></p><button class="button">Component button</button></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
@layer99999715.4

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Keep it small

Three to five layers is plenty for most sites, such as reset, base, components and utilities. If you find yourself adding a layer for every file, you are back to the same problem in a different form.

Cascade Layers exercises

Declare the order

Declare the layers base and theme in this order. In base make the button gray, in theme make it navy.

The HTML your code works on
<button class="btn">Buy</button>
<p class="note">Note</p>

Result

Check your answer to see it here.