← Back to HTML & CSS, page 3

html-css-0041 · Hard senior

What are CSS cascade layers (@layer), and how do they change specificity conflicts?

A cascade layer is a named group of rules with a rank. When normal declarations compete, a later layer wins over an earlier one regardless of specificity, and unlayered rules beat all layers. Specificity only decides inside one layer. With !important the layer order is reversed.

Full answer

A cascade layer is a named group of rules with a fixed rank. When two normal declarations compete, the browser checks the layer order before specificity. A rule in a later layer beats a rule in an earlier layer, even if the earlier rule has a much more specific selector. Specificity only decides between rules in the same layer.

Declare the order once at the top: @layer base, components, utilities;. Then put rules inside @layer name { ... } blocks, in any file. Rules that are not in any layer are treated as a final layer that beats all named layers for normal declarations. With !important, the order is reversed: an important rule in an earlier layer beats important rules in later layers.

This solves a common large-codebase problem: a third-party stylesheet or an old reset with #id selectors that you cannot override without !important. Put it in an early layer, and your own simple class selectors win.

The first link is blue. Although #page a is far more specific, components comes after base. The second link is green because utilities is the last layer. The third link is orange, since unlayered rules win over all layers for normal declarations.

You can also import a file into a layer with @import url("vendor.css") layer(vendor);, and nest layers as @layer components.buttons { ... }. Layers do not remove the need for good naming. They let you decide, by design, which group may override which.

Check the support table for the browsers you must serve. A stylesheet that depends on layers has no graceful fallback, because browsers that do not know @layer ignore the whole block.

html
<style>
  @layer base, components, utilities;

  @layer base {
    #page a { color: red; }              /* high specificity, early layer */
  }
  @layer components {
    a { color: blue; }                   /* low specificity, later layer: wins */
  }
  @layer utilities {
    .text-green { color: green; }
  }
  a.unlayered { color: orange; }         /* not in a layer: beats every layer */
</style>
<div id="page">
  <a href="#" id="a1">Plain link</a>
  <a href="#" id="a2" class="text-green">Utility link</a>
  <a href="#" id="a3" class="unlayered text-green">Unlayered link</a>
</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Declare the order once: @layer base, components, utilities.
  • Layer order is checked before specificity.
  • Unlayered rules win over layered ones for normal declarations.
  • !important reverses the layer order.
  • Layers let you control overrides without higher specificity or !important.

Common mistakes

  • expecting unlayered styles to be weaker. They are the strongest group for normal declarations, so one stray unlayered rule can override a whole layer.
  • using !important inside layers to win, then finding that important declarations reverse the layer order.

Follow-ups

  • how do layers compare with higher specificity?
  • How do you put an imported stylesheet into a layer?
  • What happens to a layered rule that has the same selector as an unlayered one?

Tips

  • Declare the layer order in one place at the top of your entry stylesheet. A wrong order is hard to spot when layers are declared in several files.
  • Describe the problem layers solve: a third-party file with strong selectors you cannot edit.
  • Say that you declare the order in one place at the top.