Sub topic 10
Inheritance and Why It Loses to Direct Rules
This chapter assumes you can read a CSS rule, write type, class, and ID selectors, and know that specificity ranks competing selectors. Here you will learn how a property value passes from parent to child, and why even a very weak direct rule beats an inherited value.
What inheritance is
Inheritance is the process by which an element takes the computed value of a property from its parent when no rule sets that property on the element itself. You set color once on body, and every paragraph, list item, and link inside it gets that color without a rule of its own.
body {
color: #222;
font-family: Georgia, serif;
}The browser does not copy the rule to the children. It resolves each property for each element, and for an inherited property with no matching declaration it uses the parent's computed value.
Which properties inherit
Not every property inherits. The CSS specifications mark each property as inherited or not.
| Group | Examples | Inherited? |
|---|---|---|
| Text and font | `color`, `font-family`, `font-size`, `line-height`, `text-align`, `letter-spacing` | Yes |
| List and table | `list-style`, `border-collapse` | Yes |
| Other | `visibility`, `cursor` | Yes |
| Box model | `margin`, `padding`, `border`, `width`, `height` | No |
| Background and layout | `background`, `display`, `position`, `float` | No |
The pattern is that properties describing text inherit, and properties describing a box do not. If border inherited, one border on a div would draw a border around every descendant.
Inheritance has no specificity
Inheritance is not a rule, so it has no selector and no specificity. It is the fallback the browser uses only when no declaration targets the element for that property.
body { color: green; }
p { color: blue; }<body>
<p>Hello <em>world</em></p>
</body>The p matches the p rule directly, so it is blue. The em has no rule setting color, so it inherits from p, not from body. The nearest ancestor with a value supplies it. The em is blue.
Now compare a strong rule on a distant ancestor with a weak rule on the child:
#page { color: red; } /* (1,0,0) */
em { color: black; } /* (0,0,1) */If em sits somewhere inside #page, it is black. The ID selector never matches em. It matches #page, and the em only receives red as a passed-down value. Any rule that matches the element directly, even the weakest type selector, wins over that.
Why this surprises people
A common bug looks like this:
.card a { color: inherit; }versus a global rule:
a { color: royalblue; }A developer sets color: crimson on .card and expects the links inside to turn crimson. They stay royalblue. The reason is that the browser's own stylesheet and the a { color: royalblue; } rule match the links directly, so inheritance never gets a chance. Even the default stylesheet counts: browsers give a a built-in color, so links ignore the color of their parent by default.
The same happens with form controls. Many browsers set font-family and font-size on input and button in their default stylesheet, so a font-family on body does not reach them.
Forcing inheritance with keywords
You can ask for the parent's value explicitly. A declaration using a keyword is a direct rule, so it participates in the cascade like any other.
| Keyword | Meaning |
|---|---|
| `inherit` | Use the parent's computed value, even for a property that normally does not inherit. |
| `initial` | Use the property's initial value from the specification. |
| `unset` | Act as `inherit` for inherited properties and as `initial` for the others. |
| `revert` | Roll back to the value from the previous origin, such as the browser default. |
a { color: inherit; text-decoration: underline; }
.panel {
border: 2px solid teal;
}
.panel .inner {
border: inherit;
}The first rule makes every link use the surrounding text color. The second makes .inner copy its parent's border. Because border does not inherit by default, the keyword is the only way to get that effect.
A reset like all: unset on a button removes browser styling, but it also drops display, cursor, and other defaults, so use it with care.
Walking the resolution order
For each property on each element, the browser follows these steps:
- Collect all declarations from rules whose selectors match the element.
- If any exist, the cascade picks the winner by origin, importance, layers, specificity, and source order.
- If none exist and the property is inherited, use the parent's computed value.
- Otherwise use the property's initial value.
Step 2 always comes first. This is why inheritance loses to direct rules: it sits below the cascade in the decision order, not inside it.
Inherited values are computed values
What passes down is the computed value, not the text you wrote. This matters for relative units.
body { font-size: 20px; line-height: 1.5; }
.a { line-height: 150%; }With line-height: 1.5, the unitless number is inherited as a number, so each child multiplies it by its own font size. With 150%, the percentage is resolved to pixels on the element where it is declared (30px for a 20px font), and descendants inherit 30px, even if their text is larger. Large headings inside .a then have lines that are too tight. Unitless line-height is the usual fix.
Using inheritance on purpose
Good practice is to set inherited properties once near the root and override only where needed.
html {
font-family: system-ui, sans-serif;
color: #1a1a1a;
}
button,
input,
select,
textarea {
font: inherit;
color: inherit;
}This sets typography once, then uses font: inherit to bring form controls in line. Because the controls are matched directly, the keyword is what links them back to the parent.
Common errors
- Setting
coloron a container and expecting links or buttons to follow it. Check whether the element has a direct rule, including from the browser's stylesheet. - Expecting
marginorborderon a parent to affect children. These do not inherit. - Using a high-specificity selector on an ancestor to "force" a value down. It does not raise the value's priority on descendants.
- Declaring
line-heightorfont-sizewith percentages oremand finding that descendants inherit a fixed result.
Exercise
Given #sidebar { color: red; } and li { color: gray; }, with <div id="sidebar"><ul><li>Item</li></ul></div>, what color is the text in the li, and why? What color is the ul?
Show answer
The li is gray. The li rule matches it directly, and the #sidebar rule only matches the div, so red reaches the li only as an inherited value, which a direct rule overrides regardless of specificity. The ul has no direct rule setting color, so it inherits red from #sidebar.
Exercise
A page sets body { color: #333; }, but links inside a paragraph stay blue. Write a rule that makes links follow the surrounding text color, and say why the original body rule had no effect on them.
Show answer
Use a { color: inherit; }. The browser's default stylesheet sets a color on a directly, so links never fall back to inheritance. The inherit keyword is a direct declaration that makes the link take its parent's computed color.