← Back to CSS Course | Chapter 1: Introduction & Basics | Lesson 7 of 7

CSS Inheritance

Think of inheritance like a family trait passed down the generations: a parent's eye color shows up in the children automatically, no one has to redeclare it. But a parent's house isn't automatically owned by the children -- some things pass down for free, others don't. CSS inheritance works the same way. Some properties flow down from parent to child elements without being restated, others stop dead at the parent.
Syntax
css
selector {
  property: inherit;
  property: initial;
  property: unset;
}

Which Properties Inherit by Default

Text-related properties like color, font-family, font-size, and line-height inherit by default -- set them on a parent and every nested element picks them up unless it overrides them. This is why setting a global font on body works without repeating it on every paragraph and span.

Note:
  • Accepted values:
  • inherit — takes the parent's computed value
  • initial — resets to the spec's default
  • unset — inherits if inheritable, else initial
  • revert — rolls back to the browser's default style
  • revert-layer — rolls back to the previous cascade layer

Example: Which Properties Inherit by Default

css
<style>
body {
  color: navy;
  font-family: sans-serif;
}
</style>
<body>
  <p>Inherits navy color and the font-family without repeating them</p>
</body>

Which Properties Don't Inherit

Box-model properties like border, margin, padding, width, and background do NOT inherit -- each element starts fresh with its own default box, otherwise every nested div would accidentally inherit its parent's border and padding.

This default exists specifically to keep layout properties predictable per element.

Example: Which Properties Don't Inherit

css
<style>
.parent {
  border: 2px solid black;
  padding: 20px;
  background: lightyellow;
}
</style>
<div class="parent">
  <div>Child has no border or padding of its own</div>
</div>

The inherit Keyword

Any property, even ones that don't inherit by default, can be forced to inherit its parent's computed value using the inherit keyword. This is useful when you want a child to deliberately match its container instead of falling back to the property's normal initial value.

Example: The inherit Keyword

css
<style>
.parent {
  border-color: blue;
  background: lightyellow;
  padding: 10px;
}
.child {
  border: 2px solid black;
  border-color: inherit;
  padding: 8px;
  background: white;
}
</style>
<div class="parent">
  <div class="child">Border forced to match the parent's border-color</div>
</div>

The initial Keyword

The initial keyword resets a property to its default value as defined by the CSS specification, ignoring both inheritance and any stylesheet rules. This is handy for undoing an inherited or cascaded value back to the browser's baseline.

Example: The initial Keyword

css
<style>
p {
  color: red;
}
.reset {
  color: initial;
}
</style>
<p class="reset">Reset back to the browser's default text color</p>

The unset Keyword

The unset keyword is context-sensitive: on a property that normally inherits, it behaves like inherit; on a property that doesn't inherit, it behaves like initial. It's a convenient single keyword for resetting a property without needing to know its inheritance behavior.

Example: The unset Keyword

css
<style>
.parent {
  color: navy;
}
.child-color {
  color: unset;
}
.child-border {
  border: 2px solid black;
  border-color: unset;
  background: lightyellow;
  padding: 8px;
}
</style>
<div class="parent">
  <p class="child-color">unset acts like inherit here</p>
  <div class="child-border">unset acts like initial here</div>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting every property to inherit, when box properties such as margin, padding and border do not inherit by default.
  2. Setting color on a parent and being surprised that a link inside it stays blue, since the browser's own link style targets a directly.
  3. Confusing inherit with initial, since inherit uses the parent's value and initial uses the property's default.
Chapter Summary
  • CSS styles HTML, and its syntax and selectors choose which elements to style.
  • CSS can be added in different ways and documented with comments.
  • Errors and inheritance explain why styles apply or fail to apply.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.