Which CSS properties are inherited
21 What is inherited and what is not
fairSet typography once, high up, and let it flow down. Then deal with the one family of elements that refuses to take part.
Stuck? Three hints, smallest first
- 1. Text properties inherit:
color,font-family,font-size,line-height,text-align. - 2. Box properties do not:
border,padding,margin,background,display. - 3. Form controls are the exception. They take their font from the operating system until you say
font: inherit.
<div class="card">
<p>This paragraph was never given a colour or a font. It inherited both.</p>
<p><em>So did this emphasis, through two levels.</em></p>
<input type="text" value="And this control, only because it was told to">
</div>
.card {
/* Set once here. Everything textual below picks it up. */
color: #17171a;
font-family: Georgia, 'Times New Roman', serif;
font-size: 0.95rem;
line-height: 1.6;
/* None of these reach the children. */
padding: 14px;
border: 1px solid #c9cad1;
border-radius: 8px;
background: #f7f7f8;
}
.card p { margin: 0 0 10px; }
/* Controls do not inherit typography. This is the fix, and it is not optional
if you want the form to look like the page. */
.card input {
font: inherit;
color: inherit;
padding: 6px 8px;
border: 1px solid #8e8e98;
border-radius: 6px;
background: #ffffff;
width: 100%;
box-sizing: border-box;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
// The children are never given a colour or a family of their own.
const paraRule = (/\.card p\s*\{([^}]*)\}/.exec(css) || [, ''])[1];
test('the paragraph rule sets no colour of its own', /color/.test(paraRule), false);
test('nor a font family', /font-family/.test(paraRule), false);
test('both are set once, on the ancestor', /\.card\s*\{[\s\S]*?color:\s*#17171a[\s\S]*?font-family:/.test(css), true);
test('the control is told to inherit, because it will not on its own', /font\s*:\s*inherit/.test(css), true);
test('and to inherit the colour too', /color\s*:\s*inherit/.test(css), true);
test('box properties are declared where they are wanted, not inherited', /\.card\s*\{[\s\S]*?padding:/.test(css), true);
test('the control needs its own border, since border does not inherit', /\.card input\s*\{[\s\S]*?border:/.test(css), true);
test('the em inherits through two levels without a rule of its own', /em\s*\{/.test(css), false);
Output
Inheritance is why a stylesheet can be short: set the typography on one ancestor and every descendant that renders text picks it up, including ones you add later. Box properties deliberately do not inherit, or every nested div would draw its own border. The trap is form controls: <input>, <select>, <textarea> and <button> start from the operating system font, not the page, so a form looks foreign until you write font: inherit. That is why the shorthand appears in nearly every reset ever published.