Sub topic 10
inherit, initial, unset, revert and the all property
This chapter assumes you know how specificity, source order, origins and layers decide a winning declaration, and that inherited and non-inherited properties behave differently. Here you will learn the five CSS-wide keywords and the all shorthand. Together they let a declaration say "take the value from somewhere else" instead of naming a value.
What a CSS-wide keyword is
Every CSS property accepts five keywords in addition to its own values: inherit, initial, unset, revert and revert-layer. They are called CSS-wide keywords because they work the same way on every property.
A keyword is a declared value, so it goes through the cascade like any other value. It has the specificity of its selector, it can lose to a later rule, and it can be marked !important. Only after the cascade has picked a winner does the browser replace the keyword with an actual value.
p { color: blue; }
#intro { color: inherit; }For <p id="intro">, the id selector wins the cascade. The winning value is the keyword inherit, so the paragraph uses its parent's computed color and ignores the blue.
The next diagram shows what happens once a keyword wins.
inherit
inherit sets the property to the computed value of the same property on the parent element. It works on any property, including those that do not inherit by default.
button,
input,
select,
textarea {
font: inherit;
color: inherit;
}Browsers give form controls their own font and color in the user-agent stylesheet, so they ignore the page's typography. This rule pulls them back in line with the surrounding text.
Two details matter:
inheritcopies the parent's computed value, not the value of some distant ancestor that set it. If the parent did not set the property, the child gets whatever the parent computed, which may itself be inherited or an initial value.- On a non-inherited property,
inheritis a deliberate override.border: inheriton a child copies the parent's whole border, which is rarely what you want but is sometimes useful forheight: inheritorbackground: inherit.
initial
initial sets the property to its initial value, the default defined in the CSS specification for that property. It ignores the parent and also ignores the browser's default stylesheet.
That second point causes the most surprise. The initial value of display is inline, not block. So display: initial on a <div> makes it inline, even though browsers style div as block.
div.flat {
display: initial; /* inline, not block */
margin: initial; /* 0 */
}
.reset-color {
color: initial; /* the spec default, usually rendered as black */
}Initial values come from the specification, so they are the same in every browser. The spec defines the initial color as canvastext, a system color that renders black in a light color scheme. In a dark color scheme it may render white.
unset
unset chooses between the two keywords above, depending on the property:
- If the property is inherited,
unsetbehaves likeinherit. - If the property is not inherited,
unsetbehaves likeinitial.
This makes unset a "put this property back to its natural behavior" keyword. A property that normally flows down from the parent flows down again, and a property that normally starts fresh starts fresh again.
.card {
color: unset; /* inherited property, so same as inherit */
margin: unset; /* not inherited, so same as initial (0) */
}| Keyword | On an inherited property (`color`) | On a non-inherited property (`margin`, `display`) |
|---|---|---|
| `inherit` | Parent's computed value | Parent's computed value |
| `initial` | Spec's initial value | Spec's initial value |
| `unset` | Parent's computed value | Spec's initial value |
| `revert` | Previous origin's value (user or user-agent) | Previous origin's value (user or user-agent) |
Like initial, unset ignores the user-agent stylesheet. display: unset on a <div> gives inline, because display is not inherited and its initial value is inline.
revert
revert rolls the cascade back to an earlier origin. Called from an author stylesheet, it discards every author declaration for that property on the element. The value then comes from the user origin if the user set one, and from the user-agent stylesheet if not. If no origin supplies a value, revert behaves like unset.
Where initial means "the spec default", revert means "what the browser would have done if I had not styled this".
.article-body ul {
list-style: revert;
padding-inline-start: revert;
}Suppose a site-wide reset has set ul { list-style: none; padding: 0; }. Inside .article-body, the rule above restores the bullets and the default indent. If you used initial on list-style, you would get the spec's initial disc. For padding-inline-start, initial would give 0, so the indent would stay gone. revert restores the browser's own default for ul, which is the indent you want.
display shows the difference most clearly:
| Declaration on a `<div>` | Result |
|---|---|
| `display: initial` | `inline` |
| `display: unset` | `inline` |
| `display: revert` | `block` |
Browsers differ slightly in their default stylesheets, so revert can produce slightly different results between browsers. initial cannot.
revert-layer
revert-layer is the layer-aware version of revert. It rolls back only to the previous cascade layer in the same origin, not all the way to the user-agent origin. The value becomes whatever would apply if the current layer's declarations for that property were not there. If no earlier layer sets it, the cascade continues down to lower origins as usual.
@layer base, theme;
@layer base {
a { color: navy; }
}
@layer theme {
a { color: crimson; }
a.plain { color: revert-layer; }
}Links are crimson because theme is the later layer. For a.plain, the winning declaration in theme is revert-layer. That removes the theme layer's contribution for color, so the value falls back to base and the link is navy.
Use revert-layer when a component needs an escape hatch from one layer's styling but should keep the layers beneath it. Use revert when you want to drop everything you authored.
The all property
all is a shorthand that resets nearly every property at once. It accepts only the CSS-wide keywords: initial, inherit, unset, revert and revert-layer. It does not accept ordinary values like all: red.
Two exclusions are worth knowing:
alldoes not touchdirectionorunicode-bidi, because text direction should not be reset by accident.alldoes not touch custom properties (--gapand so on).
.isolated {
all: initial;
}
.clean-button {
all: unset;
cursor: pointer;
padding: 0.5rem 1rem;
background: steelblue;
color: white;
}Because all is a shorthand, source order inside a rule matters. Longhands written after all survive. Longhands written before it are wiped out.
.a { color: red; all: unset; } /* color is reset */
.b { all: unset; color: red; } /* color is red */Here is a complete page that shows the difference between the keywords. Open it in a browser and compare the four boxes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS-wide keywords</title>
<style>
section { color: darkgreen; font-size: 20px; }
div { border: 2px solid gray; margin: 8px; padding: 4px; }
.i { color: inherit; border: inherit; }
.in { color: initial; display: initial; }
.u { color: unset; border: unset; }
.r { display: revert; all: revert; }
</style>
</head>
<body>
<section style="border: 3px dashed red;">
<div class="i">inherit: green text, dashed red border from parent</div>
<div class="in">initial: black text, display inline</div>
<div class="u">unset: green text, no border</div>
<div class="r">revert: a normal block with default styles</div>
</section>
</body>
</html>In .i, both properties come from the section, including the border, which is not normally inherited. In .in, the text is back to the initial color and the box becomes inline. In .u, the inherited color flows down while the non-inherited border goes back to none. In .r, all: revert removes the author rules above (the border and margin from the div rule are author rules) and leaves the browser's default div.
Common errors
Using all: unset on a button
all: unset is popular for stripping a button, but it removes more than appearance. It resets outline, so the keyboard focus ring disappears. It also resets display to inline, which changes layout. If you use it, add a visible focus style back:
.clean-button:focus-visible {
outline: 2px solid currentColor;
outline-offset: 2px;
}Expecting initial to mean the browser default
initial is the spec default, not the browser default. A heading set to font-weight: initial becomes normal, not bold. If you want the browser's styling, use revert.
Putting all after the declarations you want to keep
all resets earlier longhands in the same rule and in lower-priority rules. If a keyword on all seems to erase your styles, check whether the all declaration comes later in the rule, or whether it sits in a rule that wins the cascade over rules containing the values you wanted.
Forgetting that keywords still compete
A keyword only applies if its declaration wins. A .card { color: inherit; } rule loses to #hero .card { color: red; } because of specificity, even though inherit looks like an override.
Assuming all resets custom properties
Because all ignores custom properties, all: initial does not clear a --gap set on the element. Reset a custom property by naming it. --gap: initial sets it to the guaranteed-invalid value, so var(--gap, 1rem) then uses its fallback.
Exercise
A site reset sets h2 { font-size: 1rem; font-weight: normal; margin: 0; }. Inside .article, you want h2 elements to look like the browser's default h2 again. Which declaration is best, and why not initial?
Show answer
Use .article h2 { all: revert; }, or font-size: revert; font-weight: revert; margin: revert; if you only want those three. revert discards the author rules and uses the user-agent stylesheet, which gives a large, bold h2 with vertical margins. initial would give font-size: medium, font-weight: normal and margin: 0, because those are the spec's initial values, so the heading would not look like a default h2.
Exercise
What is the computed color and border-style of the <span> in this markup, and why?
<div style="color: teal; border: 2px solid black;"><span style="color: unset; border: unset;">Hi</span></div>
Show answer
The color is teal and the border-style is none. color is an inherited property, so unset acts like inherit and takes teal from the div. border is not inherited, so unset acts like initial, giving border-style: none and no visible border.
Exercise
Given @layer base, theme; with base { p { color: navy; } } and theme { p { color: red; } p.quiet { color: revert-layer; } }, what color is <p class="quiet"> and what would it be with color: revert instead?
Show answer
With revert-layer it is navy, because the cascade rolls back only the theme layer and finds base's navy. With revert it uses the user-agent value, which for color is the inherited or initial value (usually black), because revert discards all author styles including the base layer.