CSS Inheritance

What You'll Learn

  • What CSS inheritance is and why it matters
  • Which properties inherit by default and which don't
  • How to control inheritance with special keywords
  • The difference between inherit, initial, unset, and revert
  • Practical applications of inheritance in real-world CSS

Introduction to Inheritance

Inheritance is one of CSS's most powerful features. It's the mechanism by which certain CSS properties flow from parent elements down to their children and descendants. This creates a cascade of styles that makes writing CSS more efficient and maintainable.

Understanding inheritance helps you write less CSS and avoid repetition. Instead of styling every element individually, you can set properties on parent elements and let children inherit them automatically.

Inherited Properties

Certain CSS properties are inherited by default. These are primarily text-related properties that make sense to pass down from parent to child.

Common Inherited Properties

  • Text: color, font-family, font-size, font-style, font-weight, letter-spacing, line-height, text-align, text-indent, text-transform, white-space, word-spacing
  • Lists: list-style, list-style-type, list-style-position, list-style-image
  • Other: cursor, visibility, quotes

Live Example

In this demo, the parent container sets text properties that automatically flow down to all children and descendants:

.parent-container { color: #0066cc; font-family: Georgia, serif; font-size: 1.125rem; line-height: 1.8; } /* Children automatically inherit these properties! */

Notice how the nested paragraph and span elements inherit all the text properties from their parent, even though we didn't explicitly style them.

Non-Inherited Properties

Not all properties inherit. Properties that don't make logical sense to pass down to children—like box model properties—don't inherit by default.

Properties That Don't Inherit

  • Box Model: margin, padding, border, width, height
  • Background: background, background-color, background-image
  • Position: position, top, right, bottom, left, z-index
  • Layout: display, float, clear, overflow

Why Some Properties Don't Inherit

Imagine if border inherited—every nested element would get a border, creating visual chaos! Non-inherited properties need to be set explicitly on each element that needs them.

.parent-container { border: 4px solid #dc3545; padding: 1.5rem; background-color: #f8d7da; } /* Children do NOT inherit border, padding, or background */

The 'inherit' Keyword

You can force any property to inherit from its parent using the inherit keyword, even properties that don't normally inherit.

Syntax

.parent { background-color: lightblue; border: 3px solid #0066cc; } .child { background-color: inherit; /* Now inherits parent's background */ border: inherit; /* Now inherits parent's border */ }

Use Cases

The inherit keyword is useful when you want children to explicitly match their parent's styling for properties that don't normally inherit.

The 'initial' Keyword

The initial keyword resets a property to its CSS specification default value, ignoring any inherited or previously set values.

How It Works

initial sets the property to the value defined in the CSS specification, not the browser's default stylesheet. For most properties, this means removing any styling.

.parent { color: red; font-size: 1.25rem; } .child { color: initial; /* Resets to CSS default (usually black) */ font-size: initial; /* Resets to CSS default (medium) */ }

The 'unset' Keyword

The unset keyword is a smart keyword that acts like inherit for inheritable properties and initial for non-inheritable properties.

Dual Behavior

unset asks: "Is this property inheritable?" If yes, it inherits from the parent. If no, it resets to initial.

.parent { color: purple; border: 3px solid purple; } .child { color: unset; /* Inherits purple (color is inheritable) */ border: unset; /* Resets to none (border is not inheritable) */ }

The 'revert' Keyword

The revert keyword rolls back to the browser's default stylesheet (user agent styles), ignoring your custom CSS but preserving browser defaults.

How revert Differs

  • initial uses CSS spec defaults
  • revert uses browser stylesheet defaults
  • inherit uses parent's value
h2 { font-size: 0.875rem; /* Custom override */ margin: 0.25rem 0; } h2.reset { font-size: revert; /* Back to browser default (~1.5em) */ margin: revert; /* Back to browser default */ }

Inheritance Quick Reference

Here's a summary of all the inheritance control keywords:

Keyword Comparison

/* Four ways to control inheritance */ .element { color: inherit; /* Use parent's value */ } .element { color: initial; /* Use CSS specification default */ } .element { color: unset; /* inherit if inheritable, else initial */ } .element { color: revert; /* Use browser default stylesheet value */ }

Decision Guide

  • Want parent's value? Use inherit
  • Want to remove all styling? Use initial
  • Want natural behavior? Use unset
  • Want browser defaults? Use revert

Practical Applications

Let's look at some real-world scenarios where understanding inheritance helps you write better CSS.

Setting Global Typography

/* Set once on body, inherits everywhere */ body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; font-size: 16px; line-height: 1.5; color: #333; }

Resetting Form Elements

/* Form elements don't inherit font properties by default */ button, input, select, textarea { font-family: inherit; font-size: inherit; line-height: inherit; }

Theme Components

/* Parent sets theme, children inherit */ .card { color: #333; background: white; border: 1px solid #ddd; } .card.dark-theme { color: #f0f0f0; background: #1a1a1a; border-color: #333; } /* Children automatically adapt to theme */ .card p { color: inherit; /* Matches theme color */ }

Key Takeaways