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:
- โ
Introduction to CSS
- โ
Basic selectors (type, class, ID)
- โ
CSS syntax and properties
- โ
Linking CSS to HTML
Today's Mission:
Understand how CSS decides which styles win!
Today's Learning Objectives
- ๐ฏ Understand what the cascade is and why it matters
- ๐ฏ Master CSS specificity calculations
- ๐ฏ Learn how inheritance works
- ๐ฏ Understand rule order as the tie-breaker
- ๐ฏ Fix cascade-related CSS issues
Why Do We Need the Cascade? ๐ต
Ever had this happen?
p { color: blue; }
Multiple rules can target the same element:
- Browser default styles
- Your CSS files
- Multiple selectors targeting the same element
- Inline styles
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!
Class Selector
10
.class { }
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
<div class="main">
<div class="list subsection">What color am I?</div>
</div>
.subsection {
color: blue;
}
VS
.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
<div class="main">
<div class="list" id="subsection">What color now?</div>
</div>
#subsection {
color: blue;
}
VS
.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 { }
Total Specificity: 120
Quick Method: Count IDs (ร100), Classes (ร10), Types (ร1), then add!
Quick Practice: Calculate Specificity! โ๏ธ
Calculate the specificity of each selector:
div p span
.header .nav-item
#main .content p
body #wrapper .container ul li
Answers: (Click through when ready!)
- 3 (three type selectors)
- 20 (two class selectors)
- 111 (one ID, one class, one type)
- 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
.class.second-class { }
.class .second-class { }
.class > .second-class { }
Factor #2: Inheritance ๐จโ๐งโ๐ฆ
Some properties pass from parent to child!
<div style="color: red;">
<p>I'm red too!</p>
</div>
Properties that typically inherit:
- โ
Typography: color, font-family, font-size, line-height
- โ
Text properties: text-align, text-transform
- โ
List properties: list-style
Properties that DON'T inherit:
- โ Box model: margin, padding, border
- โ Background properties
- โ Position and display
Direct Styling ALWAYS Beats Inheritance
<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:
- Same specificity โ
- Both direct (not inherited) โ
- Now what? ๐ค
Rule Order in Action
.alert {
color: red;
}
.warning {
color: yellow;
}
<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 โจ
- ๐ Keep specificity low - use classes primarily
- ๐ฏ Be only as specific as necessary
- ๐๏ธ Structure CSS from general to specific
- ๐ซ Avoid inline styles
- โ ๏ธ Use !important only in emergencies
- ๐ Use DevTools to debug cascade issues
- ๐ Organize CSS files logically
Remember: The cascade is your friend once you understand it!
Using DevTools for Cascade Debugging ๐
Live Demo: Let's Debug Together!
- Open your Recipe Project
- Right-click โ Inspect Element
- Look at Styles panel
- See crossed-out styles (lost cascade battle)
- 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:
- Six elements with cascade problems
- Fix the CSS to match the desired output
- Use your knowledge of specificity and rule order
- No changing the HTML!
Let's walk through the setup together...
Setting Up the Exercise ๐ป
git clone https://github.com/TheOdinProject/css-exercises.git
cd css-exercises/foundations/cascade/01-cascade-fix
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:
- Open the exercise files
- Read the README instructions
- Fix the cascade issues in style.css
- Match the desired-outcome.png
- 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:
- Problem 1: Class selector being overridden by type
- Solution: Increase specificity or reorder
- Problem 2: Multiple classes with same specificity
- Solution: Use rule order to your advantage
- Problem 3: Inherited styles interfering
- Solution: Add direct styling
Remember: There are multiple correct solutions! The goal is understanding WHY it works.
Quick Knowledge Check! ๐ง
Answer These:
- Which has higher specificity:
.header .nav
or #nav
?
- Does
margin
inherit from parent elements?
- If two rules have the same specificity, which wins?
- What's the specificity of
div.highlight#main
?
Answers:
- #nav (100 > 20)
- No, margin doesn't inherit
- The last one in the CSS file
- 111 (1 ID + 1 class + 1 type)
Real-World Cascade Scenarios ๐
Scenario 1: Theme Customization
.button { background: blue; }
.button { background: green; }
Scenario 2: Responsive Design
.container { width: 100%; }
@media (min-width: 768px) {
.container { width: 80%; }
}
Homework ๐
Due Before Next Class
Your Tasks:
- Complete the cascade exercise if not finished
- Read "The CSS Cascade" article (link in Odin Project)
- Watch Kevin Powell's "CSS Specificity Explained" video
- Try the CSS Specificity Calculator online tool
- 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:
- โ
Resolves conflicts between CSS rules
- โ
Uses specificity as primary factor
- โ
Direct styling beats inheritance
- โ
Last rule wins when all else is equal
Best Practices:
- โ
Keep specificity low
- โ
Use classes over IDs for styling
- โ
Organize CSS logically
- โ
Use DevTools for debugging
Next Class Preview
Tuesday: Browser Developer Tools
- Inspecting HTML and CSS
- Live editing in the browser
- Debugging cascade issues like a pro
- Performance insights
- Mobile device emulation
Get ready to become a debugging wizard! ๐งโโ๏ธ
Great Work Today! ๐
You've Mastered the Cascade!
Now you understand:
- โ
Why CSS rules conflict
- โ
How specificity works
- โ
The role of inheritance
- โ
How to debug cascade issues
See you Tuesday at 9:30 AM! ๐
Keep practicing with the cascade!
1 / 31