Sub topic 11
Rolling back with revert-layer
This chapter assumes you can declare layers with @layer, set their order with an order statement, and read a rule's specificity. It also assumes you know that a later layer beats an earlier one for normal declarations. If any of that is shaky, review the earlier chapters on layer order and layer-versus-specificity first.
What revert-layer does
revert-layer is a CSS-wide keyword, like inherit, initial, unset, and revert. You can use it as the value of any property. It means: "Ignore everything the current layer says about this property on this element, and use the value the next lower layer would have produced."
Without the keyword, you can't undo a higher layer's value. You could rewrite the lower layer's value in the higher layer, but that duplicates it and goes stale when the lower value changes. revert-layer reads the lower value at the moment the browser resolves the cascade.
Here is the canonical example:
@layer base, components;
@layer base {
button { padding: 1rem; }
}
@layer components {
button { padding: 2rem; }
button.plain { padding: revert-layer; }
}A plain <button> gets 2rem padding from components. A <button class="plain"> gets 1rem. The button.plain rule has higher specificity than button, so inside components its revert-layer value wins. The browser then rolls that property back to the base layer, where the value is 1rem.
The rollback model
When the browser resolves a property, it sorts all matching declarations by origin, importance, layer, specificity, and source order. If the winning declaration is revert-layer, the browser discards every declaration for that property that lives in the same layer as the winner. It then picks a winner again from what remains, with the lower layers now on top.
Three consequences follow from this model.
- It is per property.
padding: revert-layerleavescolor,margin, and every other property alone. - It discards the whole layer's contribution for that property. That includes declarations in other rules of the same layer, not only the rule that contains the keyword.
- It can chain. If the newly chosen winner is also
revert-layer, the browser repeats the step and moves down another layer.
It discards the whole layer, not just one rule
This is the detail most often misunderstood. Look at this stylesheet:
@layer base, components;
@layer base {
a { color: royalblue; }
}
@layer components {
a { color: black; }
.nav a.plain { color: revert-layer; }
}For <a class="plain"> inside .nav, the components layer has two matching color declarations. The .nav a.plain rule wins inside the layer by specificity, so the winner is revert-layer. The browser discards both components declarations, including a { color: black; }, and the link is royalblue.
You might expect the rollback to land on black, the next-best declaration in the same layer. It doesn't. The rollback goes to the layer below, not to the runner-up.
Layers above are unaffected
revert-layer only removes the layer it appears in. Layers declared later in the order still apply on top.
@layer base, components, utilities;
@layer base {
.badge { background: lightgray; }
}
@layer components {
.badge { background: gold; }
.badge.quiet { background: revert-layer; }
}
@layer utilities {
.bg-teal { background: teal; }
}<span class="badge">is gold.<span class="badge quiet">is light gray, frombase.<span class="badge quiet bg-teal">is teal. Theutilitieslayer comes aftercomponentsin the order, so its declaration wins before any rollback is needed.
Use this when you want a rule to step out of the way of one layer without stepping out of the whole cascade.
Comparing the CSS-wide keywords
The keywords differ in how far back they reach.
| Keyword | Reaches back to | Typical use |
|---|---|---|
| `initial` | The property's specification default, ignoring all styles | Force a known default value |
| `inherit` | The parent element's computed value | Copy the parent's value |
| `unset` | `inherit` for inherited properties, `initial` for others | Clear a value with natural behavior |
| `revert` | The previous origin: user styles, then browser defaults | Undo all author styles for a property |
| `revert-layer` | The next lower layer in the same origin | Undo one layer's styles for a property |
revert and revert-layer are closely related. revert skips the whole author origin, so every layer and all unlayered styles are ignored. revert-layer steps back one layer at a time and keeps the author styles below.
If no lower layer sets the property, revert-layer continues the way revert does. The browser tries user styles and then its default stylesheet, so a button returns to its default padding, for example.
Using it with shorthands and all
A shorthand property is a property that sets several longhand properties at once. revert-layer on a shorthand rolls back all its longhands:
@layer base, components;
@layer base {
.panel { border: 1px solid #ccc; padding: 1rem; }
}
@layer components {
.panel { border: 3px double navy; padding: 2rem; }
.panel.flat { border: revert-layer; }
}The .panel.flat element gets border-width, border-style, and border-color from base. Its padding is still 2rem, because only border was rolled back.
The all shorthand works the same way. all: revert-layer rolls back every property in the current layer at once, which suits a "turn off this layer's styling here" escape hatch:
@layer base, components;
@layer base {
.doc p { margin-block: 1em; line-height: 1.5; }
}
@layer components {
.card p { margin-block: 0; line-height: 1.2; color: #333; }
.card .raw p { all: revert-layer; }
}Inside .card .raw, paragraphs lose the components styles and use the base ones. Remember that all does not cover custom properties, direction, or unicode-bidi. A custom property set in the discarded layer stays in effect unless you roll it back by name.
Unlayered styles
Unlayered author styles behave as if they sit in one implicit layer above every named layer. So revert-layer inside an unlayered rule rolls the property back to the layered styles:
@layer base, components;
@layer base {
a { text-decoration: underline; }
}
@layer components {
a { text-decoration: none; }
}
a.legal { text-decoration: revert-layer; }The unlayered a.legal rule wins over the layered ones. Its value is revert-layer, so the browser drops the unlayered declarations for text-decoration and picks the winner among the layers. The components layer is the highest, so the result is none. This is useful for removing an unlayered override without knowing which layer's value should apply.
Common mistakes
- Expecting a rollback to the same layer's runner-up. As shown above, all same-layer declarations for that property are dropped.
- Using it in the lowest layer. In the first layer of the order, there is no lower layer to roll back to. The result falls through to user and browser styles, like
revert. That is rarely what you meant, so check which layer the rule is in. - Using it on a property the lower layer never sets. The property then falls back to the browser's default or to inheritance, not to "nothing changes."
- Confusing it with
revert.revertthrows away all author styles for the property. If arevertmakes your button look unstyled, you probably wantedrevert-layer.
Exercise
Given the CSS below, what is the color of <a class="plain"> inside an element with class nav, and why?
@layer base, components;
@layer base { a { color: royalblue; } }
@layer components {
a { color: black; }
.nav a.plain { color: revert-layer; }
}
Show answer
It is royalblue. Inside the components layer, .nav a.plain has higher specificity than a, so its revert-layer value wins within that layer. The browser then discards all components declarations for color, including a { color: black; }, and uses the value from the next lower layer, base.
Exercise
You have @layer base, components, utilities;. A .card rule in components sets box-shadow: 0 2px 8px #0004, and base sets .card { box-shadow: none; }. Write a components rule for .card.flat that gives it the base value without repeating none. Then say what happens if an element also has the utility class .shadow-lg defined in utilities with its own box-shadow.
Show answer
Use .card.flat { box-shadow: revert-layer; } in the components layer. The element gets none from base. With .shadow-lg, the utilities layer is later in the order, so its box-shadow wins before any rollback is needed, and the element gets the utility's shadow.