Lesson 8: Mastering CSS Precedence Rules
Learn how CSS resolves conflicting styles using precedence rules, specificity calculations, and the !important declaration.
Start LearningUnderstanding CSS Precedence
CSS precedence determines which styles get applied when multiple rules target the same element. The browser follows a specific order to resolve conflicts:
Source Order (Sequential Rule)
When multiple rules with equal specificity target the same element, the last rule in the CSS source order takes precedence.
Example: Conflicting Styles
/* First rule */ h2 { color: red; text-decoration: underline; } /* Second rule - this will take precedence */ h2 { color: blue; text-decoration: none; }
This heading will be blue without underline
Even though the first rule set the text to red with underline, the second rule takes precedence because it comes later in the source order.
Specificity Rule
Specificity is a weight that determines which CSS rule is applied when multiple rules could target the same element. Each selector type has a different weight:
Specificity Calculation
p
.intro
#header
nav ul li a.active
#main .content p:first-child
Specificity in Action
Which style will win?
!important Rule
The !important
declaration overrides all other rules regardless of specificity or source order. It should be used sparingly as it can make CSS difficult to maintain.
Syntax
selector { property: value !important; }
Example: !important Override
#important-demo { color: blue; } .special-text { color: red !important; }
This text is red because of !important
Even though the ID selector has higher specificity, the !important declaration wins.
Precedence Comparison
Understanding how precedence rules interact is crucial for effective CSS development.
Scenario | Winning Rule | Reason |
---|---|---|
Inline style vs ID selector | Inline style | Inline styles have higher specificity (1,0,0,0) |
ID selector vs class selector | ID selector | IDs (0,1,0,0) beat classes (0,0,1,0) |
Class selector vs element selector | Class selector | Classes (0,0,1,0) beat elements (0,0,0,1) |
Equal specificity, different source order | Last defined rule | Source order determines precedence |
Normal rule vs !important rule | !important rule | !important overrides all other rules |
Interactive Precedence Demo
Test the precedence rules
Current applied styles: None
Practical Applications
Understanding precedence rules helps resolve common styling challenges:
Framework Overrides
Override CSS framework styles with appropriate specificity rather than !important.
Theming Systems
Create themeable interfaces where base styles can be overridden by more specific theme classes.
Responsive Design
Ensure media queries have sufficient specificity to override base styles.
Debugging CSS
Quickly identify why certain styles aren't applying by examining specificity.
Real-world Example: Theming System
/* Base button style */ .btn { padding: 10px 20px; border-radius: 4px; background: #eee; color: #333; } /* Theme-specific overrides */ .theme-dark .btn { background: #333; color: #fff; } /* High-priority button in dark theme */ .theme-dark .btn.primary { background: #0066cc !important; }
The !important is only used for the highest-priority elements in specific contexts.
Best Practices
Follow these guidelines for effective CSS precedence management:
Specificity Management Techniques
Technique | Implementation | Benefit |
---|---|---|
Class-based styling | .btn-primary instead of #submit-button |
Predictable, reusable specificity |
CSS methodologies | BEM, SMACSS, ITCSS | Systematic specificity management |
CSS variables | --primary-color: blue; |
Change values without increasing specificity |
Cascade layers | @layer base, components; |
Control source order of entire style sections |
Next Steps
Now that you understand CSS precedence rules, continue to the next lesson to learn about CSS inheritance.