← Back to HTML & CSS, page 1

html-css-0017 · Medium mid

What is the CSS cascade, and how does inheritance fit into it?

The cascade picks one value per property by comparing origin and importance, layers, specificity and source order. Inheritance only applies when the cascade gives no value: inherited properties such as color and font-family pass to children, while box properties such as margin and background do not. inherit, initial, unset and revert control it explicitly.

Full answer

The cascade is the set of rules the browser uses to choose one value for each property on each element when several declarations compete. In order, it looks at:

  1. Origin and importance: browser defaults, user styles and author styles, with !important reversing the order.
  2. Cascade layers (@layer), where a later layer wins for normal declarations.
  3. Specificity of the selector.
  4. Order of appearance, where the last declaration wins.

Inheritance is separate and only applies when the cascade produced no value at all. Some properties are inherited by default, so a child reads its parent's value: color, font-family, font-size, line-height, visibility, cursor, text-align. Most box properties are not inherited: margin, padding, border, background, width, display.

You can force the behaviour with keywords. inherit takes the parent's value. initial uses the property's default. unset acts like inherit for inherited properties and initial for the rest. revert goes back to the browser's default for that element.

The first paragraph is green but has no border, because colour inherits and border does not. The third paragraph gets a border because inherit was asked for. Buttons do not inherit the page font by default, which is why font-family: inherit on form controls is common.

html
<style>
  body { color: darkgreen; border: 3px solid black; font-family: Georgia, serif; }
  p { margin: 0; }
  p.reset { color: initial; }
  p.copy { border: inherit; }
  button { font-family: inherit; }
</style>
<p id="a">Inherits the green colour, but no border.</p>
<p id="b" class="reset">Back to default black.</p>
<p id="c" class="copy">Border copied from body on purpose.</p>
<button id="btn">Uses the page font</button>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Cascade order: origin and importance, layers, specificity, order of appearance.
  • Inheritance fills in only when no declaration won for that element.
  • color, font-family, line-height and visibility inherit; margin, border and background do not.
  • inherit, initial, unset and revert are the explicit controls.
  • Form controls do not inherit font by default.

Common mistakes

  • expecting background or margin to flow down to children. They do not inherit by default.
  • assuming the later rule always wins. Specificity and layers are compared before source order.

Follow-ups

  • what is the difference between initial and unset?
  • What is the difference between initial and revert?
  • Why does a button not use the page font by default?

Tips

  • Think in two steps when debugging: which declaration won the cascade, then if none exists, does this property inherit?
  • Debug in two steps aloud: which declaration won, then does the property inherit?
  • Mention layers when listing the cascade order, since many answers leave them out.