- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Inherit
In CSS, the inherit keyword is a powerful value that explicitly tells an element to take the exact same property value as its parent. While some CSS properties (like color or font-family) are inherited by children automatically, many others (like border or padding) are not. The inherit keyword allows you to force this behavior, ensuring visual consistency across nested components.
inherit as a way to create a "live link" to a parent's style. If the parent's property changes in the future, the child will update automatically without you needing to change the child's CSS.
Basic Usage
The inherit keyword can be applied to any CSS property. It acts as a pointer, looking up the DOM tree to the immediate parent and grabbing the "computed value" of that specific property.
Example
/* Parent element */
.parent {
color: #2c3e50;
font-size: 20px;
border: 2px solid #3498db;
}
/* Child element */
.child {
/* Color usually inherits automatically, but we can be explicit */
color: inherit;
/* Borders do NOT inherit by default; this forces the child to have a blue border too */
border: inherit;
}
inherit looks at the immediate parent. If the parent doesn't have the property defined, it continues up the tree until it finds a value or hits the root element.
When to Use inherit
- Consistency in Nested Elements: Use it when you want a component (like a button inside a specific section) to perfectly match the typography or theme of its container.
- Resetting Browser Defaults: Some elements, like
<button>or<input>, have their own default browser styles for fonts and colors. Usinginheritforces them to match your page's design. - Maintainability: It reduces the need to hardcode the same hex codes or pixel values in multiple places (DRY - Don't Repeat Yourself).
inherit for form elements like buttons and inputs. Browsers often give these elements distinct fonts that don't match the rest of your site. Setting font-family: inherit; is a standard "CSS Reset" technique.
Practical Examples
Inheriting Font Properties in Buttons
By default, buttons often ignore the font family set on the <body>. Here is how to fix that:
/* Global Styles */
body {
font-family: 'Inter', sans-serif;
color: #444;
}
/* The button would normally use 'system-ui' or 'Arial' */
button {
font-family: inherit; /* Now it uses 'Inter' */
color: inherit; /* Now it uses #444 */
padding: 10px 20px;
}
Inheriting Visibility
The visibility property is unique because if a parent is hidden, the child is also hidden. However, using inherit explicitly can help in complex logic where you might want to toggle styles via classes.
/* Hide the parent element */
.hidden-container {
visibility: hidden;
}
/* The child follows the parent's lead */
.hidden-container .child-icon {
visibility: inherit;
}
visibility: hidden; still takes up space in the layout, unlike display: none;. If you use inherit with visibility, the hidden elements will still affect the flow of your page.
Combining inherit with Other Values
While inherit is often used on its own, you can use it within shorthand properties or to specifically override part of a complex style.
Example: Partial Inheritance
/* Parent element with a thick border */
.card {
border: 4px solid #e74c3c;
}
/* Child element inheriting only the width and style, but changing the color */
.card-badge {
border-width: inherit; /* 4px */
border-style: inherit; /* solid */
border-color: #2ecc71; /* Overrides red with green */
}
Caveats
- Non-Inheritable Properties: Properties like
margin,padding, andbackgrounddo not inherit by default for a reason applying them to every child usually breaks layouts. Only force inheritance for these if you have a specific architectural reason. - Performance: While negligible in small apps, excessive use of inheritance in deeply nested DOM trees requires the browser to do more "lookups" to calculate styles.
Alternative Keywords
If inherit isn't exactly what you need, CSS provides three other global keywords to control how properties behave:
- initial: Resets the property to its official CSS specification default (e.g.,
colorbecomes black,displaybecomesinline). - unset: A hybrid. It acts like
inheritif the property naturally inherits, and likeinitialif it doesn't. - revert: Rolls back the style to whatever the browser (user-agent) or the user's custom stylesheet would have applied.
Example with Alternative Keywords
.custom-box {
/* If the parent is blue, this becomes blue */
color: inherit;
/* Forces color to black (the CSS default) regardless of parent */
border-color: initial;
/* Removes all styling and goes back to how a browser draws a button */
all: revert;
}
Summary
The inherit keyword is an essential tool for creating modular and maintainable CSS. It allows you to bridge the gap between properties that inherit naturally and those that don't, ensuring your nested components stay in sync with their containers. By mastering inherit alongside initial and revert, you gain much finer control over the CSS cascade.