Contents of CSS

Modern CSS techniques, layouts, animations, and responsive design patterns for creating beautiful web interfaces.

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 (except unicode-bidi and direction).
  • Use Case: Reset or override all styles at once.
  • Example:
    * { all: unset; }

🧠 Summary Table:

KeywordMeaningInherits?Resets to spec?Reverts to browser/user style?
inheritInherit value from parentβœ… Yes❌ No❌ No
initialReset to CSS spec-defined default❌ Noβœ… Yes❌ No
unsetinherit if inherited, else initialβœ…/❌ Smartβœ…/❌ Smart❌ No
revertRevert to browser/user/author stylesheetDependsDependsβœ… Yes
allApplies one of the above to all propertiesDependsDependsDepends

πŸ“Œ 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.