Class 9 - The Cascade

CSS Foundations

The Cascade ๐Ÿ”๏ธ

CSCI 3403 - Web Programming

Week 5, Class 9

Understanding the "C" in CSS! ๐Ÿ’ง

Quick Recap

Last Class:

Today's Mission:

Understand how CSS decides which styles win!

Today's Learning Objectives

Why Do We Need the Cascade? ๐Ÿ˜ต

Ever had this happen?

/* You wrote this... */
p { color: blue; }

/* But the text is red?! ๐Ÿ˜ค */

Multiple rules can target the same element:

The cascade decides who wins! ๐Ÿ†

The Cascade Defined ๐ŸŒŠ

The cascade is CSS's conflict resolution system

Multiple CSS Rules
Targeting Same Element
Cascade Algorithm Runs
Winner Determined!
Think of it like: A waterfall where styles flow down and the strongest survive!

The Cascade's Three Main Factors

1๏ธโƒฃ Specificity

How specific is the selector?

ID > Class > Type

2๏ธโƒฃ Inheritance

Does it inherit from parent?

Direct > Inherited

3๏ธโƒฃ Rule Order

Which rule came last?

Last one wins!

Let's explore each factor in detail...

Factor #1: Specificity ๐ŸŽฏ

Not all selectors are created equal!

Type Selector

1
p { }

Class Selector

10
.class { }

ID Selector

100
#id { }
Remember: 1 ID beats 1000 classes, 1 class beats 1000 types!

The Specificity Hierarchy

Inline Styles (1000)
ID Selectors (100)
Class Selectors (10)
Type Selectors (1)
Pro Tip: Avoid inline styles! They're nearly impossible to override.

Specificity in Action: Example 1

<!-- HTML -->
<div class="main">
  <div class="list subsection">What color am I?</div>
</div>
/* Rule 1 */
.subsection {
  color: blue;
}
VS
/* Rule 2 - WINS! */
.main .list {
  color: red;
}
What color am I?

Why? Both use class selectors, but Rule 2 has TWO classes (20) vs one (10)!

Specificity in Action: Example 2

<!-- HTML -->
<div class="main">
  <div class="list" id="subsection">What color now?</div>
</div>
/* Rule 1 - WINS! */
#subsection {
  color: blue;
}
VS
/* Rule 2 */
.main .list {
  color: red;
}
What color now?

Why? ID (100) beats two classes (20). IDs always win!

Calculating Specificity ๐Ÿงฎ

Let's practice counting!

.main #content .highlight { }
IDs
1
Classes
2
Types
0

Total Specificity: 120

Quick Method: Count IDs (ร—100), Classes (ร—10), Types (ร—1), then add!

Quick Practice: Calculate Specificity! โœ๏ธ

Calculate the specificity of each selector:

  1. div p span
  2. .header .nav-item
  3. #main .content p
  4. body #wrapper .container ul li
Answers: (Click through when ready!)
  1. 3 (three type selectors)
  2. 20 (two class selectors)
  3. 111 (one ID, one class, one type)
  4. 122 (one ID, two classes, two types)

What DOESN'T Add to Specificity? ๐Ÿšซ

These don't count!

Universal Selector

* { }

Specificity: 0

Combinators

>, +, ~, space

Don't add specificity

/* Both have same specificity (20) */
.class.second-class { } /* Chaining */
.class .second-class { } /* Descendant */
.class > .second-class { } /* Child combinator */

Factor #2: Inheritance ๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ

Some properties pass from parent to child!

<div style="color: red;">
  <p>I'm red too!</p> <!-- Inherits color -->
</div>

Properties that typically inherit:

Properties that DON'T inherit:

Direct Styling ALWAYS Beats Inheritance

<!-- HTML -->
<div id="parent">
  <div class="child">What color am I?</div>
</div>
#parent {
  color: red;
}
VS
.child {
  color: blue;
}

Result: Blue wins! Even though #parent has higher specificity (100 vs 10), .child directly targets the element.

Remember: Direct styling > Inherited styling (always!)

Factor #3: Rule Order ๐Ÿ“‹

The tie-breaker of tie-breakers!

When everything else is equal:

The LAST rule wins! ๐Ÿ†

Rule Order in Action

/* Both selectors have same specificity (10) */

.alert {
  color: red;
}

.warning {
  color: yellow; /* This wins - it's last! */
}
<!-- HTML -->
<p class="alert warning">Important message!</p>
Important message!

Note: Order in HTML class attribute doesn't matter - only CSS file order!

The Complete Cascade Algorithm

1. Collect all rules targeting element
2. Check Specificity - Higher wins!
3. If tied, check Direct vs Inherited
4. If still tied, Last Rule Wins!
Pro Tip: Use browser DevTools to see which rules are winning!

Common Cascade Problems ๐Ÿ”ง

Problem 1: Over-Specific Selectors

body div.container ul li a.link { } /* Too specific! */
.link { } /* Much better */

Problem 2: Using IDs for Styling

#header { } /* Hard to override */
.header { } /* Easier to work with */

Problem 3: !important Overuse

color: red !important; /* Nuclear option! */

Cascade Best Practices โœจ

Remember: The cascade is your friend once you understand it!

Using DevTools for Cascade Debugging ๐Ÿ”

Live Demo: Let's Debug Together!

  1. Open your Recipe Project
  2. Right-click โ†’ Inspect Element
  3. Look at Styles panel
  4. See crossed-out styles (lost cascade battle)
  5. Hover over selectors to see specificity
DevTools shows:
  • โœ“ Applied styles (winning)
  • โœ— Overridden styles (crossed out)
  • ๐Ÿ“ Source file and line number
  • ๐ŸŽฏ Computed final values

Practice Time: Cascade Fix! ๐Ÿ› ๏ธ

Today's Exercise

Complete the 01-cascade-fix exercise from The Odin Project

You'll fix CSS cascade issues in a provided HTML file

The Challenge:

Let's walk through the setup together...

Setting Up the Exercise ๐Ÿ’ป

# If you haven't cloned the exercises repo yet:
git clone https://github.com/TheOdinProject/css-exercises.git

# Navigate to today's exercise:
cd css-exercises/foundations/cascade/01-cascade-fix

# Open in VS Code:
code .
Files you'll see:
  • index.html - DON'T MODIFY
  • style.css - FIX THE CASCADE HERE
  • README.md - Instructions
  • desired-outcome.png - What it should look like

Work Session โšก

20 Minutes to Complete

Your Tasks:

  1. Open the exercise files
  2. Read the README instructions
  3. Fix the cascade issues in style.css
  4. Match the desired-outcome.png
  5. Test in your browser

Hints:

  • Check specificity first
  • Look for rule order issues
  • Some rules might need more specific selectors
  • Some might need less specific ones!

Exercise Solutions Discussion ๐Ÿ’ก

Common Fixes Needed:

Remember: There are multiple correct solutions! The goal is understanding WHY it works.

Quick Knowledge Check! ๐Ÿง 

Answer These:

  1. Which has higher specificity: .header .nav or #nav?
  2. Does margin inherit from parent elements?
  3. If two rules have the same specificity, which wins?
  4. What's the specificity of div.highlight#main?
Answers:
  1. #nav (100 > 20)
  2. No, margin doesn't inherit
  3. The last one in the CSS file
  4. 111 (1 ID + 1 class + 1 type)

Real-World Cascade Scenarios ๐ŸŒ

Scenario 1: Theme Customization

/* Base theme */
.button { background: blue; }

/* Your customization - comes after */
.button { background: green; } /* Wins! */

Scenario 2: Responsive Design

/* Mobile first */
.container { width: 100%; }

/* Desktop override - same specificity, comes later */
@media (min-width: 768px) {
  .container { width: 80%; } /* Wins on desktop! */
}

Homework ๐Ÿ“š

Due Before Next Class

Your Tasks:

  1. Complete the cascade exercise if not finished
  2. Read "The CSS Cascade" article (link in Odin Project)
  3. Watch Kevin Powell's "CSS Specificity Explained" video
  4. Try the CSS Specificity Calculator online tool
  5. Apply cascade knowledge to your Recipe Project - make it prettier!
Extra Credit: Use DevTools to find and fix a cascade issue in your Recipe Project

Today's Key Takeaways ๐ŸŽฏ

The Cascade:

Best Practices:

Next Class Preview

Tuesday: Browser Developer Tools

Get ready to become a debugging wizard! ๐Ÿง™โ€โ™‚๏ธ

Great Work Today! ๐ŸŽ‰

You've Mastered the Cascade!

Now you understand:

See you Tuesday at 9:30 AM! ๐Ÿ‘‹

Keep practicing with the cascade!