CSS Inheritance
By the end of this lesson you will be able to predict which properties a child takes from its parent, explain why, and control it with the keywords inherit, initial, unset, revert and revert-layer. You will also reset a whole element with the all property and fix the fonts in buttons and inputs, which do not follow your page font by default.
CSS Specificity gave you the short version: text properties inherit and box properties do not. This lesson goes underneath that rule.
Why some properties inherit
Inheritance means that when no rule sets a property on an element, the element takes the value its parent has. It only happens for properties defined as inherited in the CSS specification.
Think of a family in Jaipur that speaks Hindi at home. A child picks up the home language without anyone teaching it, unless the child's school decides otherwise. But the child does not "inherit" the size of the house. Each person still needs their own space.
CSS works the same way. Text often contains nested inline elements: a <strong> or <a> inside a paragraph. You want those to keep the paragraph's font and colour automatically. Boxes are different: if every child copied its parent's border or padding, nested elements would pile up borders inside borders.
| Inherited (the "home language") | Not inherited (each box's own) |
|---|---|
color, all font-* properties, line-height | margin, padding, border |
letter-spacing, word-spacing, text-align, text-indent, text-transform, white-space | width, height, display, position |
list-style, border-collapse, border-spacing | background, box-shadow, opacity |
visibility, cursor, direction, writing-mode | transform, z-index, overflow |
accent-color, color-scheme, custom properties | text-decoration (it propagates instead) |
To check any property, open its page on MDN and look for "Inherited: yes" or "no" in the formal definition table.
Three surprises
Three properties confuse almost everyone, because what you see does not match the inheritance rule.
opacityis not inherited, yet children fade with the parent. The browser paints the parent and its children as one group, then fades the group. Settingopacity: 1on a child cannot make it more solid than its parent.visibilityis inherited, and a child can override it. A child withvisibility: visibleappears inside avisibility: hiddenparent. Withdisplay: noneon the parent, nothing inside can ever appear.text-decorationis not inherited, but it propagates. An underline on a paragraph is drawn across all its inline children.text-decoration: noneon a child<span>cannot remove it. Making the childdisplay: inline-blockstops the line, because decorations do not pass into inline-block boxes.
Step 1: see the surprises
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Inheritance surprises</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1rem; }
.box { padding: 0.75rem; margin-bottom: 0.75rem; background: #dbeafe; }
.faded { opacity: 0.4; }
.faded strong { opacity: 1; } /* has no visible effect */
.hidden-parent { visibility: hidden; }
.hidden-parent .peek { visibility: visible; }
.underlined { text-decoration: underline; }
.underlined .no-line { text-decoration: none; } /* still underlined */
.underlined .block { display: inline-block; } /* line stops here */
</style>
</head>
<body>
<div class="box faded">Train 12015 is <strong>delayed</strong> by 40 minutes.</div>
<div class="box hidden-parent">This text is hidden, <span class="peek">but this part peeks through.</span></div>
<p class="underlined">Platform 4: <span class="no-line">Ajmer Shatabdi</span> and <span class="block">Jaipur Intercity</span></p>
</body>
</html>You should see the whole first box faded, including the bold word. In the second box only "but this part peeks through" is visible, while the box keeps its space. In the last line "Ajmer Shatabdi" is still underlined, while "Jaipur Intercity" is not.
Children inherit the computed value
A child does not inherit the text you typed in the parent's rule. It inherits the parent's computed value: the value after the browser has turned relative units into something fixed where the property's definition says so.
For font-size, an em or % value becomes pixels. If a section is 16px and you set section { font-size: 1.25em; }, the computed value is 20px, and every child inherits 20px, not "1.25em". This is also why nested lists shrink when you write li { font-size: 0.9em; }: each level computes 0.9 of an already reduced size.
line-height is the famous case. 1.5em is computed to pixels on the parent, so a large heading inside inherits a line height that is too small. A unitless 1.5 stays a number, and each child multiplies it by its own font size. Line Height & Text Transformation shows this trap with a live example, so here you only need the reason: numbers are inherited as numbers, lengths as lengths.
The defaulting keywords
Every CSS property accepts five special keywords. They let you choose where a value comes from instead of writing one.
| Keyword | What it gives the element |
|---|---|
inherit | The parent's computed value, even for a property that does not usually inherit |
initial | The starting value from the specification, ignoring the browser's defaults |
unset | inherit for inherited properties, initial for the rest |
revert | The value from the browser's own style sheet (or the user's), as if your styles did not exist |
revert-layer | The value from the previous cascade layer; outside layers it acts like revert |
The trap is initial. It does not mean "the browser default for this element". The specification's starting value for display is inline, so div { display: initial; } turns a block into an inline element. The browser's style sheet is what makes a <div> a block, and revert is the keyword that brings that back. revert is Baseline widely available, and revert-layer is Baseline 2022.
Step 2: compare the keywords
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Defaulting keywords</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1rem; }
section { color: #7c2d12; border: 2px solid #ea580c; padding: 0.5rem; }
div { background: #ffedd5; margin: 0.25rem; padding: 0.25rem; display: flex; color: #1e3a8a; }
.k-inherit { color: inherit; border: inherit; }
.k-initial { color: initial; display: initial; }
.k-unset { color: unset; display: unset; }
.k-revert { color: revert; display: revert; }
</style>
</head>
<body>
<section>
<div>Normal div: blue text, flex</div>
<div class="k-inherit">inherit: brown text and the parent's orange border</div>
<div class="k-initial">initial: black text, inline</div>
<div class="k-unset">unset: brown text, inline</div>
<div class="k-revert">revert: brown text, block</div>
</section>
</body>
</html>Read the result row by row. initial and unset both make the box inline, so those two rows shrink to fit their text and sit side by side. revert gives a block again, and its colour is brown because the browser's style sheet has no colour for a <div>, so the inherited value wins.
The all shorthand
The all property sets every property at once to one of the five keywords. It does not touch direction, unicode-bidi or custom properties.
/* A button that looks like plain text, then gets its own focus ring back */
.text-button {
all: unset;
cursor: pointer;
color: #1d4ed8;
}
.text-button:focus-visible {
outline: 2px solid currentColor;
outline-offset: 2px;
}all: unset wipes the browser's button styles, but it also wipes the focus outline, so keyboard users can no longer see where they are. Always add a :focus-visible style back. all: revert is gentler: it undoes your styles on that element and returns the browser defaults, which is useful for embedded content that should not pick up a site's theme.
Form controls do not inherit fonts
Browsers style <button>, <input>, <select> and <textarea> with their own font in the browser's style sheet, usually a small system font of about 13px. Because a value is set directly on those elements, they do not inherit the font from body. Your page uses one font, and your search box uses another.
Step 3: fix the form font
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Form font fix</title>
<style>
body { font-family: Georgia, serif; font-size: 18px; padding: 1rem; }
.fixed :is(button, input, select, textarea) { font: inherit; }
form { margin-bottom: 1rem; }
</style>
</head>
<body>
<form>
<p>Before: Kabir's Chai order</p>
<input value="2 masala chai"> <button type="button">Order</button>
</form>
<form class="fixed">
<p>After: Kabir's Chai order</p>
<input value="2 masala chai"> <button type="button">Order</button>
</form>
</body>
</html>The first input and button look small and use a different typeface. The second pair matches the paragraph. Most modern resets include button, input, select, textarea { font: inherit; }, which you will meet in CSS Reset & Normalize. Be careful with color: inherit on inputs: check that the text stays readable on the field's background.
Common mistakes
- Using
initialto mean "browser default". It means the specification's starting value. Userevert. - Trying to un-underline a child. Change the decoration on the parent, or make the child
inline-block. - Expecting
opacity: 1to rescue a child. Fade the background colour with an alpha value instead of fading the whole box. all: unseton buttons with no focus style. Add:focus-visibleback.- Forgetting form controls. Add
font: inheritonce in your base styles.
Interview questions
1. Why do text properties inherit while box properties do not?
Text is nested: links, bold words and spans sit inside paragraphs and should look like them without extra rules. Box properties belong to one box. Inheriting a border or padding would repeat it on every nested element.
2. What is the difference between initial and revert?
initial uses the property's starting value from the specification, so display: initial is inline for every element. revert rolls back to the browser's or user's style sheet, so display: revert on a <div> gives block.
3. Is the specified value or the computed value inherited?
The computed value. A parent with font-size: 1.25em at 16px passes 20px to its children. That is why line-height: 1.5em passes a fixed length, while line-height: 1.5 passes a number that each child multiplies by its own font size.
4. Why do buttons and inputs not use the page font, and how do you fix it?
The browser's style sheet sets a font directly on form controls, and a direct value always beats inheritance. Writing font: inherit on button, input, select, textarea makes them take the parent's font.
5. What does all: unset do, and what is the risk?
It sets every property, except direction, unicode-bidi and custom properties, to inherit or initial. The risk is losing useful defaults such as the focus outline and the element's display type, so you must add accessible focus styles back.
Key points
- Inherited properties take the parent's value when no rule sets them; mostly text, list and table properties.
opacityandtext-decorationare not inherited but still affect children;visibilityis inherited and can be overridden.- Children inherit computed values, which is why
emline heights cause trouble and unitless numbers do not. inherit,initial,unset,revertandrevert-layerchoose where a value comes from.initialis the specification's value, not the browser default;revertis the browser default.allresets every property at once; add focus styles back afterall: unset.- Form controls need
font: inheritto match the page.
Exercise
Create a school notice page for Meera's Kochi school. Set a font, size and colour on body. Add a notice card with a border and a link inside it. Then:
- make the card's heading use the browser's normal heading size again after you set
h2 { font-size: 1rem; }on the page (use one keyword); - give a "Read more"
<button>the card's font and colour, and make it look like plain text with an accessible focus ring; - make a
.stampelement inside the card copy the card's border.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Notice</title>
<style>
body { font-family: system-ui, sans-serif; font-size: 18px; color: #1f2937; padding: 1rem; }
h2 { font-size: 1rem; }
.card { border: 3px double #0f766e; padding: 1rem; }
.card h2 { font-size: revert; }
.card button { all: unset; font: inherit; color: inherit; text-decoration: underline; cursor: pointer; }
.card button:focus-visible { outline: 2px solid #0f766e; outline-offset: 2px; }
.stamp { border: inherit; display: inline-block; padding: 0.25rem 0.5rem; }
</style>
</head>
<body>
<article class="card">
<h2>Annual Day on 20 December</h2>
<p>Parents are invited to the school hall at 5 pm. <span class="stamp">Approved</span></p>
<button type="button">Read more</button>
</article>
</body>
</html>font-size: revert returns the heading to the browser's own size for an <h2>. all: unset clears the button styles, and the next declarations add back what is needed, including a visible focus ring. border: inherit copies a property that is not normally inherited.
Quick Quiz
1. Which property is inherited by default?
visibility inherits, which is why a child can override it with visible.
2. What does display: initial give a <div>?
The specification's initial value of display is inline; the browser's style sheet is what makes a div a block.
3. A parent has font-size: 20px; line-height: 1.5em;. What line height does a child with font-size: 40px inherit?
1.5em is computed to 30px on the parent, and the child inherits that fixed length.
4. Why does a <button> ignore the font set on body?
Inheritance only fills in a value when nothing sets the property on the element.
Learn CSS with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
