Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

The Cascade & Specificity | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 4 of 50

·css

The Cascade & Specificity

12 mins

Course progress8%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • The cascade follows origin → importance → specificity → source order.
  • `!important` should be used sparingly.
  • Understanding specificity prevents unexpected style overrides.

When multiple rules match the same element, the cascade decides the winner.

  1. Origin – The source of the style sheet:
    • User‑agent (browser defaults)
    • User (user‑style sheets for accessibility)
    • Author (your CSS)
    • Inline (style= attribute) – highest origin level.
  2. Importance – !important elevates a declaration above its origin level.
  3. Specificity – A numeric weight based on selector components:
    • inline → (1,0,0,0)
    • #id → (0,1,0,0)
    • .class, [attr], :pseudo‑class → (0,0,1,0)
    • element, ::pseudo‑element → (0,0,0,1)
  4. Source order – If everything else ties, the later rule wins.

Specificity calculator tip: You can copy a selector into the Chrome DevTools Styles pane; hovering the selector shows its computed specificity.


Specificity Battle Demo

/* 0,0,0,1 */ p            { color:gray; }     /* lowest */
/* 0,0,1,0 */ .note       { color:blue; }
/* 0,1,0,0 */ #important   { color:red; }      /* highest */
/* 1,0,0,0 */ <p style="color:green;">   /* inline */

Best‑Practice Checklist

  • Never rely on !important for regular styling – it makes later overrides impossible.
  • Keep selector chains as short as possible (e.g., .card not section > div > .card).
  • Reset box-sizing globally to avoid unexpected width calculations.
  • Use CSS variables for theme colors; they inherit and never increase specificity.
Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonBasic SelectorsContinue to lesson 5The Box ModelBack to CSS Basics to Advanced curriculum