Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

@layer — cascade layers

The @layer rule creates and orders cascade layers, controlling precedence between groups of rules independently of selector specificity.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

@layer [ <layer-name># | <layer-name>?  {
  <stylesheet>
} ]

@layer <layer-name-list>; establishes name order, while @layer <name> { <stylesheet> } places rules in a layer. The first order declaration is significant; reopening an existing layer appends rules without moving it. Anonymous layers have a stable position but cannot be reopened by name. Layers can nest and nested names can be addressed with dot notation.

For normal author declarations, a later layer outranks an earlier one regardless of specificity between the layers. Normal author rules outside every layer outrank all normal layered author rules. This lets a reset sit before component rules without a specificity contest. An @import qualifier can put an entire imported sheet into a layer.

For !important declarations the layer order reverses: earlier layers protect important rules from later layers, and layered important declarations outrank unlayered important declarations. revert-layer rolls back the current layer’s winner, not the entire cascade. The example declares reset before components, opens them in reverse source order, and shows that components win between layers while an unlayered normal rule beats both.

Examples

const style = document.createElement('style');
style.textContent = `@layer reset, components;
@layer components { .probe { --layer-order: components; --origin: layered; } }
@layer reset { .probe { --layer-order: reset; } }
.probe { --origin: unlayered; }`;
document.head.append(style);
const probe = document.createElement('div');
probe.className = 'probe';
document.body.append(probe);
const computed = getComputedStyle(probe);
console.log('layer-order=' + computed.getPropertyValue('--layer-order').trim() + '; origin=' + computed.getPropertyValue('--origin').trim());

Result

layer-order=components; origin=unlayered

The @layer rule creates and orders cascade layers, controlling precedence between groups of rules independently of selector specificity.

Sources