This paragraph will use Courier New as well.
CSS inherit
In CSS, the inherit
keyword tells an element
to take the same computed style as its parent element.
In other words, if you apply inherit to a CSS
property, that property will "inherit" its value from the parent element in the DOM
tree.
Why Use
inherit?
Many CSS properties do not get their value automatically from their parent elements. For example:
colorusually does inherit.margin,padding,border, andwidthdo not inherit by default.
So if you want a child element to look the same as its
parent - for properties that do not inherently inherit their
value - then you add inherit.
Basic
Syntax of inherit
selector {
property: inherit;
}
Example 1: Inheriting Font Color
HTML
<div class="parent-red-text">
This is a red text.
<p class="child-inherit-color">This paragraph inherits the red color.</p>
</div>
CSS (relevant part)
.parent-red-text {
color: red;
}
.child-inherit-color {
color: inherit; /* This will inherit 'red' from its parent */
}
Example in action:
This paragraph inherits the red color.
Here, the <p> tag
doesn't specify its
own color. Instead, it says: “I'll inherit it from my parent,” which is red.
Example 2: Inheriting Margin (which doesn’t inherit by default)
HTML
<div class="parent-margin-box">
Parent Box (with 20px margin)
<p class="child-inherit-margin">This paragraph inherits the margin from its parent.</p>
</div>
CSS (relevant part)
.parent-margin-box {
margin: 20px;
}
.child-inherit-margin {
margin: inherit; /* This will inherit '20px' margin from its parent */
}
Example in action:
This paragraph inherits the margin from its parent.
Normally, margins don't inherit, but this tells the browser to do it anyway.
When Should
You Use inherit?
- To ensure consistency in style (like fonts, spacing).
- When creating responsive design systems or design tokens.
- In reset styles, for example:
* { box-sizing: inherit; } html { box-sizing: border-box; }This way, every element takes
box-sizingfromhtml.
Real-Life Example: Inheriting Fonts Across Sections
HTML
<section class="section-font-example">
Section using 'Courier New'
<article class="article-inherit-font">
Article inheriting font
<p>This paragraph will use Courier New as well.</p>
</article>
</section>
CSS (relevant part)
.section-font-example {
font-family: 'Courier New', monospace;
}
.article-inherit-font {
font-family: inherit; /* Inherits 'Courier New' */
}
Example in action:
This helps ensure all parts of a section follow the same font style, even if deeply nested.
Common Mistake to Avoid
CSS
body {
font-size: 16px;
}
p {
font-size: 14px;
}
span {
font-size: inherit;
}
HTML
<div class="body-font-size">
Body text (16px)
<p class="paragraph-font-size">
Paragraph text (14px).
This <span class="span-inherit-font-size">span inherits 14px</span> from the paragraph.
</p>
</div>
Example in action:
Paragraph text (14px). This span inherits 14px from the paragraph.
If the <span> is
inside a <p>, it’ll inherit 14px, not 16px.
Remember: inherit pulls from the immediate parent,
not from body.
Building
Strong CSS Architecture with inherit
Using inherit lets you:
- Avoid repetitive code
- Design modular, scalable styles
- Keep style logic centralized
- Maintain DRY (Don’t Repeat Yourself) principles
Advanced
Tip: Use inherit in Forms
CSS
input,
textarea,
select {
font-family: inherit;
font-size: inherit;
color: inherit;
}
HTML
<div class="form-elements-container">
<p>Form elements below will inherit font styles from this container.</p>
<input type="text" placeholder="Your Name">
<textarea placeholder="Your Message"></textarea>
<select>
<option>Option 1</option>
<option>Option 2</option>
</select>
</div>
Example in action:
Form elements below will inherit font styles from this container.
This ensures your form elements look the same as surrounding content — helpful in custom-styled UIs.
Bonus
Example: Using CSS Variables + inherit
CSS
:root {
--main-color: #3498db;
}
body {
color: var(--main-color);
}
p {
color: inherit; /* Will get the same color from body */
}
HTML
<div class="body-color-var">
This text is using --main-color from :root.
<p class="paragraph-inherit-var">This paragraph inherits the color.</p>
</div>
Example in action:
This paragraph inherits the color.
This combo of CSS variables and inheritance is powerful for theming your website.
What’s the
Difference: initial, unset, inherit?
inherit– Get value from parentinitial– Reset to default value (browser default)unset– Acts likeinheritif the property is inheritable, orinitialif not
Example:
color: initial; /* goes to browser default (usually black) */
Use Cases You Can Practice
- Create a blog layout where all text inherits color and font-size.
- Style form inputs using inherited font styles.
- Make a responsive card design with inherited spacing and typography.
- Build a navigation menu with inherited link styles from a wrapper class.
Summary:
When to Use inherit
Use inherit in CSS when:
- You want to pass down styles like color, font, spacing.
- You want form elements or child components to look consistent.
- You need to override conflicting styles without hardcoding values again.
Conclusion
CSS inherit is like
passing on a family tradition from parent to child. It is simple, but powerful to
developers. If you
learn to understand inherit and use it properly, you can keep your code cleaner, and
"smarter", and writing maintenance
will get easier.
Whether you are creating a simple & beautiful landing page
or an intricate application, remember this simple keyword -
inherit- keeps it all linked together like threads
in a spider web.