Sub topic 9
Inheritance: which properties pass down and why
This chapter assumes you can write CSS rules, know how the cascade picks a winning declaration, and can inspect an element in browser developer tools. It covers inheritance, the step that supplies a value when no rule targets an element for a given property.
What inheritance does
Inheritance gives a child element the computed value of a property from its parent. It applies only when the cascade finds no declaration for that property on the element. A declaration that targets the element directly always wins over an inherited value, even when its selector has low specificity.
So the order of questions for one property on one element is:
- Does any rule declare this property for the element? If yes, the cascade picks the winner.
- If no, is the property inherited by default? If yes, use the parent's computed value.
- If no, use the property's initial value.
Which properties inherit
Each CSS property is defined as inherited or not. The rule of thumb is that properties controlling text and list appearance inherit, and properties controlling a box do not.
| Inherited by default | Not inherited by default |
|---|---|
| `color` | `border` |
| `font-family`, `font-size`, `font-weight` | `margin`, `padding` |
| `line-height`, `letter-spacing` | `background` |
| `text-align`, `text-transform` | `width`, `height` |
| `list-style`, `visibility`, `cursor` | `display`, `position`, `overflow` |
The reason is practical. If you set a font on body, you want every paragraph and link to use it. If borders inherited, every nested element would draw its own border inside its parent's.
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. */Check the "Inherited" row in the MDN reference page for any property you are unsure about. Do not guess from the property's name.
What gets passed down
The child receives the parent's computed value, not the text you wrote. This matters for relative units.
.parent { font-size: 20px; line-height: 1.5; }
.other { font-size: 20px; line-height: 150%; }With line-height: 1.5, the unitless number is inherited as is, and each child recalculates it against its own font size. With 150%, the parent computes the result (30px) and every child inherits 30px, even a child with a larger font. Prefer unitless line-height for this reason.
Forcing inheritance on or off
Form controls are a common surprise. Browsers give button, input and textarea their own font and color through the user agent stylesheet, so they ignore the page's typography. A declaration targeting the element beats inheritance, and the user agent stylesheet is such a declaration. The inherit keyword fixes this by asking for the parent's value explicitly.
button {
font: inherit;
color: inherit;
}
.card {
margin: unset;
}The unset keyword acts as inherit for inherited properties and as initial for the others. Because margin is not inherited, margin: unset resets it to 0. The next chapter covers these keywords in full.
Common errors
- Expecting a box property to pass down. Setting
borderorpaddingon a container does not style its children. Target the children, or use a custom property. - Forgetting that a direct rule beats inheritance. If a link stays blue while its parent has
color: red, the user agent stylesheet is targetingadirectly. Inheritance never overrides it. - Using a percentage where a number would do. Percentages and lengths in
line-heightfreeze at the parent's computed value. - Assuming
visibility: hiddenhides the whole subtree for good.visibilityinherits, so a child can reappear withvisibility: visible.display: nonecannot be undone by a child.
Seeing inheritance in developer tools
Open the Styles panel and look for the "Inherited from" headings. They list rules from ancestors that supply values to the selected element. Open the Computed panel to see the final value of each property. Many browsers also let you tick "Show all" to see properties that came only from inheritance or from initial values.
Exercise
A page has article { color: crimson; border: 2px solid crimson; } and contains <article><p>Text</p></article>. Without any other rules, what color and border does the p have, and why?
Show answer
The paragraph's text is crimson because color is inherited and no rule targets the p. It has no border, because border is not inherited, so it gets its initial value of none.
Exercise
A parent sets font-size: 20px; line-height: 150%. A child sets font-size: 40px and has no line-height rule. What is the child's line height, and what would change if the parent used line-height: 1.5 instead?
Show answer
The child inherits the computed value of 30px, so its 40px text has a 30px line height and the lines overlap. With 1.5, the child inherits the number and computes 1.5 × 40px = 60px.