CSS Inheritance & Cascade

Understand how CSS properties are inherited and how the cascade determines which styles apply.

What is Inheritance in CSS?

Inheritance in CSS is the mechanism by which some property values applied to a parent element are passed down to its children. Not all CSS properties are inherited. For example, color and font-family are inherited, but margin and padding are not.

You can force inheritance using the inherit keyword, or prevent it with initial or unset.

Interactive Example: Inheritance

/* Parent sets color and font-family */
.parent {
  color: #3498db;
  font-family: 'Segoe UI', Arial, sans-serif;
}
.child {
  font-size: 1.2em;
}
Parent (color and font-family set)
Child (inherits color and font-family)

What is the Cascade in CSS?

The cascade is the algorithm that determines which CSS rule applies when multiple rules could apply to the same element. It is based on three main factors:

  1. Importance (e.g., !important declarations)
  2. Specificity (how specific the selector is)
  3. Source Order (which rule comes last in the CSS)

The browser evaluates these factors in order to resolve conflicts and apply the correct style.

Interactive Example: Cascade & Specificity

/* Multiple rules for the same element */
.box {
  background: #f0f4ff;
  color: #222;
}
#special {
  color: #e74c3c;
}
.box {
  color: #3498db !important;
}
This box demonstrates the cascade and specificity.

Try to predict which color will apply and why. Hover for explanation. The color will be blue (#3498db) because the '!important' rule has the highest priority, overriding both the ID selector and the other class rule.

Best Practices & Tips

Advanced: initial, inherit, unset, revert

CSS provides four special keywords to control property values and inheritance:

/* Example: Using initial, inherit, unset, revert */
.parent-demo {
  color: #e67e22;
  font-weight: bold;
}
.child-inherit {
  color: inherit;
}
.child-initial {
  color: initial;
}
.child-unset {
  color: unset;
}
.child-revert {
  color: revert;
}
Parent (color: orange, font-weight: bold)
Child (color: inherit)
Child (color: initial)
Child (color: unset)
Child (color: revert)

Try it: Hover over each child to see a tooltip explaining the result!

Further Reading