Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 139 ยท The Cascade

Inheritance

Learn which CSS properties pass from parent to child, why form controls ignore your fonts, and how inherit, initial, unset and revert control inheritance.

Set color on the <body> and every paragraph, list item and heading on the page changes color too, even though you never wrote a rule for them. Set a border on the <body>, though, and only the body gets one. The difference is inheritance: some properties pass their value down from parent to child, and others don't.

Inheritance is what makes CSS practical. Without it you would have to set the font on every single element. In this lesson you will learn how inheritance works, which properties inherit, why some elements seem to ignore it, and how to switch it on or off for any property.

How inheritance works

For every property on every element, the browser needs a value. It gets one like this:

  1. If any declaration targets the element for that property, the cascade picks a winner and that value is used.
  2. If none does, and the property is inherited, the element takes its parent's value.
  3. If none does, and the property is not inherited, the element gets the property's initial value, the default defined in the CSS specification.

Inheritance flows down the tree one level at a time. A <strong> inside a <p> inside an <article> gets its color from the <p>, which may have gotten it from the <article>, which may have gotten it from <body>.

Color flows down the tree

The heading, the paragraphs, <strong> and <em> all turn purple and serif. Only the <article> has a border, because border and padding are not inherited. Imagine if they were: every paragraph, and every word inside it, would get its own box.

Which properties inherit?

The rule of thumb: properties about text inherit; properties about boxes don't.

Inherited properties include:

  • color
  • font and its parts: font-family, font-size, font-weight, font-style
  • line-height, letter-spacing, word-spacing
  • text-align, text-indent, text-transform, white-space
  • list-style and its parts
  • visibility, cursor
  • direction, quotes, border-collapse (and a few other table and text properties)
  • every custom property, such as --brand (see Custom Properties)

Properties that do not inherit include:

  • margin, padding, border, outline
  • width, height and their min/max versions
  • background and all its parts
  • display, position, float, overflow
  • Flexbox and Grid properties
  • opacity, transform, box-shadow, filter

Every property's definition says "Inherited: yes" or "no", so you never have to guess. DevTools also helps: in the Computed panel, an inherited value is shown along with the ancestor it came from.

Backgrounds look inherited, but they're not. A child with no background is transparent, so you see the parent's background through it. The initial value of background-color is transparent, not "the parent's color".

Inherited values are the computed value

A child does not inherit the text you wrote; it inherits the parent's computed value. For most properties, that difference is invisible. For relative units, it matters.

em is computed before it is inherited

letter-spacing: 0.1em becomes 2px on .box (0.1 ร— 20px), and the pixels are what get inherited. The small text does not recalculate 0.1 ร— 12px. This is why line-height is usually written without a unit, such as line-height: 1.5: a plain number is inherited as a number and multiplied by each element's own font size. Line Height and Vertical Rhythm goes into the details.

Why forms ignore your font

Here is a classic surprise. You set a nice font on the <body>, and every piece of text changes... except the text in buttons, text fields and drop-downs.

Form controls don't inherit fonts by default

The controls are not refusing to inherit. Inheritance only applies when no declaration sets the property, and the browser's own stylesheet sets font (and often color) directly on form controls. That user-agent declaration targets the control, so it beats inheritance. The fix is to set the property yourself with the keyword inherit, which says "use my parent's value" explicitly. Almost every modern reset includes this rule:

CSS
button,
input,
select,
textarea {
  font: inherit;
}

You will meet more of these in Resets and Base Styles.

Controlling inheritance with keywords

Every CSS property accepts a handful of special keywords that control inheritance directly. Here they are in brief; Global Values covers each in depth.

  • inherit takes the parent's value, even for properties that normally don't inherit.
  • initial uses the property's initial value from the specification, ignoring the parent.
  • unset acts like inherit for inherited properties and like initial for the rest. In other words, "pretend nothing set this property".
  • revert rolls back to the value the browser's own stylesheet (or the user's) would have given.
Forcing inheritance on a non-inherited property

color: initial gives the specification's initial color, which is the default text color (usually black on a light page), not whatever the page might set on <body>. That is a common trap: initial does not mean "the browser default for this element", and it does not mean "the color my site normally uses". If that is what you want, revert or inherit is usually the better choice.

Using inheritance on purpose

Good stylesheets lean on inheritance rather than fighting it:

  • Set text styles high up. Put the main font-family, color and line-height on body (or :root) once. Everything inherits them.
  • Override where a component differs. A dark card sets color: white, and everything inside follows without extra rules.
  • Use currentColor. This keyword means "the current value of color". Borders, icons and shadows written with currentColor follow the inherited text color automatically. See Colors.
  • Make icons follow text. An inline SVG with fill="currentColor" changes color with the link or button it is in.
One color change, many effects

Each variant sets just one property, color. The text, the border and the SVG dot all follow, because the dot's fill and the border use currentColor, and the text inherits.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which of these properties is inherited by default?

    Text-related properties such as font-family inherit. Box-related ones like padding, border and backgrounds do not.
  2. 2. You set font-family on body, but your <button> still uses a different font. Why?

    Inheritance only applies when nothing sets the property. The user-agent stylesheet does, so add font: inherit to form controls.
  3. 3. A parent has font-size: 20px; letter-spacing: 0.1em. A child has font-size: 10px and no letter-spacing rule. What is the child's letter-spacing?

    The parent's computed value, 2px, is what is inherited. It is not recalculated against the child's font size.
  4. 4. What does unset do on a property that is not inherited, like margin?

    unset behaves like inherit for inherited properties and like initial for everything else.

Summary

  • When no declaration targets an element for a property, inherited properties take the parent's computed value; others get their initial value.
  • Text properties (color, font, line-height, spacing, alignment) inherit; box properties (margin, padding, border, background, size, layout) don't.
  • Any declaration that targets the element beats an inherited value.
  • Form controls get fonts from the browser stylesheet; add font: inherit to fix them.
  • inherit, initial, unset and revert let you control inheritance per property.
  • Lean on inheritance: set text styles high in the tree and use currentColor.

Next: !important โ€” what it really does, and why it should be rare.