What Is CSS Inheritance and Which Properties Are Inherited?

Learn What Is CSS Inheritance and Which Properties Are Inherited? with practical examples, explanations, best practices, and real-world coding techniques.

Try Editor

CSS inheritance is the mechanism that allows certain CSS property values to pass from a parent element to its child elements automatically. Instead of defining the same value on every element, you can set a property on a parent and let compatible child elements inherit that value.

Inheritance is one of the reasons CSS can style an entire section of a page with only a few rules. For example, setting the color property on the body can establish a default text color that many descendants inherit unless they define their own value.

CSS Inheritance in One Example

body {
  color: #334155;
  font-family: Arial, sans-serif;
}

Many descendant elements can inherit these values automatically when they do not specify their own values.

What Is CSS Inheritance?

CSS inheritance means that an element can receive the computed value of an inherited property from its parent when the element does not specify that property itself.

Consider this HTML:

<div class="parent">
  <p>
    Hello CSS
  </p>
</div>

And this CSS:

.parent {
  color: blue;
}

The paragraph can inherit the blue text color from its parent because color is an inherited property. You do not need to write another color rule for the paragraph unless you want a different value.

Parent and Child Elements

To understand inheritance, you need to understand the relationship between parent and child elements. HTML creates a document tree in which elements can contain other elements.

body
↓
div
↓
section
↓
p

If a CSS property is inheritable, a value defined on the parent can become the inherited value for the child. This behavior can continue through multiple generations unless a descendant overrides the property.

Which CSS Properties Are Inherited?

Not every CSS property inherits by default. Many properties related to text and typography are commonly inherited, while most box-model and layout properties are not.

Commonly Inherited

  • color
  • font-family
  • font-size
  • font-style
  • font-weight
  • line-height
  • text-align
  • visibility

Commonly Not Inherited

  • margin
  • padding
  • border
  • width
  • height
  • display
  • position
  • background-color

The exact inheritance behavior belongs to each CSS property's definition. Therefore, do not assume that a property inherits simply because it appears visually related to text or layout.

Example: Color Inheritance

The color property is a simple way to understand inheritance.

<div class="container">
  <h2>CSS Inheritance</h2>
  <p>This text inherits the color.</p>
</div>
.container {
  color: #2563eb;
}

Both the heading and paragraph can inherit the text color from the container if they do not define a different color.

Overriding an Inherited Value

A child element can specify its own value and stop inheriting that property value from its parent.

.container {
  color: blue;
}

.container p {
  color: orange;
}

Here, the parent provides blue as the inherited color, but the paragraph explicitly specifies orange. Therefore, the paragraph uses orange instead of the inherited blue value.

Inheritance vs Cascade

Inheritance and the CSS cascade are related but different concepts.

Inheritance Cascade
Transfers values from ancestor to descendant for inheritable properties. Determines which competing declarations have precedence.
Occurs when an element does not have a specified value for an inherited property. Considers factors such as origin, importance, layers, specificity, scope, and source order.
Focuses on parent-child relationships. Focuses on resolving competing declarations.

The inherit Keyword

CSS provides the special keyword inherit for explicitly telling an element to use the inherited value of a property. This works even for properties that do not normally inherit by default.

.parent {
  border: 2px solid orange;
}

.child {
  border: inherit;
}

The child explicitly receives the inherited value of border from its parent. This is different from relying on the property's default inheritance behavior.

The initial Keyword

The initial keyword resets a property to its initial value as defined by CSS. It does not mean "copy the parent's value."

body {
  color: blue;
}

p {
  color: initial;
}

The paragraph no longer uses the parent's inherited blue value for color. Instead, the property is reset to its defined initial value.

The unset Keyword

The unset keyword behaves differently depending on whether the property naturally inherits. For an inherited property, unset behaves like inherit. For a non-inherited property, it behaves like initial.

p {
  color: unset;
  margin: unset;
}

Because color is inherited, color: unset allows the inherited value. Because margin is normally not inherited, margin: unset resets it to its initial value.

The revert Keyword

The revert keyword rolls a property back to the value from an earlier cascade origin. It is especially useful when overriding styles and wanting to restore behavior that would have applied from another origin or earlier level of the cascade.

button {
  all: revert;
}

The meaning of revert is different from both initial and inherit. These CSS-wide keywords solve different problems and should not be treated as interchangeable reset mechanisms.

CSS-Wide Keyword Comparison

Keyword Meaning
inherit Use the property's inherited value from the parent.
initial Use the property's initial value.
unset Act like inherit for inherited properties and initial for non-inherited properties.
revert Roll back the property's declaration to an earlier cascade origin.

Does Margin Inherit in CSS?

No. margin is not normally inherited. If you set a margin on a parent, its child does not automatically receive that same margin value.

.parent {
  margin: 40px;
}

The child does not automatically get margin: 40px. This is an important distinction between inheritance and simply observing that descendants are visually affected by a parent.

Does Padding Inherit in CSS?

No. padding does not normally inherit either. Setting padding on a parent creates spacing inside that parent rather than passing the same padding declaration to every child.

.card {
  padding: 24px;
}

The child can still appear affected because its content is physically located inside the parent's padded area, but that does not mean the child inherited the padding property.

Why Does Text Usually Inherit?

Inherited typography makes stylesheet authoring much easier. A parent can establish a common text environment and descendants can use it automatically unless a more specific rule changes the value.

body {
  font-family: "Segoe UI", sans-serif;
  color: #1f2937;
  line-height: 1.6;
}

.content {
  font-size: 1rem;
}

This creates a useful baseline for large sections of a website without repeating the same declarations on every text element.

Inheritance Through Multiple Levels

An inherited property can travel through multiple levels of the document tree.

<div class="level-one">
  <section>
    <article>
      <p>Inherited text</p>
    </article>
  </section>
</div>
.level-one {
  color: purple;
}

The paragraph can inherit the purple color through the intermediate elements as long as those elements do not override the property.

Inheritance with Classes

Classes do not change the concept of inheritance. A class can set a property on an element, and descendants can inherit the value when the property is inheritable.

.theme-dark {
  color: white;
  font-family: Arial, sans-serif;
}

Any appropriate descendants inside an element with theme-dark can inherit these properties automatically. This makes inheritance useful for component and theme design.

Inheritance and CSS Variables

CSS custom properties are inherited by default. This makes them especially useful for themes and design tokens.

:root {
  --primary-color: #f97316;
}

.card {
  color: var(--primary-color);
}

A custom property defined on an ancestor can be available to descendants through inheritance. A nested component can also override the custom property for itself and its descendants.

:root {
  --primary-color: #f97316;
}

.card {
  --primary-color: #2563eb;
}

.card .button {
  background-color: var(--primary-color);
}

The button can use the value inherited from the nearest ancestor that defines the custom property.

The all Property

The all shorthand can reset almost all CSS properties on an element at once. It accepts CSS-wide keywords such as initial, inherit, unset, and revert.

.component {
  all: unset;
}

This can be useful for isolated components, but it should be used carefully. Resetting a large number of properties can also remove styles that were intentionally inherited or supplied by the surrounding context.

Inheritance and Form Controls

Form controls are a common source of confusion because browser defaults and user-agent styling can make elements appear visually different from surrounding text. Explicitly inheriting properties such as fonts can sometimes produce more consistent form typography.

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

This tells these controls to use the inherited font shorthand value rather than relying solely on their own browser defaults.

Inheritance Does Not Mean Everything Is Copied

A common beginner mistake is thinking that when a parent has a style, every child receives the same CSS declarations. That is not how inheritance works.

Only properties defined as inheriting can obtain their value from the parent automatically. Other properties can still affect the layout or appearance of descendants without being inherited.

Example

.parent {
  padding: 30px;
  color: blue;
}

The child can inherit the blue color, but it does not inherit the parent's padding property.

Common CSS Inheritance Mistakes

Assuming Every Property Inherits

Properties such as margin, padding, width, and display do not normally inherit. Always check the property's inheritance behavior.

Confusing Inheritance with Layout

A child can be visually affected by a parent's padding or layout without inheriting those properties.

Confusing Inheritance with Specificity

A child rule and an inherited parent value are not the same kind of competition as two declarations directly targeting the same element.

Using !important to Fix Everything

Many inheritance problems are better solved by explicitly setting the required property or using the correct CSS-wide keyword.

Resetting Too Much CSS

Broad resets such as all: unset can also remove useful inherited and default behavior.

How to Debug CSS Inheritance

Browser DevTools are extremely useful when you are unsure where a value came from. Inspect the element and examine the Styles and Computed panels. Look for properties whose values are inherited from an ancestor.

Inspect Element
↓
Check Computed Styles
↓
Find the Property
↓
Check Whether It Is Inherited
↓
Inspect Parent Rules
↓
Check Child Overrides

CSS Inheritance Best Practices

  • Use inherited typography to establish sensible defaults at high-level containers.
  • Do not repeat inherited values unnecessarily on every child element.
  • Use explicit child rules when a component intentionally needs a different value.
  • Use inherit when you want to explicitly copy the parent's value for a property.
  • Use initial, unset, or revert when you specifically need reset behavior.
  • Remember that CSS custom properties inherit by default.
  • Inspect computed styles when an inherited value is unexpected.
  • Avoid broad resets unless you understand which styles and inherited values they will remove.
  • Keep theme-related properties at an appropriate ancestor level so descendants can inherit them naturally.

Practical Theme Example

Inheritance becomes especially useful when creating a theme system. Instead of assigning the same text color and font to every component, establish the common values on a root container.

<main class="app-theme">

  <section class="card">
    <h2>Dashboard</h2>
    <p>Welcome back.</p>
  </section>

  <section class="card">
    <h2>Statistics</h2>
    <p>Your latest activity.</p>
  </section>

</main>
.app-theme {
  color: #1f2937;
  font-family: "Segoe UI", sans-serif;
  line-height: 1.6;
}

.card {
  padding: 24px;
  border: 1px solid #d1d5db;
  border-radius: 12px;
}

.card h2 {
  margin-bottom: 8px;
}

.card p {
  color: #64748b;
}

The common typography is inherited from .app-theme, while the paragraph explicitly overrides the inherited color. This keeps the stylesheet concise while still allowing component-specific customization.

Key Takeaways

CSS inheritance allows certain property values to flow from parent elements to descendants.

Common inherited properties include color, font-related properties, line-height, and some text-related properties.

Properties such as margin, padding, width, height, and display do not normally inherit.

A child can override an inherited value by declaring the property itself.

inherit, initial, unset, and revert are different CSS-wide keywords with different purposes.

CSS inheritance is different from the cascade: inheritance transfers values through parent-child relationships, while the cascade resolves competing declarations.

CSS custom properties inherit by default, making them especially useful for theme and design-token systems.

Browser DevTools can help identify whether a value comes from the current element or is inherited from an ancestor.

Code Examples

html
<!DOCTYPE html>
<html>
<head>
  <style>

    .container {
      color: #2563eb;
      font-family: Arial, sans-serif;
    }

  </style>
</head>

<body>

  <div class="container">
    <h2>CSS Inheritance</h2>

    <p>
      This paragraph inherits the
      color and font from its parent.
    </p>
  </div>

</body>
</html>
    
css
/* Override an inherited value */

.parent {
  color: blue;
}

.parent p {
  color: orange;
}

/*
Parent:
blue

Paragraph:
orange
*/
    
css
/* Explicitly inherit a
   non-inherited property */

.parent {
  border: 2px solid orange;
}

.child {
  border: inherit;
}

/*
The child explicitly receives
the parent's border value.
*/
    
css
/* CSS-wide keywords */

.parent {
  color: blue;
  margin: 30px;
}

.child {
  color: inherit;
  margin: initial;
}

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

.revert-example {
  all: revert;
}
    
css
/* CSS custom properties
   are inherited by default */

:root {
  --primary-color: #f97316;
  --spacing: 16px;
}

.card {
  --primary-color: #2563eb;

  padding: var(--spacing);
  color: var(--primary-color);
}

.card .button {
  background-color: var(--primary-color);
}
    
html
<!DOCTYPE html>
<html>
<head>
  <style>

    .app-theme {
      color: #1f2937;
      font-family: Arial, sans-serif;
      line-height: 1.6;
    }

    .card {
      border: 1px solid #d1d5db;
      padding: 20px;
    }

    .card p {
      color: #64748b;
    }

  </style>
</head>

<body>

  <main class="app-theme">

    <section class="card">
      <h2>Dashboard</h2>
      <p>
        The paragraph inherits the font
        and line-height but overrides color.
      </p>
    </section>

  </main>

</body>
</html>
    

Pro Tip

Think of inheritance as a default communication channel from parent to child. Use it for values that naturally describe a shared context, such as typography, text color, and theme variables, instead of repeating the same declarations throughout your components. When you need a different behavior, explicitly override the property rather than fighting the cascade with increasingly specific selectors.

More CSS lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.