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 calculate specificity as an (IDs, classes, types) triple. It explains how a value reaches an element without any rule targeting that element, and why such a value gives way to any rule that does target it.
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. Only some properties inherit by default. Text-related properties such as color, font-family, font-size, line-height, and text-align inherit. Box-related properties such as margin, padding, border, and background do not.
article {
color: #333;
font-family: Georgia, serif;
border: 1px solid #ccc;
}Every element inside article receives the gray text and the serif font. None of them receives the border. A paragraph with a border on every child would be rarely wanted, which is why border does not inherit.
Inheritance has no specificity
An inherited value is not a declaration that competes in the cascade. The browser first looks for declarations that match the element itself. Only when none sets the property does it fall back to inheritance. So an inherited value loses to any matching rule, even the weakest.
#page {
color: green; /* (1,0,0), but it targets #page, not the link */
}
a {
color: blue; /* (0,0,1), targets the link directly */
}<div id="page">
<p>Text with <a href="/docs">a link</a>.</p>
</div>The paragraph is green, because it inherits from #page. The link is blue. The ID selector has far higher specificity than a, but it never matches the link. The type selector does, so it wins. The same effect explains why links ignore the color of the surrounding text unless you reset it.
Controlling inheritance
Four keywords let you take control of this behavior on any property.
| Keyword | Effect |
|---|---|
| `inherit` | Take the parent's computed value, even for properties that normally do not inherit. |
| `initial` | Use the property's specification default. |
| `unset` | Act as `inherit` for inheriting properties and as `initial` for the rest. |
| `revert` | Roll back to the value from the previous origin, usually the browser's default styles. |
.card {
border: 2px solid teal;
}
.card .label {
border: inherit; /* copies the parent's border */
}
a {
color: inherit; /* links now use the surrounding text color */
}The a { color: inherit; } rule is a common pattern. It is a direct rule, so it beats the browser's default blue link color, and its value is whatever the parent has.
Common errors
- Setting a style on a container and expecting it to override a rule on a child. The container's value only applies if nothing targets the child.
- Expecting
marginorborderon a parent to flow down to children. - Forgetting that browsers apply default styles directly to elements such as
a,h1, andbutton. These count as direct rules, so inherited values lose to them. - Using
!importanton the parent to force a child's color. It does not help, because inheritance is not part of the cascade contest.
Exercise
Given body { color: red; } and p { color: blue; }, what color is a <strong> element inside a <p>, and why? Would changing the first rule to body { color: red !important; } change the answer?
Show answer
The <strong> is blue. No rule targets strong for color, so it inherits from its parent p, whose color is blue. Adding !important to the body rule does not change this. That declaration only applies to body, and strong inherits the computed value of p, which is still blue.