CSS Cascade - Guided Notes

CSCI 4513 - Web Programming | Week 5, Class 9

Name: _________________________________ Date: _____________

📚 Today's Focus: Understanding how CSS decides which styles "win" when rules conflict

🌊 What is the Cascade?

Definition

The cascade is CSS's _________________ resolution system.

It determines which CSS rules get _________________ to HTML elements.

Why Do We Need It?

Multiple sources of CSS can target the same element:

💡 Remember: CSS only does what we tell it to do!

🎯 The Three Main Factors

The cascade uses three main factors to resolve conflicts:

Factor Description Rule
1. _________________ How specific is the selector? More specific wins
2. _________________ Does it inherit from parent? Direct beats inherited
3. _________________ Which rule came last? Last one wins

1️⃣ Factor #1: Specificity

Specificity Hierarchy (from highest to lowest)

  1. _________________ styles (avoid these!) - Value: 1000
  2. _________________ selectors - Value: 100
  3. _________________ selectors - Value: 10
  4. _________________ selectors - Value: 1
⚠️ Important Rule: 1 ID beats _______ classes, 1 class beats _______ type selectors!

Practice: Calculate Specificity

Calculate the specificity for each selector:

  1. p = _______
  2. .header = _______
  3. #main = _______
  4. .nav .item = _______
  5. #header .nav li = _______
  6. div.highlight#main = _______

What DOESN'T Add to Specificity?

📝 Specificity Examples

Example 1: Class Selectors

<div class="main"> <div class="list subsection">Text</div> </div>
/* Rule 1 */ .subsection { color: blue; } /* Specificity: _____ */ /* Rule 2 */ .main .list { color: red; } /* Specificity: _____ */

Which rule wins? _________________

Why? _________________________________________

Example 2: ID vs Classes

<div class="main"> <div class="list" id="subsection">Text</div> </div>
/* Rule 1 */ #subsection { color: blue; } /* Specificity: _____ */ /* Rule 2 */ .main .list { color: red; } /* Specificity: _____ */

Which rule wins? _________________

Why? _________________________________________

2️⃣ Factor #2: Inheritance

Properties That Typically Inherit:

Properties That DON'T Inherit:

💡 Key Rule: Direct styling ALWAYS beats _________________!

Inheritance Example

<div id="parent"> <div class="child">Text</div> </div> #parent { color: red; } /* Specificity: 100 */ .child { color: blue; } /* Specificity: 10 */

What color is the text? _________________

Why? _________________________________________

3️⃣ Factor #3: Rule Order

The Tie-Breaker

When two rules have:

Then: The _________________ rule wins!

Rule Order Example

.alert { color: red; } .warning { color: yellow; } <p class="alert warning">Message</p>

What color is the text? _________________

Why? _________________________________________

Note: Order in the HTML class attribute does NOT matter - only CSS file order!

🔄 The Complete Cascade Algorithm

Fill in the cascade decision process:

  1. Collect all _________________ targeting the element
  2. Check _________________ - higher wins!
  3. If tied, check _________________ vs inherited
  4. If still tied, _________________ rule wins!

🏋️ Practice Problems

Problem 1: Predict the Color

<div id="container" class="box"> <p class="text">What color am I?</p> </div> #container { color: red; } .box p { color: blue; } .text { color: green; }

Answer: _________________

Explanation: _________________________________________

Problem 2: Calculate Total Specificity

Selector IDs Classes Types Total
body #wrapper .container ul li ____ ____ ____ ____
nav > ul > li > a.active ____ ____ ____ ____

✨ Best Practices

Do's ✅

Don'ts ❌

🔍 Using DevTools for Cascade Debugging

DevTools shows you:

DevTools Exercise

Open your Recipe Project and find:

  1. A style that's being overridden: _________________________________
  2. The winning rule's specificity: _________________________________
  3. The source file and line number: _________________________________

📋 Today's Exercise: 01-cascade-fix

Setup Commands

git clone _________________________________________ cd css-exercises/foundations/cascade/_________________ code .

Exercise Notes

Problems encountered and solutions:

  1. Problem: _________________________________________
    Solution: _________________________________________
  2. Problem: _________________________________________
    Solution: _________________________________________
  3. Problem: _________________________________________
    Solution: _________________________________________

📝 Additional Notes

Use this space for any additional notes, examples, or questions:












🎯 Key Takeaways

Complete these summary statements:

  1. The cascade resolves _________________________________________
  2. Specificity is calculated by counting _________________________________________
  3. Direct styling always beats _________________
  4. When specificity is equal, the _________________ rule wins
  5. The best practice is to keep specificity _________________