Inheritance is the rule that some property values flow from a parent element to its children automatically.
It is why setting a font on the body changes the font of everything inside it, and it is why not setting a colour anywhere on a page does not leave the text colour at an unspecified value.
The previous lesson in this module covered "How Do the Cascade and Specificity Decide Who Wins?", and the next lesson in this module is about "Which Units Belong Where: px, rem, em and %?".
Which of the properties are inherited?
Many text-related properties are, and most box properties are not.
The list is worth learning by heart for the common ones, because it explains most of the surprises.
inherited not inherited
--------------------------- ---------------------------
color margin
font-family padding
font-size border
font-weight background
font-style width, height
line-height display
letter-spacing position
text-align z-index
text-indent overflow
text-transform flex properties
visibility box-sizing
cursor opacity (see note)The pattern is clear. Properties that describe the text are inherited, and properties that describe the boxes are not.
That is not an accident of the implementation. The padding of a child should not be set by the padding of its parent, but a child should look like it is in the same document as its parent.
How do you set a sensible default?
Because inherited properties cascade from the parent, the usual pattern is to set the text defaults on the root element, and then to let everything else follow.
:root {
color: #1c1c1c;
font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
font-size: 100%;
line-height: 1.5;
}
body {
background: #fff;
}Setting these on the :root and not on the body matters for a subtle reason. The html is the ancestor of everything, including the elements that are rendered outside the body, such as the contents of a popup or the scrollbars.
The :root selector matches the root element, and that is the html.
What do the inherit, the initial, the unset and the revert mean?
These four keywords let you change how a value is inherited, and each one means something slightly different.
.child {
color: inherit; /* take the parent's computed value, whatever it is */
color: initial; /* use the property's default value from the specification */
color: unset; /* inherit for inherited properties, initial for the rest */
color: revert; /* use whatever the user agent or a lower-priority origin says */
}The inherit is the one you will use most, and it is the answer to "I want this element to match its parent exactly, even for the properties that are not inherited by the default".
A common use is a link inside a heading. Links have their own colour by default, and the color: inherit makes the link take the heading's colour instead.
The unset is the clever one. It means "inherit if this property is inherited, and otherwise reset it to the initial value", and that is almost always what you want when you are clearing a value that you did not set yourself.
How do you stop a value being inherited?
You set the property explicitly on the child.
An explicit value always beats an inherited one, whatever the specificity of the parent's rule.
.card {
color: #1c1c1c;
font-size: 1rem;
}
.card h3 {
color: #800d4d; /* explicit: does not inherit the card's colour */
font-size: 1.25rem;
}There are two properties that need care.
The visibility is inherited, and a child that sets the visibility: visible can become visible again inside a parent that is hidden. That detail surprises people when they try to hide a wrapper.
And the opacity is not inherited. Setting it on a parent makes the whole group translucent, because the opacity applies to the rendered result of the group and not to each child.
Why does inheritance matter in practice?
Because it determines how much of the CSS you write.
A stylesheet that sets the text properties once at the root, and then lets them flow down, is far shorter and far more consistent than one that sets the font on every selector.
It also gives you a predictable place to change things. If the base font size is set on the root, then one line changes it everywhere.
/* the whole site's typography in one place */
:root {
--font-body: system-ui, sans-serif;
--font-heading: Georgia, "Times New Roman", serif;
--text-size: 1rem;
--text-muted: #5c5c5c;
}
body { font-family: var(--font-body); font-size: var(--text-size); }
h1, h2, h3 { font-family: var(--font-heading); }The custom properties in that example are themselves inherited, and that is why a variable that is defined on the root is available everywhere. It is also why redefining a variable on a subtree changes only that subtree.
That behaviour is the foundation of theming, and it works precisely because inheritance is doing what it always does.
Which properties inherit by default?
Inheritance is not all-or-nothing.
Most properties are set on every element and never travel down the tree, but a specific list of them does pass from a parent to its children, and knowing that list explains a lot of surprising behaviour.
The inheritable ones are mostly the text properties. There is the color, the font-family, the font-size, the font-style, the font-weight, the line-height, the letter-spacing, the text-align, the text-indent, the text-transform, the visibility, the white-space, the word-spacing, the list-style-type and the list-style-position, plus the cursor and the direction.
<div class="panel">
<!-- inherits color and font-size from .panel -->
<p>This text takes the panel's colour.</p>
<!-- does not inherit: padding and border are never inherited -->
<span class="badge">New</span>
</div>.panel {
color: #33475b;
font-size: 1.05rem;
}
.badge {
/* padding and border are not inherited, so they must be set here */
padding: 0.2rem 0.5rem;
border: 1px solid #b9c6d2;
border-radius: 999px;
}That is why a single color on the body styles most of a page, while a single padding on the body styles nothing at all.
You can override the behaviour for any property with the four global keywords.
The inherit forces the parent's value on a child, and that is useful for opting a specific element back into inheritance.
The initial sets the property to its default value.
The unset means inherit if the property inherits and use the initial value otherwise, and that is what you usually want when you are resetting a property that you did not create.
And the revert rolls the declaration back to whatever the previous cascade level said.
Quick reference
- Text properties inherit, and box properties do not.
- You set inherited defaults on the
:rootso that they reach every element. - The inherit forces the parent's value, the unset means inherit-or-reset, the initial resets, and the revert defers to a lower origin.
- An explicit value on a child always beats an inherited one.
- The visibility is inherited and it can be undone by a child, and the opacity is not inherited at all.
- Custom properties are inherited, and that is what makes theming possible.
Try it yourself: set a font and a colour on the root element, and watch both of them flow into a deeply nested list. Then add a padding rule on a parent, and confirm that the child does not inherit it. Finally, put the color: inherit on a link inside a coloured heading, and see the link match the heading exactly.