Skip to content
Back to Concepts
CSS UniverseCurriculumCascade & Inheritance
1st Year CS & Web Engineering FoundationCore Concept 02

Cascade & Inheritance

Why is it called Cascading Style Sheets? Understand how values flow like water down the DOM tree, how inheritance works, and how the browser resolves property clashes.

1. The Mental Model

Genetic DNA vs The Waterfall Appeal

CSS blends two foundational mechanisms to calculate the final style of every HTML element:

1. Inheritance (DNA Passing)

Just like children inherit their parents' hair color and eye color, elements inherit typographic properties like color, font-family, and line-height from their parent containers.

What does NOT inherit:

You do NOT inherit your parent's shoes or jacket size! Similarly, geometry properties (margin, padding, border, background) NEVER inherit automatically.

2. The Cascade (Court of Appeals)

When multiple rules target the exact same element with conflicting values (e.g. color: red vs color: blue), the Cascade algorithm acts as judge:

  1. Origin & Importance: Browser default vs User vs Author vs !important
  2. Specificity: (A, B, C, D) score vector
  3. Source Order: The last rule in the stylesheet breaks ties!
2. Inheritance Property Matrix

What Inherits Automatically (And What Doesn't)

✅ Inherited Properties (Typographic)

Flows down naturally
  • color — Text foreground color
  • font-family, font-size, font-weight — Typography attributes
  • line-height, letter-spacing — Text pacing
  • text-align, text-transform — Alignment & casing
  • visibility — Hidden but takes layout space
  • list-style — Bullet points and numbers

❌ Non-Inherited Properties (Box Geometry)

Stops at parent
  • margin, padding, border — Box model dimensions
  • width, height, max-width — Element dimensions
  • background-color, background-image — Visual surface
  • display, position, top, left — Layout positioning
  • overflow, z-index — Containment & layering
  • transform, opacity — Graphic rendering
3. The Universal Value Keywords

`inherit`, `initial`, `revert`, and `unset`

Every single CSS property accepts these four universal keywords to manually control the cascade:

inherit

Forces the child element to take the exact computed value of its direct parent, even for non-inherited properties!

button { font-family: inherit; }
initial

Resets property to the official W3C specification initial default (e.g. color: #000, display: inline).

p { color: initial; }
revert

Rolls back the property to the browser's User-Agent stylesheet default (e.g. restoring link underlines).

a.reset { color: revert; }
unset

A hybrid: acts like inherit if the property naturally inherits, or like initial if it doesn't!

all: unset; /* Complete reset */
4. Live Inheritance Simulator

Watch DNA Flow Down the DOM Tree

Adjust the parent properties below. Notice how color and font-size naturally flow to child elements, while padding stays strictly on the parent!

<div class="parent-box">

I am parent text. My color is #6366f1 and font-size is 18px.

<div class="child-node">

I am a nested child paragraph!

Active text color: #6366f1 (inherited from parent)
5. The Cascade Battle Arena

Which Value Cascades Through?

Round 1 of 3
Round 1: Inheritance vs Direct Targeting

The container has color: blue. A paragraph inside has no color specified, but a global "p" rule says color: red. What color is the text?

<div class="card" style="color: blue;">
  <p>Hello CSS World</p>
</div>

/* In stylesheet: */
p { color: red; }
Contestant Rules:
6. Common Traps & Pro-Tips

3 Big Cascade Traps for Beginners

Trap 1: The Button Font Trap

Why don't buttons have my body font?

HTML form controls (<button>, <input>) have User-Agent defaults that stop font inheritance!
Fix: Add button, input { font-family: inherit; } to your base reset.

Trap 2: `initial` vs `revert`

`display: initial` makes divs inline!

The W3C initial spec for display is inline. If you use initial on a div, it collapses into an inline element!
Fix: Use revert to get back the browser's block default.

Best Practice: Source Order

Write Base First, Overrides Last

When specificity ties occur, CSS resolves ties by source order. Always load your base resets first, component styles second, and utility override modifiers at the very bottom!

Next Concept in Curriculum

3. Pseudo-Classes & Elements

Dynamic UI states (:hover, :focus-visible, :nth-child) and virtual sub-elements (::before, ::after).

Continue to Pseudo-Classes