Beginner 15 min readModule: Module 2: The Cascade, Specificity & Inheritance
The Cascade & Specificity Hierarchy
Master specificity calculation: inline styles (1000) > IDs (100) > Classes (10) > Elements (1).
What You Will Learn in This Lesson
- How the Cascade algorithm resolves conflicting properties
- The 4-column specificity matrix (Inline, ID, Class, Type)
- Why !important should be avoided in scalable codebases
Introduction & Core Concept
When multiple conflicting CSS rules target the same element, the browser uses specificity and the cascade order to determine which style wins.
WHY DOES THIS MATTER IN THE REAL WORLD?
Understanding specificity prevents fragile codebases where developers resort to adding '!important' everywhere to override stubborn styles.
Specificity Resolution
csscss
12345678/* Specificity: 0-0-0-1 (Element) */button { color: black; }/* Specificity: 0-0-1-0 (Class wins over element) */.action-btn { color: blue; }/* Specificity: 0-1-0-0 (ID wins over class) */#submit-btn { color: green; }
Line-by-Line Technical Breakdown
1If two selectors have equal specificity, the rule written last in the stylesheet takes precedence.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[CSS]
CSS SOURCE EDITOR
Interactive Live CodeIndustry Best Practices & Professional Standards
- Keep specificity low and uniform using single class selectors (BEM methodology).
Lesson Summary & Core Takeaways
- Specificity and source order resolve all CSS style declarations deterministically.