Set font-family on the <body> and every paragraph, heading and list on the page uses that font. You didn't write a rule for each one; they inherited it. Inheritance is one of the three ideas behind the "cascading" in Cascading Style Sheets, alongside specificity and source order, and it saves you from repeating yourself thousands of times.
But not everything inherits. Set a border on a <div> and its children don't get borders. Knowing which properties pass down, and how to force or stop it, explains a lot of "why is this text that colour?" moments.
This lesson also covers the tools for winning cascade battles: the !important flag, which beginners often overuse, and cascade layers (@layer), the modern way to decide in advance which group of styles wins. Used well, layers mean you almost never need !important again.
Which properties inherit?
| Usually inherited (text-related) | Not inherited (box-related) |
|---|---|
color, font-family, font-size, font-weight | margin, padding, border |
line-height, letter-spacing, text-align | width, height, display |
list-style, visibility, cursor | background, position, overflow |
Custom properties (--brand) | box-shadow, opacity, transform |
The rule of thumb: properties that affect text inherit, and properties that affect the box don't. If borders inherited, every nested element would get a border, which would be chaos.
Controlling inheritance with keywords
Four keywords work on any property:
| Keyword | Effect |
|---|---|
inherit | Take the parent's value, even for properties that don't normally inherit |
initial | Use the property's default value from the CSS specification |
unset | Act like inherit for inherited properties, and initial for the rest |
revert | Go back to the browser's own default style for that element |
A common use is button { font: inherit; color: inherit; }, because form controls don't inherit fonts by default. The all property resets every property at once: all: unset strips an element back to a blank slate.
!important
Adding !important to a declaration makes it beat any normal declaration, whatever the specificity:
.alert { color: red !important; }It's a sledgehammer. Once one rule uses it, the only way to override it is another !important with higher specificity, and stylesheets end up in an arms race. Acceptable uses are rare: utility classes that must always win, such as .hidden { display: none !important; }, overriding inline styles from a third-party widget you can't edit, and user accessibility stylesheets.
Cascade layers with @layer
Layers let you group styles and say which group wins, before specificity is even checked:
@layer reset, base, components, utilities;
@layer base { a { color: blue; } }
@layer components { .nav a { color: black; } }
@layer utilities { .text-red { color: red; } }The first line sets the order. Later layers beat earlier layers, so a simple .text-red in utilities beats a more specific selector in components. Styles that aren't in any layer beat all layered styles. This is ideal for bringing in a framework or reset: put it in an early layer and your own styles win without fighting specificity.
CSS Inheritance Syntax
Inheritance and layers
.child { color: inherit; border: inherit; }
.reset { margin: initial; }
.plain { all: unset; }
.undo { font-weight: revert; }
.hidden { display: none !important; }
@layer base, components;
@layer base { h1 { color: navy; } }CSS Inheritance Summary
- Text-related properties such as
colorandfontinherit; box properties such asmarginandborderdon't. inherit,initial,unsetandrevertcontrol inheritance for any property.- Form controls don't inherit fonts, so use
font: inheriton them. !importantbeats normal rules but makes CSS hard to maintain; keep it for utilities.@layersets which group of styles wins, regardless of specificity.
CSS Inheritance Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Inheritance in action
<div class="card">
<h3>Inherited styles</h3>
<p>This text is purple and in a serif font, but has no border.</p>
<button>A button</button>
<button class="fixed">Fixed button</button>
</div>.card {
color: #6d28d9;
font-family: Georgia, serif;
border: 3px solid #6d28d9;
padding: 12px;
}
.fixed {
font: inherit;
color: inherit;
}Line-by-line explanation
- Lines 2 to 3Colour and font pass down to the heading and paragraph.
- Line 4The border stays on the card; children don't get their own borders.
- Lines 9 to 10Buttons don't inherit fonts by default, so the second one asks for them.
inherit, initial, unset and revert
<div class="parent">
<p class="a">inherit border</p>
<p class="b">initial color</p>
<h2 class="c">revert font weight</h2>
</div>.parent {
color: #b45309;
border: 2px dashed #b45309;
padding: 8px;
font-family: sans-serif;
}
.a { border: inherit; padding: 4px; }
.b { color: initial; }
.c { font-weight: 400; }
.c { font-weight: revert; }Line-by-line explanation
- Line 8The paragraph copies the parent's dashed border.
- Line 9The default colour value is black, not the parent's amber.
- Lines 10 to 11
revertgoes back to the browser's bold for headings, undoing line 10.
Cascade layers beat specificity
<nav class="nav"><a href="#" class="text-red">Sale</a> <a href="#">Shop</a></nav>@layer components, utilities;
@layer components {
.nav a { color: #111827; font: 600 18px sans-serif; }
}
@layer utilities {
.text-red { color: #dc2626; }
}Line-by-line explanation
- Line 1Utilities come after components, so they win.
- Line 4
.nav ahas higher specificity than.text-red. - Line 8The single class still wins, because its layer is later.
Common CSS Inheritance Mistakes
Expecting borders and padding to inherit
.box { padding: 20px; } /* expecting children to get padding too */.box > * { padding: 20px; }Box properties don't inherit. Target the children directly if they need the style.
Fixing every conflict with !important
.button { color: white !important; }
.nav .button { color: black !important; }@layer base, components;
@layer components { .button { color: white; } }Fix the cause: lower the specificity of the winning rule, reorder rules, or use layers.
Forgetting font inheritance on form controls
body { font-family: "Inter", sans-serif; } /* inputs and buttons still use the system font */body { font-family: "Inter", sans-serif; }
input, button, select, textarea { font: inherit; }CSS Inheritance in Real Projects
Inheritance and layers in practice
Modern CSS resets begin with font: inherit on form controls. Tailwind CSS v4 puts its base styles, components and utilities into cascade layers, so utilities always win. Large companies use layers to keep third-party widget styles below their own. !important still appears in utility classes such as Bootstrap's .d-none, which must hide an element no matter what.
CSS Inheritance Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Make the button use its parent's font.
button {
font: ;
}
inherit takes the parent's value.Which property is inherited by default?
What does @layer base, utilities; do?
Which keyword returns a property to the browser's default for that element?
The note should use the card's colour and font, and the input should match the page font. Fix the CSS.
<div class="card">
<p class="note">Card note</p>
<input class="field" value="Name">
</div>.card { color: rgb(22, 101, 52); font-family: Georgia, serif; }
.note { color: initial; }
.field { font-family: initial; }- Note uses the card colour
- Input uses the card font
CSS Inheritance Quiz
5 questions to check what you learned.
Why doesn't a border set on a parent appear on its children?
- It's a bug
- Borders need !important
- border isn't an inherited property
- Children can't have borders
Which styles win when cascade layers are used?
- Unlayered styles, then the last layer declared
- Layered styles always beat unlayered ones
- The first layer declared
- The most specific, ignoring layers
What does all: unset do?
- Deletes the element
- Removes classes
- Turns off JavaScript
- Resets every property to inherit or initial
When is !important reasonable?
- On every colour
- In utility classes that must always apply, like a hidden class
- To fix any conflict quickly
- It's never allowed
Are CSS custom properties inherited?
- No, never
- Only on :root
- Yes, they inherit by default
- Only with !important
CSS Inheritance Challenge
Tidy up a form's styles
- Make all
buttonandinputelements usefont: inherit - Create layers in this order:
base,utilities - In
base, give.form buttonacolorofrgb(255, 255, 255)and abackgroundofrgb(37, 99, 235) - In
utilities, give.dangerabackgroundofrgb(220, 38, 38)
<form class="form">
<input class="in" value="Email">
<button class="ok" type="button">Save</button>
<button class="danger" type="button">Delete</button>
</form>.form { font: 18px Georgia, serif; }
- Controls inherit the font
- Save button is blue
- Delete button is red
- Uses cascade layers
.form button is more specific than .danger.