Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Custom Properties, Cascade and Global Keywords

CSS inheritance

How CSS inheritance works: which properties inherit by default, how to force or stop it with inherit, initial and unset, and common pitfalls, with a live demo.

Some CSS properties pass their value down from a parent element to its children automatically. That is inheritance. It is why setting font-family on <body> styles the whole page, while setting border on a <div> does not draw borders around every element inside it.

Every property is defined as either inherited or not inherited. The reference pages for each property state this under "Inherited".

Try it

Inherited or not?

How it works

For each property on each element the browser works out the computed value in this order: a value from the cascade if any declaration matches; otherwise the parent's computed value if the property is inherited; otherwise the property's initial value.

Important consequences:

  • Inherited values are passed as computed values. If a parent has font-size: 2em, children inherit the resulting pixel size, not the 2em itself.
  • Any matching declaration beats inheritance, even from a very weak rule such as * { color: black }. A universal selector therefore breaks inheritance for everything it matches.
  • Browser default stylesheets are declarations too: links are blue because the browser sets a { color: ... }, not because inheritance failed.

Properties that inherit

Typically inherited (text and list related): color, font and its longhands, line-height, letter-spacing, word-spacing, text-align, text-indent, text-transform, white-space, direction, visibility, cursor, list-style, quotes, caret-color, color-scheme, pointer-events, border-collapse, border-spacing, and all custom properties (--*).

Typically not inherited (box and layout related): display, width, height, margin, padding, border, background, position and offsets, overflow, opacity, z-index, transform, box-shadow, flex and grid properties, text-decoration, vertical-align.

Always check the property's own reference page when it matters.

Controlling inheritance

Keyword Effect
inherit Take the parent's computed value, for any property.
initial Use the property's initial value.
unset inherit for inherited properties, initial for the rest.
revert Go back to the browser or user style.

Usage notes

  • Set shared text styles once on body or :root and let them cascade down.
  • Form controls have their own browser fonts; write font: inherit on input, button, select and textarea to match the page.
  • text-decoration is not inherited, but it still paints through inline children, which surprises people: you cannot remove a parent's underline from a child by setting text-decoration: none on the child.
  • Custom properties inherit, which makes them a good way to theme a subtree.
  • Inheritance follows the DOM tree, not the visual layout. Absolutely positioned or floated children still inherit from their parent element.