How CSS revert-layer Works in Cascade Layers
The CSS revert-layer keyword allows developers to step
backward through the cascade hierarchy, rolling a property's value back
to the declaration set in the previous cascade layer. This article
explains the evaluation order of cascade layers, how
revert-layer determines which value to apply next, and how
its behavior distinctively differs from other reset keywords like
revert and unset.
The Role of Cascade Layers
CSS Cascade Layers (@layer) establish an explicit
priority order for author styles, independent of selector specificity.
Layers defined later in the stylesheet take precedence over layers
defined earlier. Outside of these, unlayered author styles maintain the
highest priority over all layered author styles.
@layer base, theme, components;In this order, rules inside components override matching
rules inside theme, which in turn override
base.
How revert-layer
Evaluates Values
When revert-layer is assigned to a CSS property, the
browser ignores the current layer's declaration for that property and
searches for the next winning value as if the current layer does not
declare it.
The evaluation follows this precise path:
- Previous Author Layers: The browser inspects the layer immediately preceding the current one in the layer order. If a matching rule exists there, that value is applied.
- Next Preceding Layers: If no match exists in the immediate predecessor, the browser continues moving backward through earlier author layers until a match is found.
- Implicit Layer / Other Origins: If the keyword is
invoked in the lowest-priority author layer (or if no previous author
layer matches the selector), the property rolls back to matching
declarations in subsequent origins:
- The user style sheet (if present).
- The user-agent style sheet (browser default styles).
- Unlayered Styles: If
revert-layeris used inside an unlayered author rule, it rolls back the property to the highest-priority declared@layer.
Practical Example
Consider the following cascade configuration:
@layer base {
p {
color: red;
}
}
@layer theme {
p {
color: blue;
}
}
@layer components {
p.alert {
color: revert-layer;
}
}When evaluating a <p class="alert">:
- The browser checks
components. It seescolor: revert-layer. - It steps back to the
themelayer and findsp { color: blue; }. - The computed color becomes
blue. - If
themehad no declaration forp, the browser would continue back tobaseand resolve tored.
revert-layer vs.
revert
While both keywords roll back values, their scopes are different:
revertdiscards all author-level styles entirely for that declaration, rolling back directly to the user or user-agent style sheet regardless of any other cascade layers defined by the author.revert-layeronly steps out of the current layer. It preserves matching declarations from lower-priority layers within the same author origin before falling back to user or user-agent defaults.