Coding/Learn/css/the cascade specificity
CSS Beginner Course
01.Introduction to CSS
02.Selectors (Element, Class & ID)
03.3 Ways to Add CSS to HTML
04.Colors & Backgrounds (HEX, RGB, HSL)
05.Typography & Fonts (font-size, font-weight, line-height)
06.The Cascade & Specificity Wars
07.The Sacred CSS Box Model
08.Padding vs Margin & Shorthand Notation
09.Borders & Border Radius (Rounded Corners & Circles)
10.The Magic of box-sizing: border-box
11.Sizing & Units (px, rem, em, %, vh, vw)
12.The Display Property (block, inline, inline-block, none)
13.Flexbox Part 1: Container Powers (justify, align, gap)
14.Flexbox Part 2: Direction, Wrapping & Item Powers
15.Positioning (relative, absolute, fixed, sticky)
16.Z-Index & The 3D Stacking Context
17.Pseudo-Classes (:hover, :active, :focus, :first-child)
18.Smooth Transitions & Box Shadows
19.Responsive Design & Media Queries (@media)
20.Capstone: Crafting a Modern UI Profile Card
Course Certificate0%

Complete all lessons to earn

LearnThe Cascade & Specificity Wars
6 / 20 0%
Lesson 06 of 20
5 mins read

The Cascade & Specificity Wars

PreviousNext Lesson
In Plain English

The "C" in CSS stands for Cascading. When two CSS rules target the exact same element with conflicting properties, the browser uses a scoring formula called Specificity to declare the winner. Whoever has the highest rank wins!

The Mental Model

"Think of military rank. An element selector (p) is a Private (Score: 1). A class selector (.alert) is a Captain (Score: 10). An ID selector (#main-alert) is a General (Score: 100). An inline style is the President. If a Private and a General give conflicting orders, the General always wins!"

Deep Dive: How It Works

Order of Appearance: If two rules have the exact same specificity score, the one written LAST in the stylesheet wins (the cascade).

Specificity Hierarchy: Element tag (1pt) < Class / pseudo-class (10pts) < ID (100pts) < Inline style (1000pts).

The !important nuclear option: Overrides everything, but creates unmaintainable code wars. Avoid using !important in healthy codebases!

Inheritance: Some properties (like color and font-family) automatically trickle down from parent <body> to child elements.

Syntax Blueprint

/* Specificity: 1 pt */
p { color: white; }

/* Specificity: 10 pts (WINS!) */
.danger-text { color: #f87171; }

Class selector has higher specificity score than tag selector and takes priority.

PointCore Rules to Remember

Point star
The Cascade Principle: Styles cascade down from general rules to specific classes.
Point star
Last Rule Wins on Tie: When scores are equal, the rule lower down in the file wins.
Point star
Avoid !important: Fix specificity issues with better class names rather than hammering !important.

Common Beginner Traps & How to Fix Them

Mistake:Using !important to fix every minor override

Why it happens: Frustration when a style doesn't apply.

How to fix: Check if an existing class or ID has higher specificity, and use a matching class instead.

Pro Tip: Keep your specificity low and uniform by styling 95% of your components with single class names (.btn, .card).

Live Interactive Example

Hit Run Code to see it live
Specificity Duel
CSS3
1
2
3
4
5
6
7
8
9
10
Live Browser Preview
Click "Preview / Apply" to view the rendered output.
How it works: The class selector score of 10 overrides the tag selector score of 1.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Override a tag rule with a class

  • PointWrite a class rule for .active-link setting color: #38bdf8; and font-weight: bold;
1
2
Browser Preview & Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates