Sub topic 11
Rolling back with revert-layer
This chapter covers the revert-layer keyword. It assumes you know basic CSS syntax, selector specificity, how to declare layers with @layer, and how layer order and unlayered styles work. You should also be comfortable reading applied styles in DevTools.
What revert-layer does
Normally, when a declaration wins the cascade, the values from lower-priority layers are discarded. revert-layer changes this for a single property. It tells the browser to ignore the value from the current layer and to resolve the property as if the current layer had not set it. The browser then uses the next-best value from an earlier layer.
In practice, you can say "undo what this layer did here, and give me whatever the layers beneath me decided."
A worked example
Here, components sets a large padding on every button. One variant, button.plain, wants the padding that base defined instead.
@layer base, components;
@layer base {
button { padding: 1rem; }
}
@layer components {
button { padding: 2rem; }
button.plain { padding: revert-layer; }
}For a plain <button>, the winning rule is button { padding: 2rem; } from components. For <button class="plain">, the winning rule is button.plain, because it has higher specificity within the same layer. Its value is revert-layer, so the browser rolls back the components layer's contribution for this property on this element. It falls back to base, and the padding is 1rem.
You did not need to repeat 1rem in the variant. If someone later changes the base padding, the plain button follows the change.
The resolution steps
How far back it goes
revert-layer rolls back only the layer that contains the declaration. It does not undo everything. The browser looks at the layers below the current one, in cascade order, and picks the winner among them as usual.
- If an earlier layer sets the property, that value wins.
- If no earlier layer sets it, the browser keeps going down to the origin's remaining styles. Eventually it reaches the user-agent default (or an inherited value, for inherited properties).
- Within an unlayered rule,
revert-layerbehaves likerevert, because unlayered styles are treated as one implicit final layer and there is nothing to roll back to other than the origin below.
Compare it with the related keywords:
| Keyword | Rolls back to |
|---|---|
| `revert-layer` | The value from an earlier layer in the same origin |
| `revert` | The value from an earlier origin, such as the user-agent default |
| `initial` | The property's initial value from the CSS specification |
| `unset` | The inherited value, or the initial value if the property is not inherited |
Common mistakes
- Expecting it to skip only one rule. It discards the whole layer's contribution for that property, not just one competing rule. Other rules in the same layer that set the property are ignored too.
- Using it with no earlier layer. If
componentsis the first layer, there is nothing beneath it to fall back to. The result is the browser default, which may surprise you. - Confusing layer order with source order. The "earlier" layer is the one declared earlier in the order statement, not the one written first in the file. Check your
@layerorder line. - Forgetting
!importantreverses order. Rolling back an!importantdeclaration still follows the reversed layer priority, so the layer you land on may be the opposite of what you expect.
Checking it in DevTools
Open the element in the Styles pane. Browsers that support layers group rules under their layer names. The padding: revert-layer declaration appears as the winner, and the components value is struck through. Find the base group to confirm that its value is the one applied in the Computed pane.
Exercise
Given the CSS below, what is the padding of <a class="flat">, and why?
@layer reset, theme;
@layer reset {
a { padding: 4px; }
}
@layer theme {
a { padding: 12px; }
a.flat { padding: revert-layer; }
}
Show answer
The padding is 4px. The a.flat rule wins within the theme layer because of its higher specificity, and its value is revert-layer. That discards the theme layer's value for this element and property, so the browser uses the next-best value from the earlier reset layer, which is 4px.
Exercise
A project has the layer order base, components, and base does not set margin at all. A components rule sets margin: revert-layer on .card. What margin does .card get?
Show answer
It gets the user-agent default margin for the element, because no earlier layer sets margin. The browser keeps rolling back through earlier origins until it finds a value. For a div this is 0. For a p it is the default paragraph margin.