Inheritance and Computed Values
Why some properties flow from parent to child and others do not, the keywords that control it, and how a written value becomes the one the browser uses.
Inheritance and Computed Values
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Some properties inherit by default (`color`, `font-*`, `line-height`, `text-align`, `visibility`, `cursor`). Most box and layout properties do not (`margin`, `padding`, `border`, `background`, `width`).
- If the cascade finds no value for a property on an element, it uses the inherited value for inherited properties and the property's initial value for the rest.
- `inherit`, `initial`, `unset`, `revert` and `revert-layer` force that choice. `unset` means `inherit` for inherited properties and `initial` otherwise.
- The browser computes a value in steps: specified, computed, used, actual. Relative units such as `em` are resolved to absolute values at the computed step.
- A unitless `line-height: 1.5` passes the multiplier down to children. A percentage or length passes down one already-resolved pixel value.
The element nobody styled
Take a <span> inside a <p> inside a <div class="article">. You set color and font-family on the div, and say nothing about the p or the span. They still come out in that color and typeface. If CSS made you restate text properties on every element, no one would write it.
So the cascade has a second job beyond picking winners. When no rule gives an element a value for a property, it still needs one. There are two places to look, and which one it uses depends on the property.
Two kinds of property
Some properties are inherited: if you don’t set one, the element uses its parent’s computed value. That group is mostly about text: color, font-family, font-size, font-weight, line-height, letter-spacing, text-align, visibility, cursor, list-style.
The rest are non-inherited: with no value set, the element gets the property’s initial value, the default the spec defines. margin, padding, border, background, width, display and position are all here. This is the sensible split. If a border was inherited, one boxed container would box everything inside it.
Custom properties (--brand) inherit by default, which is exactly what makes them useful for theming, as covered in custom properties.
Forcing the choice with keywords
Any property accepts these as a value:
inherit: take the parent’s computed value, even for a non-inherited property.initial: take the property’s spec-defined initial value.unset: behave asinheritif the property inherits,initialif it doesn’t.revert: roll back to what the browser’s default stylesheet would give (or the user’s), discarding author styles.revert-layer: roll back to the value from a previous cascade layer.
A practical one is border-color: inherit, or color: inherit on a link, so that it takes whatever text color surrounds it. all: unset applies unset to every property at once. It is heavy-handed: on a <button> it deletes the default look and the default focus outline, and you have to rebuild both. Prefer resetting the specific properties you want changed.
revert is easy to misread as initial. They differ for display: a <div> has the user-agent rule display: block, so display: revert gives block, while display: initial gives inline, the property’s initial value. I checked the revert case in Chromium and it returned block.
From written value to used value
What you write is not what the browser draws with. A value moves through stages:
- Specified: what the cascade chose (or inheritance/initial supplied).
- Computed: relative values are resolved as far as they can be without layout.
em,remand keywords become lengths. Percentages usually wait. - Used: layout finishes the job: a
width: 50%becomes pixels once the container’s size is known. - Actual: rounding to what the device can paint.
getComputedStyle(el) is named for the second stage but reports resolved values, usually pixels. I checked a child with font-size: 1.5em inside a 24px parent: it reports 36px, so em was resolved against the parent for font-size itself, and against the element’s own font size for everything else like padding: 1em.
Why line-height is the classic trap
What gets inherited is the computed value, and that can differ depending on how you wrote it. Compare:
.a { font-size: 20px; line-height: 150%; } /* child inherits 30px */
.b { font-size: 20px; line-height: 1.5; } /* child inherits the number 1.5 */
.a span, .b span { font-size: 40px; }
In .a, the percentage is resolved to 30px at the parent and handed down as a length. The 40px span ends up with 30px line height, so lines overlap. In .b, the unitless number is handed down as a multiplier, so the span gets 40 × 1.5 = 60px. I measured exactly those values in Chromium. This is why the advice is always to write line-height unitless.
Using inheritance on purpose
The idea behind the runner-up video, going by its title, is setting inherited properties once, high in the tree, and not repeating them. A body { font-family: ...; line-height: 1.5; color: ... } covers the whole page. Then form-control elements, which browsers do not make inherit fonts by default, get one line: button, input, select, textarea { font: inherit; }.
What the video covers
Kevin Powell’s “HTML & CSS for Absolute Beginners: Inheritence” (the spelling is his) is a clip from his free beginner course, under seven minutes. From its title it covers how inheritance works at a beginner level. I could not read a transcript, so the keyword table, the staged value pipeline and the line-height experiment come from this lesson and not the video.
Where this fits
Inheritance is what makes a stylesheet short, and the computed-value pipeline is why em and rem behave the way they do in values, units and math functions. Next, cascade layers and scope adds new rungs to the ladder from the previous lesson. For a first pass at the cascade, see the frontend roadmap’s box model and cascade.
Check yourself
You set `border: 1px solid red` on a `<div>`. Do the paragraphs inside get a red border? Why or why not?
No. `border` is not an inherited property. Inheritance is the exception, applied to properties like `color` and `font-family` where passing the value down is almost always what you want.
A parent has `font-size: 20px; line-height: 1.5` and a child inside it has `font-size: 40px`. Compare that with the parent having `line-height: 150%` instead. What line-height does the child get in each case?
With `1.5` the child inherits the number and multiplies by its own font size: 60px. With `150%` the percentage is resolved against the parent's 20px first, so the child inherits 30px, which will look cramped at 40px text. I measured 60px and 30px in Chromium.
What does `all: unset` do to a `<button>`, and why might that be a bad default?
It sets every property to `unset`, so inherited ones inherit and the others return to their initial values, discarding even the browser's default button styling. That strips useful behavior such as the focus outline, so you have to rebuild what you removed.
Why does `getComputedStyle(el).fontSize` return `36px` for `font-size: 1.5em` on a child of a 24px element?
`em` is resolved at the computed-value step. For `font-size`, `1.5em` is relative to the parent's font size, so 1.5 × 24 = 36px, and that absolute value is what the API reports.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.