Sub topic 9
Inheritance: which properties pass down and why
This chapter assumes you can write CSS rules with selectors and declarations, know how elements nest in HTML, and have used browser developer tools to inspect an element. It also assumes you know the cascade picks one winning declaration per property. Inheritance covers the case where no declaration targets an element's property at all.
What inheritance is
Inheritance is the mechanism by which an element takes the computed value of a property from its parent when no cascaded value exists for that property on the element itself. It fills gaps, and it never competes with a declaration that targets the element directly.
Suppose you set color on body and never mention color on a paragraph. No rule targets the paragraph's color, so the browser uses the parent's computed value. The paragraph's parent does the same with its own parent, up to the root.
Every element's text color therefore does not need its own rule. Setting a few properties near the top of the tree styles the entire page.
The order of the decision
For each property on each element, the browser asks these questions in order:
- Does the cascade produce a winning declaration for this element? If so, use it.
- If not, is the property an inherited property? If so, use the parent's computed value.
- Otherwise, use the property's initial value.
The cascade always runs first. A declaration that matches the element, even a low-specificity one such as * { color: black; }, beats inheritance. This is the source of many surprises, covered in the errors section below.
Which properties inherit
Each CSS property definition states whether it is inherited. The rule of thumb is that properties controlling text and typography inherit, and properties controlling the box and layout of a single element do not.
| Group | Inherited | Not inherited |
|---|---|---|
| Text color | `color` | `background-color` |
| Fonts | `font-family`, `font-size`, `font-weight`, `font-style`, `line-height` | |
| Text layout | `text-align`, `text-indent`, `text-transform`, `letter-spacing`, `white-space` | `text-decoration` (see below), `vertical-align` |
| Lists | `list-style` and its parts | |
| Box model | `margin`, `padding`, `border`, `width`, `height` | |
| Positioning and display | `visibility`, `cursor` | `display`, `position`, `float`, `top`, `left` |
| Visual effects | `opacity`, `overflow`, `box-shadow`, `background` |
The reasoning behind the split is practical. Text styling describes how a whole passage should look, so passing it down is convenient. A border or a margin describes one box. If borders inherited, every child of a bordered div would draw its own border, and the page would be unusable.
This table is a guide, not a specification. When you are unsure, check the property's page on MDN or the specification. Each page has an "Inherited" row in its formal definition.
Special cases worth knowing
text-decorationis not inherited, but it still appears on descendants. An underline on a parent is drawn across the text of its descendants as a single decoration, so you cannot remove it by settingtext-decoration: noneon a child. The parent's decoration is still painted over the child's content.opacityis not inherited either, but it applies to the element and everything inside it as a group. A child of a 50% opaque parent is displayed at 50% or less, even though the child's ownopacityis still1. Child values multiply with the parent's effect.visibilityis inherited. A child of avisibility: hiddenparent is hidden unless the child setsvisibility: visible. This differs fromdisplay: none, which removes the element and its subtree from rendering and cannot be undone by a child.cursoris inherited. Setting it on a container affects the children unless they override it.
A worked example
The example below sets inherited and non-inherited properties on body.
body {
color: #222;
font-family: system-ui, sans-serif;
border: 1px solid #ccc;
}
/* A <p> inside body inherits color and font-family from body,
but not border, because border is not an inherited property. */The paragraph is drawn in dark gray in the system font. It has no border of its own, because border's initial value is none. Only the body element has a border.
You can confirm this in developer tools. Select the paragraph and open the Computed panel. The color and font-family rows show the values from body, and the border-top-width row shows 0px.
Inheritance uses computed values
A child inherits the parent's computed value, not the parent's declared text. This matters for relative units.
.parent {
font-size: 20px;
line-height: 1.5;
}
.child {
font-size: 10px;
}For font-size: 20px the computed value is 20px. For line-height: 1.5, the number itself is inherited, which is a special case. Because 1.5 is a unitless number, each child multiplies it by its own font size. The .child element therefore gets 1.5 × 10px = 15px.
Compare this with line-height: 150% or line-height: 1.5em. For those, the computed value is an absolute length, calculated against the parent's font size. The child inherits 30px and keeps it, even when its own text is smaller. That produces cramped or loose text, so prefer unitless line heights.
The same reasoning applies to percentages and em in font-size. A font-size: 120% rule is resolved against the parent's computed font size, and the result is passed on. Nested elements with 120% compound, so each level grows by another 20%.
ul { font-size: 120%; }With a list nested inside a list, the inner items render at 144% of the outer context, which is a common cause of text that grows as lists nest.
Inheritance follows the element tree, not the selectors
Inheritance follows the document tree. It does not follow where a rule sits in the stylesheet, and it ignores which selectors matched the parent. Only the parent's final computed value matters.
Two consequences follow from this:
- A property's value on a child can come from a grandparent when the parent has no cascaded value for that property. The value passes through unchanged.
- Pseudo-elements such as
::beforeand::afterinherit from the element they belong to, which is their originating element, not from its parent.
Common errors
A direct rule beats an inherited value
.card { color: crimson; }
a { color: blue; }Links inside .card are blue, not crimson. The a rule targets the links directly, so the cascade produces a value and inheritance never runs. This is a useful point to remember when you set color on a container and a child ignores it. The browser's own stylesheet often declares a { color: ... }, which is why links do not pick up the surrounding text color without help.
The fix is to ask the element to inherit explicitly:
.card a { color: inherit; }Form controls do not match the page font
Buttons, inputs and textareas have user agent styles that set their own font-family, font-size and sometimes color. These declarations come from the browser's origin, so they win over inheritance. A common fix is:
button,
input,
select,
textarea {
font: inherit;
color: inherit;
}Expecting a border or margin to pass down
If you set margin or border on a wrapper and expect the children to match, nothing happens. These properties are not inherited. Either target the children with a selector, or pass the value through a custom property, which is covered in a later chapter.
Expecting `background` to inherit
An element with no background looks like it shares its parent's background, because the initial value of background-color is transparent. The parent's background shows through. Nothing was inherited. If you give the child opacity or a positioned layer, the difference becomes visible.
Forcing and blocking inheritance
You can override the default behavior for a single property with a keyword:
button {
font: inherit;
color: inherit;
}
.card {
margin: unset;
}inherittakes the parent's computed value, whether or not the property normally inherits.unsetacts likeinheritfor inherited properties and likeinitialfor non-inherited ones. In the example,marginis not inherited, somargin: unsetresets it to0.
A later chapter covers these keywords, along with initial, revert and the all shorthand.
Inheritance and custom properties
Custom properties (names starting with --) inherit by default, whatever they hold. This gives you a way to pass any value down the tree, including values for properties that do not inherit.
:root { --gap: 1rem; }
.compact { --gap: 0.25rem; }
.stack > * + * { margin-top: var(--gap); }Every .stack inside a .compact element picks up the smaller --gap. Other stacks use the value from :root. margin-top itself still does not inherit. Only --gap does, and var(--gap) reads the nearest value.
Checking inheritance in developer tools
- Select an element in the Elements or Inspector panel.
- In the Styles panel, scroll past the element's own rules. Sections titled "Inherited from
body" or similar list inherited properties, grouped by the ancestor they came from. Browsers show only properties that are inheritable. - Open the Computed panel and expand a property. It lists the rule that supplied the value. For an inherited value it shows where it came from. Some browsers have a "Show all" or "Group" option to display properties that match the initial value.
If a value looks wrong, work upward. Find the nearest ancestor that has a cascaded declaration for the property. That ancestor is the source.
Summary of the rules
- A cascaded value always beats inheritance, including values from the browser's default stylesheet.
- Inherited properties take the parent's computed value. Non-inherited properties fall back to their initial value.
- Text and font properties inherit. Box, layout and background properties do not.
- Use unitless
line-heightso each child recalculates against its own font size. - Custom properties inherit and can carry values to any property.
Exercise
A page has .panel { color: crimson; } and a .panel contains a link <a href="#">Read more</a>. The browser's default stylesheet sets link color. The link is not crimson. Why, and what single declaration makes it crimson?
Show answer
The link has a matching declaration from the browser's default stylesheet, so the cascade produces a value and inheritance never applies. Adding .panel a { color: inherit; } makes the link take the computed color of its parent, which is crimson.
Exercise
A parent has font-size: 20px and a rule line-height: 1.5. A child sets font-size: 10px. What is the child's computed line height? What would it be if the parent used line-height: 150% instead?
Show answer
With the unitless 1.5, the number is inherited and the child computes 1.5 × 10px = 15px. With 150%, the parent computes 30px and the child inherits that fixed length, so its line height is 30px.
Exercise
You set border: 2px solid red on a div and expect its child paragraphs to have red borders too. They do not. Why? Name a property in the same group that the paragraphs would inherit if you set it on the div.
Show answer
border is a box property and is not inherited, so each paragraph uses the initial value, none. Text properties such as color, font-family or text-align are inherited and would pass down.