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.
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.
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:
- Origin & Importance: Browser default vs User vs Author vs
!important - Specificity:
(A, B, C, D)score vector - Source Order: The last rule in the stylesheet breaks ties!
What Inherits Automatically (And What Doesn't)
✅ Inherited Properties (Typographic)
Flows down naturallycolor— Text foreground colorfont-family, font-size, font-weight— Typography attributesline-height, letter-spacing— Text pacingtext-align, text-transform— Alignment & casingvisibility— Hidden but takes layout spacelist-style— Bullet points and numbers
❌ Non-Inherited Properties (Box Geometry)
Stops at parentmargin, padding, border— Box model dimensionswidth, height, max-width— Element dimensionsbackground-color, background-image— Visual surfacedisplay, position, top, left— Layout positioningoverflow, z-index— Containment & layeringtransform, opacity— Graphic rendering
`inherit`, `initial`, `revert`, and `unset`
Every single CSS property accepts these four universal keywords to manually control the cascade:
inheritForces the child element to take the exact computed value of its direct parent, even for non-inherited properties!
initialResets property to the official W3C specification initial default (e.g. color: #000, display: inline).
revertRolls back the property to the browser's User-Agent stylesheet default (e.g. restoring link underlines).
unsetA hybrid: acts like inherit if the property naturally inherits, or like initial if it doesn't!
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!
I am parent text. My color is #6366f1 and font-size is 18px.
I am a nested child paragraph!
Which Value Cascades Through?
Round 1 of 3The 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; }3 Big Cascade Traps for Beginners
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.
`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.
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!
3. Pseudo-Classes & Elements
Dynamic UI states (:hover, :focus-visible, :nth-child) and virtual sub-elements (::before, ::after).