Chapter 18: Cascade & Inheritance

Cascade Inheritance

These two words explain why your styles sometimes do what you expect… and sometimes completely ignore what you wrote. If you master Cascade + Inheritance, 70–80% of “Why isn’t my style applying?!” moments disappear forever.

Let’s go slow, step-by-step, like we’re sitting together with VS Code open and Live Server running.

1. What is the Cascade?

Cascade = the system CSS uses to decide which rule wins when multiple rules try to style the same property on the same element.

Think of it like a court case: several lawyers (CSS rules) are arguing about the same thing (e.g. “What color should this paragraph be?”). The judge (browser) follows strict rules to pick the winner.

The full cascade order (from strongest to weakest):

  1. Transition & animation declarations (almost never relevant for beginners)
  2. !important (very high priority — avoid unless debugging or overriding third-party CSS)
  3. Inline styles (style=”color: red;”) — extremely strong
  4. Specificity (we’ll cover this in detail next)
  5. Source order — if everything else is equal, the rule that appears later in the CSS file (or later <style> block) wins

2. Quick Visual Cascade Flowchart (Memorize This Mentally)

text

3. What is Inheritance?

Inheritance = some CSS properties are automatically passed down from a parent element to all its children (and grandchildren, etc.).

You set the style once on a parent → children get it “for free” (unless they override it).

Properties that inherit by default (very common):

  • color
  • font-family, font-size, font-weight, font-style
  • line-height
  • text-align
  • text-indent
  • letter-spacing, word-spacing
  • visibility
  • cursor
  • list-style (for <ul>, <ol>)

Properties that do NOT inherit (you must set them explicitly on each element):

  • margin, padding, border
  • background, background-color
  • width, height, display
  • position, float, flex, grid
  • box-shadow, border-radius
  • overflow

Quick memory trick: If it affects how text looks or how the element behaves as a container → usually inherits. If it affects size, spacing, position, or visual boxes → usually does not inherit.

4. Real Example – Let’s See Both in Action

index.html – copy-paste this

HTML

style.css – watch cascade & inheritance fight

CSS

What You Should See & Experiment With

  1. All normal text is dark gray (#1e293b) → inheritance from body
  2. Header text is white → overrides inherited color
  3. Paragraph inside .card is navy → later p rule wins over body color (source order + equal specificity)
  4. Inline purple paragraph → inline style wins (cascade priority #3)
  5. Buttons have no margin/padding from body → non-inherited properties
  6. .btn.special has different background & shadow → specificity override

Quick experiments to do right now:

  • Change body { color: teal; } → almost all text changes (inheritance!)
  • Add color: lime !important; to body → everything turns lime (except inline styles)
  • Move the p { color: navy; } rule beforebody rule → navy disappears (source order!)
  • Add style=”color: orange;” to any <p> → orange wins (inline beats everything else)

Summary Table – Cascade vs Inheritance

Concept What it decides Key mechanism Memory trick / gotcha
Cascade Which rule wins when many target same property !important > inline > specificity > source order Think of it as a fight with strict ranking
Inheritance Which properties automatically pass to children Automatic for text-related props “Looks-related” yes, “Box-related” no

How does it feel now? Any part still confusing? Want to see a bigger “specificity war” example with more competing rules?

Next possible steps:

  • “selectors & specificity deep dive”
  • “how to debug when styles don’t apply”
  • “BEM to avoid specificity problems”
  • “CSS custom properties + inheritance”

You’re getting very strong now — these two concepts unlock most CSS mysteries. Chai thanda mat hone dena — keep coding! 🚀 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *