Sub topic 10
inherit, initial, unset, revert and the all property
This chapter assumes you know how the cascade picks a winner (origin, layers, specificity, source order) and how inheritance passes some properties from parent to child. Here you will learn the five CSS-wide keywords and the all shorthand. They let you decide where a property's value comes from, instead of writing a new value.
The CSS-wide keywords
Every CSS property accepts these keywords. Each one tells the browser where to look for the value.
| Keyword | Value used |
|---|---|
| `inherit` | The parent's computed value |
| `initial` | The property's initial value from the CSS specification |
| `unset` | `inherit` if the property is inherited, otherwise `initial` |
| `revert` | The value from the previous origin, usually the browser's default stylesheet |
| `revert-layer` | The value from the previous cascade layer in the same origin |
These keywords are declared values, so they take part in the cascade like any other declaration. Specificity and source order still decide whether the keyword wins. Only after it wins does it change where the value comes from.
inherit
inherit copies the parent's computed value. It works on any property, including ones that normally do not inherit. Form controls are the usual case, because browsers give them their own font and color.
button {
font: inherit;
color: inherit;
}After this rule, buttons use the same font and text color as the surrounding text.
initial
initial sets the value defined in the specification. It does not restore the browser's default styling. For example, the initial value of display is inline, so display: initial on a div makes it inline, even though the browser's stylesheet normally makes it block.
.badge {
display: initial;
color: initial;
}The initial value of color is not fixed by the specification. It depends on the browser and the user's settings, and it is usually black. Do not assume initial gives you a specific color.
unset
unset removes your declaration's effect in a way that depends on the property:
- For an inherited property such as
color, it acts likeinherit. - For a non-inherited property such as
margin, it acts likeinitial.
.card {
margin: unset;
}margin is not inherited, so unset resets it to its initial value of 0.
Correction: the diagram must be a diagram fence. Here it is in full.
revert
revert rolls the cascade back to the origin below the current one. In author styles, that means the browser's default stylesheet, or the user's stylesheet if there is one. If no earlier origin sets the property, revert behaves like unset.
This differs from initial. Suppose your site has a reset that removes heading margins, and one component wants the browser's normal heading spacing back:
h1, h2, h3 { margin: 0; }
.article h2 {
margin: revert;
}The h2 inside .article gets the browser's default margin for headings, which is about 0.83em in most browsers. With initial it would get 0.
revert-layer
revert-layer rolls back only to the previous cascade layer within the same origin. Declarations in the current layer for that property are ignored, and the value comes from an earlier layer.
@layer base, components;
@layer base {
a { color: navy; }
}
@layer components {
a { color: teal; }
a.plain { color: revert-layer; }
}A link with class plain ignores teal and uses navy from the base layer. If no earlier layer sets the property, the value comes from the next origin down, as with revert.
The all property
all is a shorthand that sets every property at once. It accepts only the CSS-wide keywords. The properties direction and unicode-bidi are excluded, and so are custom properties.
.widget {
all: unset;
}
.embed {
all: initial;
}all: unset strips the element of both browser styling and inherited-property overrides. A button with all: unset loses its border, padding, and background, and it also loses its focus outline styling, so you must provide your own focus style for keyboard users.
You can follow all with specific declarations. Source order and specificity still apply, so later declarations override the reset:
.widget {
all: unset;
display: block;
color: #222;
}Common errors
- Expecting
initialto restore the browser's look. Userevertfor that. - Using
unsetand expecting0or a default for an inherited property such ascolor. It inherits instead. - Using
alland forgetting that it removesdisplaystyling, so adivbecomes inline. - Setting
all: unseton interactive controls and losing the focus indicator.
Exercise
A page has p { color: red; } in the author stylesheet. A <p class="reset"> inside a <div style="color: green"> has the rule .reset { color: unset; }. What color is its text, and what would it be with color: initial instead?
Show answer
With unset, the text is green. color is an inherited property, so unset behaves like inherit and takes the parent's computed value. With initial, the text uses the specification's initial value for color, which is usually black in the browser. It is not green and not red.