Skip to content

CSS Inheritance

CSS inheritance allows some property values to pass from parent elements to child elements. Understanding inheritance helps you write cleaner CSS, reduce repeated styles, debug unexpected values, and build consistent typography, color, and design systems.

What Is CSS Inheritance?

CSS inheritance means that certain CSS property values applied to a parent element are automatically passed down to its child elements. This is why setting text color or font on the body can affect most text on the page.

body {
  color: #212529;
  font-family: system-ui, sans-serif;
}

In this example, many child elements inside the page inherit the text color and font family from the body.

Why CSS Inheritance Is Important

  • Reduces repeated CSS declarations.
  • Makes global typography and color easier to manage.
  • Helps create consistent page styling.
  • Supports scalable design systems and themes.
  • Explains why child elements sometimes receive unexpected styles.
  • Works together with cascade and specificity.
  • Improves maintainability in large stylesheets.

CSS Inheritance Cheatsheet

The following table explains common inheritance concepts and keywords.

Concept / Keyword Example Purpose
Inherited property color, font-family Property naturally passes from parent to child.
Non-inherited property margin, padding Property does not naturally pass to children.
inherit border-color: inherit; Forces a property to use the parent value.
initial color: initial; Resets property to its initial CSS value.
unset color: unset; Acts like inherit for inherited properties and initial for non-inherited properties.
revert font-size: revert; Rolls back to the value from browser/user styles or previous cascade origin.
all all: unset; Applies reset behavior to almost all properties.

Common Inherited CSS Properties

Text-related properties are usually inherited because children often need the same text appearance as their parent.

Property What It Controls
color Text color.
font-family Font family.
font-size Text size.
font-weight Text thickness.
line-height Line spacing.
text-align Text alignment.
visibility Visibility state.
cursor Mouse cursor style.

Common Non-Inherited CSS Properties

Layout and box model properties are usually not inherited because child elements should control their own spacing, size, and layout.

Property Why It Usually Does Not Inherit
margin Children should not automatically receive parent outside spacing.
padding Children should not automatically receive parent inside spacing.
border Children usually should not duplicate parent borders.
width Children need their own sizing behavior.
height Children need independent height behavior.
display Children should not automatically become the same layout type.
position Children usually need separate positioning context.
background-color Children often remain transparent instead of copying parent background.

The CSS inherit Keyword

The inherit keyword forces a property to take the computed value from its parent, even if that property does not normally inherit.

.card {
  border-color: #0d6efd;
}

.card button {
  border-color: inherit;
}

Here, the button border color inherits from the card border color.

The CSS initial Keyword

The initial keyword resets a property to its initial CSS value.

.message {
  color: initial;
  font-weight: initial;
}

Use initial when you want to ignore inherited or previously applied values and return to the property's default initial value.

The CSS unset Keyword

The unset keyword behaves differently depending on whether the property naturally inherits.

  • For inherited properties, unset acts like inherit.
  • For non-inherited properties, unset acts like initial.
.component {
  color: unset;
  margin: unset;
}

In this example, color may inherit, while margin resets to its initial value.

The CSS revert Keyword

The revert keyword rolls a property back to the value it would have had from the browser stylesheet, user stylesheet, or previous cascade origin.

button.custom-reset {
  all: revert;
}

revert is useful when you want to undo author styles and restore more natural browser behavior.

The CSS all Property

The all shorthand applies a reset keyword to almost every CSS property.

.unstyled-button {
  all: unset;
  cursor: pointer;
}

Use all carefully because it can remove important default styles, including focus, display, and accessibility-related visual behavior.

CSS Inheritance vs Specificity

Inherited values are weaker than directly applied styles. A direct rule on an element usually beats a value inherited from its parent.

body {
  color: black;
}

p {
  color: blue;
}

The paragraph becomes blue because p is directly targeted.

CSS Inheritance vs Cascade

The cascade decides which CSS rule applies. Inheritance decides whether a child receives a value from its parent when no direct value is set.

.article {
  color: #212529;
}

.article p {
  color: #0d6efd;
}

Paragraphs inside .article use the direct paragraph rule instead of only inheriting the article color.

Parent and Child Inheritance Example

CSS inheritance is useful for setting typography once on a parent container.

.article-content {
  color: #212529;
  font-family: Georgia, serif;
  line-height: 1.7;
}

.article-content h2 {
  font-family: system-ui, sans-serif;
}

Paragraphs inherit the article typography, while headings can override selected properties.

Form Elements and Inheritance

Some form controls do not always inherit typography exactly as expected. You may want to explicitly set form elements to inherit font styles.

button,
input,
textarea,
select {
  font: inherit;
}

This keeps form controls visually consistent with the surrounding content.

CSS Variables and Inheritance

CSS variables inherit by default. This makes them powerful for themes and component styling.

:root {
  --brand-color: #0d6efd;
}

.card {
  --brand-color: #6610f2;
  border-color: var(--brand-color);
}

.card a {
  color: var(--brand-color);
}

The link inside the card uses the scoped variable value from the card.

CSS Inheritance and Accessibility

  • Set readable global typography on the body.
  • Use inherited text color carefully to maintain contrast.
  • Do not remove focus styles with broad reset rules.
  • Use font: inherit; on form controls for consistent readable forms.
  • Test theme inheritance in light mode and dark mode.
  • Be careful with all: unset; because it can remove important default behavior.

CSS Inheritance and SEO

CSS inheritance does not directly affect rankings, but clean inherited styles improve readability, accessibility, responsive behavior, and long-term maintainability.

  • Consistent typography improves content readability.
  • Readable inherited colors support better user experience.
  • Maintainable CSS makes page updates easier.
  • Accessible styling supports more users.
  • Cleaner stylesheets help reduce layout and design bugs.

Common CSS Inheritance Mistakes

  • Expecting margin, padding, border, or background to inherit automatically.
  • Forgetting that directly targeted styles override inherited values.
  • Using all: unset; and accidentally removing useful defaults.
  • Not setting form controls to inherit font styles.
  • Using inherited colors without checking contrast.
  • Forgetting that CSS variables inherit by default.
  • Confusing inheritance with specificity or cascade order.
  • Removing link styles and making links hard to identify.

CSS Inheritance Best Practices

  • Set global font family, font size, line height, and color on the body.
  • Use inheritance for typography and color consistency.
  • Use inherit intentionally for links, buttons, and form controls.
  • Use font: inherit; for form controls when building custom UI.
  • Avoid broad resets that remove important accessibility styles.
  • Use CSS variables to inherit theme and component values.
  • Use direct selectors when a child element needs a different style.
  • Test inherited styles across components, themes, and responsive layouts.

Key Takeaways

  • CSS inheritance passes some parent property values to child elements.
  • Text-related properties like color and font-family usually inherit.
  • Box model properties like margin, padding, and border usually do not inherit.
  • inherit forces a property to use the parent value.
  • initial, unset, and revert help reset values.
  • Directly applied styles usually beat inherited styles.
  • Good inheritance usage creates cleaner, more maintainable CSS.

Pro Tip

Use inheritance for typography and colors, but do not expect layout properties like margin, padding, border, or display to inherit automatically.