References

Beginner-friendly references for web development, with live, editable examples.

The CSS @layer at-rule

At-rule CSS All modern browsers Updated
Quick answer

The CSS @layer at-rule creates cascade layers that order your styles by intent — e.g. @layer reset, base, components, utilities;. A rule in a later layer beats one in an earlier layer regardless of specificity, which ends specificity wars and makes it predictable whose styles win. Unlayered styles win over all layers.

Overview

@layer adds a new, higher-priority dimension to the cascade. Normally, when two rules conflict, specificity decides the winner — which is how you end up with selector arms races and a trail of !important. Layers change the rules: declare your layers in order, and a rule in a later layer beats a rule in an earlier layer no matter how specific the earlier one is.

The pattern is to name your layers up front in priority order — @layer reset, base, components, utilities; — then assign rules to them. Now a low-specificity utility in the utilities layer can override a high-specificity component rule simply because its layer comes later. Intent, not selector weight, decides the outcome.

This is transformative for managing CSS at scale, especially third-party code: drop a framework into an early layer and your own styles in a later one, and your overrides just work without !important. One rule to remember: unlayered styles win over all layered styles, so anything not in a layer sits on top of the whole stack.

Syntax

/* declare the order once, up front */
@layer reset, base, components;

@layer base {
  a { color: blue; }
}

@layer components {
  /* wins because its layer comes later */
  a { color: rebeccapurple; }
}

Example

Live example
<style>
  @layer base, theme;
  @layer base { .badge { background: #94a3b8; } }
  @layer theme { .badge { background: #1c7ce9; } }
  .badge { color: #fff; padding: 12px 18px; border-radius: 8px; font: 600 14px system-ui, sans-serif; display: inline-block; }
</style>
<span class="badge">The "theme" layer comes later, so I am blue</span>

Best practices

  • Declare the full layer order once at the top so priority is explicit and predictable.
  • Put resets and third-party frameworks in early layers, and your own overrides in later ones.
  • Remember unlayered styles beat every layered style — keep that in mind when something stubbornly wins.
  • Combine with @import url(framework.css) layer(framework) to slot external CSS straight into your layer order.

Frequently asked questions

What does @layer do in CSS?
It defines cascade layers that order styles by intent. A rule in a later layer beats one in an earlier layer regardless of specificity.
Do cascade layers override specificity?
Within the layered cascade, yes — layer order is checked before specificity. A simple selector in a later layer beats a complex one in an earlier layer.
How do I set the order of cascade layers?
Declare them in one statement, e.g. @layer reset, base, components;. The order listed is the priority order, lowest to highest.
Do unlayered styles win over layered ones?
Yes. Any style not assigned to a layer takes priority over all layered styles, sitting at the top of the layer stack.