inherit, initial, unset, revert

There are four keywords that are valid values for any CSS property (see the title). Of those, day to day, I’d say I see the inherit used the most. Perhaps because it’s been around the longest (I think?) but also …


The post inherit, initial, unset, revert appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.

There are four keywords that are valid values for any CSS property (see the title). Of those, day to day, I’d say I see the inherit used the most. Perhaps because it’s been around the longest (I think?) but also because it makes logical sense (“please inherit your value from the next parent up that sets it”). You might see that with an override of a link color, for example.

<footer>
  ©2012 Website — <a href="/contact">Contact</a>
</footer>
/* General site styles */
a {
  color: blue;
}

footer {
  color: white;
}
footer a {
  color: inherit;
}

That’s a decent and elegant way to handle the fact that you want the text and links in the footer to be the same color without having to set it twice.

The others behave differently though…

  • initial will reset the property back to the spec default.
  • unset is weird as heck. For a property that is inherited (e.g. color) it means inherit, and for a property that isn’t inherited (e.g. float) it means initial. That’s a brain twister for me such that I’ve never used it.
  • revert is similarly weird. Same deal for inherited properties, it means inherit. But for non-inherited properties it means to revert to the UA stylesheet. Kinnnnnda useful in that reverting display, for example, won’t make a <p> element display: inline; but it will remain a sensible display: block;.

PPK covered all this in more detail.

I’m glad he found my whining about all this:

Chris Coyier argues we need a new value which he calls default. It reverts to the browser style sheet in all cases, even for inherited properties. Thus it is a stronger version of revert. I agree. This keyword would be actually useful.

Amen. We have four properties for fiddling with the cascade on individual properties, but none that allow us to blast everything back to the UA stylesheet defaults. If we had that, we’d have a very powerful tool for starting fresh with styles on any given element. In one sense: scoped styles!

PPK has a fifth value he thinks would be useful: cascade. The idea (I suppose) is it kinda acts like currentColor except for any property. Sort of like a free variable you don’t have to define that gives you access to what the cascaded value would have been, except you’re going to use it in some other context (like a calculation).


The post inherit, initial, unset, revert appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


Print Share Comment Cite Upload Translate
APA
Chris Coyier | Sciencx (2024-03-28T10:16:56+00:00) » inherit, initial, unset, revert. Retrieved from https://www.scien.cx/2021/06/22/inherit-initial-unset-revert-2/.
MLA
" » inherit, initial, unset, revert." Chris Coyier | Sciencx - Tuesday June 22, 2021, https://www.scien.cx/2021/06/22/inherit-initial-unset-revert-2/
HARVARD
Chris Coyier | Sciencx Tuesday June 22, 2021 » inherit, initial, unset, revert., viewed 2024-03-28T10:16:56+00:00,<https://www.scien.cx/2021/06/22/inherit-initial-unset-revert-2/>
VANCOUVER
Chris Coyier | Sciencx - » inherit, initial, unset, revert. [Internet]. [Accessed 2024-03-28T10:16:56+00:00]. Available from: https://www.scien.cx/2021/06/22/inherit-initial-unset-revert-2/
CHICAGO
" » inherit, initial, unset, revert." Chris Coyier | Sciencx - Accessed 2024-03-28T10:16:56+00:00. https://www.scien.cx/2021/06/22/inherit-initial-unset-revert-2/
IEEE
" » inherit, initial, unset, revert." Chris Coyier | Sciencx [Online]. Available: https://www.scien.cx/2021/06/22/inherit-initial-unset-revert-2/. [Accessed: 2024-03-28T10:16:56+00:00]
rf:citation
» inherit, initial, unset, revert | Chris Coyier | Sciencx | https://www.scien.cx/2021/06/22/inherit-initial-unset-revert-2/ | 2024-03-28T10:16:56+00:00
https://github.com/addpipe/simple-recorderjs-demo