Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 6 of 11

CSS Cascade, Specificity & Inheritance

The 'Cascading' in CSS refers to how the browser decides which styles win when multiple rules target the same element. Understanding cascade, specificity, and inheritance is essential - it explains why your styles sometimes don't work and how to fix conflicts.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

How the Cascade Works

  • Cascade - When multiple rules set the same property, the browser uses a priority system to decide the winner
  • Specificity - Each selector has a score: ID (#) = 100 points, Class (.) = 10 points, Element (p) = 1 point. Higher score wins
  • Source order - When specificity is equal, the rule that appears last in the CSS wins
  • !important - Overrides all specificity. p { color: red !important; }. Avoid using it - makes debugging very hard
  • Inheritance - Some properties (color, font-size, font-family) are inherited by child elements automatically
  • Non-inherited properties - margin, padding, border, background do NOT inherit. Each element needs its own
  • inherit keyword - Force inheritance: border: inherit; makes a child copy the parent's border

Try It Yourself: Specificity Battle

Try It Yourself: Specificity BattleCSS
CSS
โœ“ Valid
CSS|24 lines|โœ“ Valid syntax
UTF-8

Specificity Example

Specificity Example
/* Specificity scoring */

/* 1 point - element selector */
p { color: black; }

/* 10 points - class selector */
.text { color: blue; }

/* 100 points - ID selector */
#intro { color: red; }

/* 11 points - element + class */
p.text { color: green; }

/* Winner for <p class="text" id="intro">?
   #intro wins (100 points) โ†’ color: red */

/* !important overrides everything (avoid!) */
p { color: purple !important; }
/* Now purple wins regardless of specificity */

Tip

When your styles aren't working, don't reach for !important. Instead, open DevTools (F12), inspect the element, and look at the Styles panel. It shows you exactly which rule is winning and its specificity score - fix the root cause instead of using !important.

Diagram
Loading diagramโ€ฆ

The cascade determines which style wins when multiple rules conflict

Common Mistake

Using ID selectors (#header, #nav) for CSS styling creates specificity problems. IDs have 100 points vs classes with 10 points, making them very hard to override later. Use classes for styling and reserve IDs for JavaScript and anchor links.

Practice Task

In the specificity battle editor above: (1) Add a new rule .blue-text { font-size: 24px; } and predict which paragraph gets 24px. (2) Try adding !important to the element selector's color and see how it overrides everything. (3) Remove the !important - never use it in real projects!

Quick Quiz

Key Takeaways

  • The 'Cascading' in CSS refers to how the browser decides which styles win when multiple rules target the same element.
  • Cascade - When multiple rules set the same property, the browser uses a priority system to decide the winner
  • Specificity - Each selector has a score: ID (#) = 100 points, Class (.) = 10 points, Element (p) = 1 point. Higher score wins
  • Source order - When specificity is equal, the rule that appears last in the CSS wins

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/11 done