日本語

CSS inherit, initial, unset, and revert: What Each Keyword Resets To

​​CSS​Accessibility,HTML,Playwright

The CSS keywords inherit, initial, unset, and revert all cancel a declaration, but each one resets the value to a different place. inherit uses the parent's value. initial uses the initial value defined in the specification. unset uses the parent's value for inherited properties and the initial value for the rest. revert uses the browser's default style. This article shows which one to choose when you want to reset the look of a button or link. In the second half, I remove a button's appearance with all: unset and check what you lose when building your own button.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened HTML and CSS pages with Playwright and read the computed values with getComputedStyle(). I did not test Firefox, Safari, real smartphones, or browsers with a user stylesheet.

Where each of the four keywords returns to

The CSS specification (CSS Cascade 5) defines the four keywords as follows.

Keyword Returns to
inherit The computed value of the parent element
initial The initial value defined in the specification for each property
unset Same as inherit for inherited properties, same as initial for non-inherited properties
revert The value without the site's CSS (usually the browser's default style)

A common mistake is to think of initial as the browser's default value. MDN's page on revert notes that the initial value of display is inline, and that a div becomes block because of the browser's stylesheet (the user agent stylesheet). If you want to return to the browser's default, use revert, not initial.

Comparing computed values

I set the parent element to color: rgb(216, 88, 120), font-size: 20px, and border: 2px solid rgb(0, 200, 232). For the child element, I first applied a different value in the site's CSS. Then I canceled the same property with each of the four keywords and read the computed value.

css
.parent { color: rgb(216, 88, 120); font-size: 20px; font-weight: 400; border: 2px solid rgb(0, 200, 232); }
.c-a { color: rgb(0, 0, 255); }          /* value before the reset */
.c-a.t { color: unset; }                  /* replace this with each of the four keywords to compare */
Element and property Before reset inherit initial unset revert
color on span Blue Parent's pink Black Parent's pink Parent's pink
color on a Blue Parent's pink Black Parent's pink Link blue rgb(0, 0, 238)
font-weight on h2 300 400 400 400 700
font-size on h2 14px 20px 16px 20px 30px
display on div flex block inline inline block
margin-top on p 0px 0px 0px 0px 20px
border on span 1px dashed Same 2px solid line as the parent None None None

The table shows the following.

unset and revert gave the same values when the browser has no default style for that element and property (color and border on span). They differed where the browser does have a default style. With revert, the a returned to link blue, the h2 became bold (700) and 30px (1.5 times the parent's 20px), the div became block, and the p got its 20px top margin back. unset ignores those default styles and uses the parent's value or the initial value.

initial does not use the parent's value even for inherited properties. The color became black even though the parent was pink, and the font-size became 16px even though the parent was 20px. As the example on MDN's page on all shows, the initial value of font-size is medium. MDN's page on initial also notes that the initial value of an inherited property can produce unexpected results, and suggests considering inherit, unset, or revert instead.

inherit also works on properties that are not normally inherited. With border: inherit, the span got the same 2px solid light-blue line as its parent.

Point

When in doubt: use revert if you want the browser's default. Use unset if you want to remove the site's declaration, so that inherited properties take the parent's value and everything else takes the initial value. Use initial only when you specifically want the initial value.

Strictly speaking, revert returns to "the value without the site's CSS." The specification's section on revert says that it goes to the user's own styles (the user stylesheet) if they exist, and otherwise to the browser's default style. revert-layer, which reverts to the layer just below the current one in a cascade layer, is covered in the separate article "How to organize style priority with CSS @layer."

Removing a button's appearance with all: unset

The all property sets a single keyword on almost all properties at once. The specification's section on all says that the only exceptions are direction, unicode-bidi, and custom properties (variables that start with --).

When you add all: unset to a <button>, the background, border, and padding that the browser applies are removed, and only the text remains. This is useful for an icon-only button, or for a button that should look like a text link.

Four-row comparison. A button with no rules and a button with appearance: none are both small gray buttons with a background and border. The button with all: unset has no border or background, and only the label "Save" (the Japanese label in the image means "Save") at the same size as the surrounding text. The button with all: unset plus your own rules is a light-blue button with rounded corners. The labels in the image are in Japanese.
Button appearance for each set of rules (Chromium in the test environment)

I compared the computed values of the button on a page where the body text was font: 18px/1.5 serif.

Property No rules all: unset
display inline-block inline
font-family Arial serif (inherited from parent)
font-size 13.3333px 18px (inherited from parent)
background-color rgb(239, 239, 239) transparent
padding 1px 6px 0px
border 2px outset none
cursor default auto
box-sizing border-box content-box
text-align center start

Because the font size and typeface are inherited from the parent, the button text matches the surrounding text. The problem of a button's text being smaller than the body text goes away.

I also tried appearance: none for comparison. The computed values for the properties in the table above were the same as with no rules, and the background, border, and padding remained. In headless Chromium in the test environment, the look barely changed. If you want to build a button's appearance from scratch, appearance: none alone is not enough.

What to add after all: unset

Write your own declarations after all: unset

all is a normal property, so within the same rule, the declaration written later wins.

css
/* padding is removed */
.bad { padding: 12px; all: unset; }

/* padding is kept */
.good { all: unset; padding: 12px; }

In the test environment, the padding of .bad was 0px and that of .good was 12px.

Restore the focus ring

When I moved focus to a button with all: unset using the Tab key, :focus-visible matched, but outline-style was none and no ring appeared. A button with no rules gets outline: auto from the browser's default style. all: unset also removes this default focus ring.

Because keyboard users need to see where they are, restore the ring with :focus-visible.

css
.icon-button {
  all: unset;
  cursor: pointer;
  display: inline-flex;
  padding: 8px 16px;
  border-radius: 6px;
  background: #00c8e8;
}

.icon-button:focus-visible {
  outline: 3px solid #d85878;
  outline-offset: 2px;
}
Two-row comparison. In the top row, a button with only all: unset shows nothing around the label "Save" (the Japanese label in the image means "Save") after focus is moved with the Tab key. In the bottom row, the button with :focus-visible added shows a pink ring around the light-blue button. The labels in the image are in Japanese.
After moving focus with the Tab key

With this code, the outline for a button focused with the Tab key was a 3px solid line. The separate article "How to make focus rings easier to see for keyboard navigation in CSS" covers how to style focus rings in more detail.

Show that the button can be pressed

With all: unset, cursor changes from default, the value for a button with no rules, to auto. To show that the button can be pressed, add cursor: pointer as in the example above.

A disabled (disabled) button with no rules shows its text in light gray (rgba(16, 16, 16, 0.3)). With all: unset, however, the text kept its normal color. Because the look no longer signals that the button can't be pressed, change the appearance with :disabled.

css
.icon-button:disabled {
  cursor: not-allowed;
  opacity: 0.4;
}

In the test environment, a button with disabled matched this rule, and its cursor was not-allowed and its opacity was 0.4.

box-sizing also returns to its initial value

Even when *, *::before, *::after { box-sizing: border-box; } is applied to the whole page, a button with all: unset gets the initial value content-box for box-sizing. With width: 100px, 10px left and right padding, and a 1px border, a button with no rules was 100px wide, while the all: unset button was 122px wide. For a button whose width matters, write box-sizing: border-box again after all: unset.

Button behavior is not removed

all changes only CSS properties. A button with all: unset can still receive focus with the Tab key, and the Enter and Space keys fired click (two presses gave two clicks). Playwright's getByRole("button") also found it as a button. A button with disabled did not fire click when clicked, even with all: unset applied.

If you keep the element as a <button> and use all: unset only to remove the appearance, keyboard operation and treatment as a button remain intact.

all: revert returns to the browser's button

If you add all: revert to a button that has background and padding from the site's CSS, the button returns to the browser's default button. Its computed values match a button with no rules (inline-block, Arial at 13.3333px, background rgb(239, 239, 239), padding 1px 6px, and a 2px outset border).

css
.button { cursor: pointer; padding: 8px 16px; background: #00c8e8; }
.button.native { all: revert; } /* only this one uses the browser's default button */

all: unset removes everything down to a blank state, while all: revert goes back to the browser's default. Use unset when you are building a button from scratch, and revert when you want to remove the site's shared button styling from just one place.

Note that all does not remove custom properties. I added all: unset to the same rule that had --accent, and used color: var(--accent). The value of --accent remained, and the text took that color.

​Posted by 86