Tuesday, November 26, 2019

Understanding CSS Inheritance (Inherit, Initial, Unset, and Revert)

Understanding CSS Inheritance (Inherit, Initial, Unset, and Revert)

Relationships exist in HTML, and these relationships make it possible for us to select and style the elements of a web page. When an HTML element is nested within another, the outer element is known as the parent, while the inner element is the child.

A child element can itself become parent to another element. 

inheritance is important
Inheritance, cleverly demonstrated using human beings

Why all this preamble and backstory? Because it’s important! 

When you style a parent element, in some cases the style will be inherited by its children elements. I say some cases because this kind of inheritance doesn’t always hold true.

The Importance of CSS Inheritance

If you have ever styled the contents of a web page, it’s quite possible that you didn’t write a font style for every element that had to display text. It is possible that you only added your font styles to the body element, for example:

If you want a uniform font style for all this content, you only have to style the body element:

This is possible because of inheritance across HTML elements. And it’s helpful, as we don’t have to repeat the same font style for the divs and headings. The same applies to color styles, which when applied to a parent element will be applied to the children of that parent unless a different color style gets applied to the children elements.

While inheritance makes things easy for us, things wouldn’t be so easy if all CSS properties behaved in this way.

Here is another example:

Who knew cucumbers were berries?! We’ll implement a few styles to the parent list and see what gets inherited down the bloodline:

The font and color styles that were applied to the unordered list are inherited by its children elements and even its grandchildren elements. However, this is not the case with the border styles. 

So which other properties are inherited?

A Complete List of Inherited Properties

According to the W3C, here are the properties that can be inherited.

  1. azimuth
  2. border-collapse
  3. border-spacing
  4. caption-side
  5. color
  6. cursor
  7. direction
  8. elevation
  9. empty-cells
  10. font-family
  11. font-size
  12. font-style
  13. font-variant
  14. font-weight
  15. font
  16. letter-spacing
  17. line-height
  18. list-style-image
  19. list-style-position
  20. list-style-type
  21. list-style
  22. orphans
  23. pitch-range
  24. pitch
  25. quotes
  26. richness
  27. speak-header
  28. speak-numeral
  29. speak-punctuation
  30. speak
  31. speech-rate
  32. stress
  33. text-align
  34. text-indent
  35. text-transform
  36. visibility
  37. voice-family
  38. volume
  39. white-space
  40. widows
  41. word-spacing

You can find more about this list on the W3C’s website (you definitely do not need to memorize all of these!).

How to Force Properties to Inherit

Since some properties cannot be inherited, you might think that the way out is to apply them to the children elements as well. The styles we used above might look like this:

We’d still only have border styles on the parent list and the first sub-list. But the issue is that we’ve had to repeat ourselves. The pain of copying the same style over and over becomes evident.

A good solution would be one where you only need to apply the style once, preferably on the parent, and do a little tweak on the child to inherit. This will keep everything dry and clean.

The Inherit Keyword

According to the MDN Docs:

“The inherit CSS keyword causes the element for which it is specified to take the computed property of the property from its parent element.” – MDN

In other words, it’s a way of stating that the value of a particular property should be obtained from the element’s parent. This keyword can be used on any CSS property.

Going back to our example, here is how the style would appear:

With that, the result will look like this:

So, should our border styles ever need to change, we’d only need to change them in one place.

How to Force Properties Not to Inherit

While it’s possible to enforce inheritance on properties that are not inheritable by default, in some cases it might make sense not to do so. An alternative is to make use of the property’s initial values.

The Initial Keyword

You can set the default or initial value of a CSS property by using the initial CSS keyword. This will cause the inherited value of the property to go back to its initial value.

In this example there are a couple of things happening. We have two div elements whose red color properties are inherited by the h1 and p elements nested within. However, we also apply a global h1 style (blue color) but we make sure the second h1 doesn’t inherit either style in the following way:

Our h1 in the .berries block goes back to whatever color the browser originally applied. Here’s what that looks like:


Other CSS Inheritance Keywords

In addition to the inherit and initial keywords, we can also use revert and unset. In fact, these alternatives are actually recommended because initial can produce some unexpected results.

The Unset Keyword

The unset keyword is subtly different. It resets an element’s value to the inherited value (if it inherited) and to its initial value if not. Here’s our example again:

In this case, the color property of our second h1 reverts to its inherited value (red) instead of its initial value (black):

The Revert Keyword

Lastly, we have the revert keyword, which works similarly to unset in most cases. It resets the property to its inherited value (if it inherits from its parent), or to the default value established by the user’s own stylesheet (if it exists), or then the browser’s styles.

Conclusion

When a style rule is declared in a stylesheet there are lots of places where the value of the property could come from;

  1. The stylesheet defined by the web author.
  2. User-defined styles.
  3. Browser defined styles.

Where the styles are fetched from depends on how the property works in regards to inheritance. If the property is inheritable, then its value will come from its parent which will be declared in the stylesheet created by the web author. Else it will come from either the second, or third source.

Inheritance in CSS can be a little confusing! I hope this tutorial helped you make sense of it.

Learn More About CSS Selectors


No comments:

Post a Comment