Inheritance in CSS

Inheritance in CSS means some property values pass from a parent element to its children. This is why setting a font or text color on the body can affect paragraphs, links, list items, and many nested elements. Inheritance helps CSS avoid repetition, but it can also cause confusion when a child receives a value you did not write directly on it.

Not every CSS property inherits. Text-related properties often inherit. Box-model and layout properties usually do not. Understanding which properties inherit helps you write cleaner CSS and debug unexpected styles faster.

Basic Inheritance Example

If a parent sets color, child text usually inherits that color unless another rule overrides it.

.article {
  color: #374151;
  font-family: Georgia, serif;
}

.article p {
  line-height: 1.7;
}

The paragraph inherits color and font family from .article. The line height is applied directly to the paragraph.

Common Inherited Properties

Properties related to text and typography commonly inherit. This makes it easy to set a consistent reading style on a container.

Inherited PropertyCommon Use
colortext color
font-familytypeface
font-sizetext size
font-weighttext weight
line-heightline spacing
text-aligntext alignment

This is why setting typography on body or a content wrapper is common.

Common Non-Inherited Properties

Layout and box properties usually do not inherit. If margin inherited, every nested element would accidentally receive parent spacing, which would be chaotic.

Non-Inherited PropertyReason
marginspacing should be element-specific
paddinginner spacing should be element-specific
borderborders should not automatically copy
backgroundbackgrounds should not automatically copy
widthlayout size should not automatically copy
displaylayout behavior should be explicit

The inherit Keyword

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

.child {
  border-color: inherit;
}

This can be useful when a child element should match the parent color, border color, or another shared value.

The initial Keyword

The initial keyword resets a property to its initial CSS value, not necessarily the browser default stylesheet value.

.box {
  color: initial;
}

For color, the initial value is typically black. This is not always the same as reverting to a user-agent style for a specific element.

The unset Keyword

The unset keyword behaves like inherit for inherited properties and like initial for non-inherited properties.

.component {
  color: unset;
  margin: unset;
}

Color inherits because it is normally inherited. Margin resets to its initial value because margin is not normally inherited.

The revert Keyword

The revert keyword rolls a property back to the value it would have had from a previous cascade origin. In practical terms, it can help restore browser or user stylesheet behavior.

button.custom-reset {
  all: revert;
}

This can be useful, but it should be tested carefully because revert interacts with the cascade and browser defaults.

The all Property

The all property applies a reset keyword to nearly every property at once. It can be powerful for isolated components but risky if used casually.

.widget {
  all: initial;
}

This removes many inherited and default styles. It can also remove useful behavior, so use it with a clear reason.

currentColor

The currentColor keyword represents the current computed text color. It is useful for borders, icons, SVG, and shadows that should match text color.

.link-card {
  color: #2563eb;
  border: 1px solid currentColor;
}

If the text color changes, the border changes with it. This is a clean way to connect visual parts to inherited color.

Font Inheritance in Form Controls

Some form controls may not inherit typography exactly as expected because browsers apply their own default styles. A common reset is to make form controls inherit font settings.

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

This makes form controls match the surrounding design more consistently.

Inheritance and Components

Components should decide when to inherit and when to define their own values. A card may inherit text color from the page, but a warning badge should probably define its own color.

.card {
  color: inherit;
}

.badge-warning {
  color: #92400e;
  background: #fef3c7;
}

Good components use inheritance intentionally. They do not accidentally depend on whatever parent happens to contain them.

Inheritance vs Cascade

Inheritance provides values from parents. The cascade decides which declarations win. A direct declaration on an element usually beats an inherited value from a parent.

.article {
  color: #6b7280;
}

.article strong {
  color: #111827;
}

The strong text receives a direct color, so it does not use the inherited article color.

Debugging Inheritance

Browser developer tools can show inherited styles separately from directly applied styles. If a value appears unexpectedly, inspect the element and check which parent supplies it.

  • Check whether the property normally inherits.
  • Check parent elements for typography and color values.
  • Check direct declarations on the element.
  • Check reset keywords such as inherit, initial, unset, and revert.
  • Check browser defaults on form controls.
  • Use currentColor for matching borders and icons.

Inheritance in Typography Systems

Typography systems rely heavily on inheritance. A site can set the base font family, color, and line height on body, then let most content inherit those values.

body {
  font-family: system-ui, sans-serif;
  color: #111827;
  line-height: 1.6;
}

h1,
h2,
h3 {
  line-height: 1.15;
}

The body creates the default reading style. Headings override only the line height because headings need tighter spacing. This avoids repeating the same font family on every element.

Inheritance and Links

Links are a common place where inheritance matters. A link may inherit font size and family from surrounding text but define its own color and decoration.

a {
  color: #2563eb;
  text-decoration-thickness: 0.08em;
}

.muted a {
  color: inherit;
}

The muted link inherits color from its parent, which can be useful in secondary text, cards, and metadata blocks. Use this intentionally so links remain recognizable.

Inheritance in SVG Icons

SVG icons often use inheritance through currentColor. This lets icons match nearby text without separate color rules.

.icon {
  width: 1em;
  height: 1em;
  fill: currentColor;
}

.button {
  color: white;
}

An icon inside the button becomes white because it follows the button text color. This is a clean pattern for icon systems.

Inheritance and Shadow DOM

In component systems and web components, inheritance can cross or stop at boundaries depending on how the component is built. Custom properties are often used to intentionally pass theme values into components because they inherit by default.

This is one reason custom properties are popular for theming. They provide controlled inheritance without exposing every internal style.

Preventing Accidental Inheritance

Sometimes a component should not inherit a parent style. A badge, alert, or code block may need its own color, font, or line height to remain readable in any context.

code {
  font-family: ui-monospace, monospace;
  color: #be123c;
}

The code element should not use the normal body font because monospace text communicates code. Direct declarations are useful when a component needs independence.

Inheritance Checklist

  • Use inheritance for broad text styling.
  • Define component-specific values when context should not control them.
  • Use currentColor for icons and borders that should follow text.
  • Use inherit intentionally, not as a guess.
  • Be careful with all: initial or all: unset.
  • Inspect inherited values in DevTools.

Inherited Custom Properties

Custom properties inherit by default, which makes them useful for passing design values through a component tree. A parent can set an accent color, and children can use it without receiving extra classes.

.profile-card {
  --accent: #2563eb;
}

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

The link does not inherit the color property directly. It reads an inherited custom property and uses that value. This gives component authors more control than relying only on normal inherited properties.

Inheritance and Resets

Resets often use inheritance to make elements behave consistently. Form controls are a common example because browsers apply platform-specific font styles.

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

This makes form elements blend with the surrounding interface. It is a deliberate use of inheritance, not a browser default assumption.

Inherited Does Not Mean Weak

Inherited values can still shape the entire design. A single body color, font family, or line height can affect thousands of elements. Treat inherited values as major design decisions, not background details.

Designing Predictable Inheritance

Good CSS uses inheritance deliberately. Global document styles should define the normal reading experience, such as font family, text color, and line height. Components should then decide which values they want to accept from the page and which values they must control themselves.

For example, an article body can inherit the site typography because it is long-form text. A code block, alert box, navigation menu, or button may need direct declarations because it has a specific role. This separation prevents components from looking broken when they are placed inside a dark section, a small card, or a differently styled sidebar.

The safest approach is to let content inherit broad text rules, but make interface components explicit where readability, spacing, or branding matters.

This balance keeps the stylesheet clean. You get the benefit of global consistency without making every component fragile or dependent on the exact parent where it appears.

That is the practical goal of inheritance.

Common Inheritance Mistakes

  • Expecting margin or padding to inherit.
  • Forgetting child text inherits parent color.
  • Using all: initial and removing useful defaults.
  • Confusing initial with browser default styling.
  • Not resetting form controls to inherit font.
  • Building components that accidentally depend on parent styles.

Inheritance in CSS FAQ

What is inheritance in CSS?

Inheritance is when some CSS property values pass from parent elements to child elements.

Do all CSS properties inherit?

No. Text-related properties often inherit, but layout and box-model properties usually do not.

What does inherit do in CSS?

The inherit keyword forces a property to use the computed value from its parent.

What is currentColor?

currentColor represents the current computed text color and can be used for borders, icons, and related styling.


Continue learning CSS in order
Follow the topic sequence with the previous and next lesson.