What is the difference between inherit, initial, revert, unset, and all in CSS?
In CSS, inherit
, initial
, revert
, unset
, and all
are global property values that can be applied to any CSS property, and they control how the value of that property is computed. Let's break them down:
πΉ inherit
- Definition: The property value is explicitly inherited from the parent element.
- Use Case: Useful when the property is not normally inherited by default, but you want it to be.
- Example:
p { color: inherit; }
- If the parent has
color: red
, then the<p>
will also have red text.
πΉ initial
- Definition: Resets the property to its default value as defined by the CSS specification (not the browser's stylesheet).
- Use Case: When you want to override styles and reset them to the standard default.
- Example:
div { display: initial; /* Resets to 'inline' for most elements */ }
πΉ unset
- Definition: Acts like:
inherit
if the property is normally inherited (e.g.,color
,font-family
)initial
if it is not normally inherited (e.g.,display
,margin
)
- Use Case: A smart reset when you're not sure if the property is inherited by default.
- Example:
span { all: unset; }
πΉ revert
- Definition: Reverts the property to the default value from the user-agent (browser stylesheet), user stylesheets, or author stylesheets, depending on the cascade order.
- Use Case: Useful when you're overriding some framework or third-party styles and want to go back to the browser default.
- Example:
button { all: revert; /* Removes framework styles and uses browser styles */ }
- Note: Supported in modern browsers, but not in Internet Explorer.
πΉ all
- Definition: A shorthand that applies a global keyword (
inherit
,initial
,unset
,revert
) to all properties (exceptunicode-bidi
anddirection
). - Use Case: Reset or override all styles at once.
- Example:
* { all: unset; }
π§ Summary Table:
Keyword | Meaning | Inherits? | Resets to spec? | Reverts to browser/user style? |
---|---|---|---|---|
inherit | Inherit value from parent | β Yes | β No | β No |
initial | Reset to CSS spec-defined default | β No | β Yes | β No |
unset | inherit if inherited, else initial | β /β Smart | β /β Smart | β No |
revert | Revert to browser/user/author stylesheet | Depends | Depends | β Yes |
all | Applies one of the above to all properties | Depends | Depends | Depends |
π More questions:
Q: How is revert
different from initial
?
A: initial
sets the value to the CSS spec default, whereas revert
goes back to the value defined by the user-agent stylesheet or previously declared styles, following the normal CSS cascade.
Q: What does all: unset
do?
A: It removes all styles, including margin, padding, borders, and resets the element to a "bare" state. Itβs a handy way to eliminate default and framework styles.
Q: Why not always use unset
instead of initial
or inherit
?
A: While unset
is flexible, it may not be ideal when you want precise controlβlike explicitly inheriting or resetting a specific non-inherited property.
π‘ Real-world Example:
button { all: unset; background-color: blue; color: white; padding: 0.5rem 1rem; border-radius: 5px; }
This is common in design systems or resets where you remove all browser default styles from a button before styling it from scratch.
β Pro Tip:
Always test revert
for browser compatibility, especially if you are supporting legacy browsers like Internet Explorer.