When two rules conflict, the browser does not guess—it follows the cascade. Understanding it saves hours of “why is my color wrong?”
Resolution order (simplified)
- Origin and importance (
!important). - Specificity — inline style > ID > class/attribute/pseudo-class > type.
- Source order — later rules win ties.
Specificity intuition
p— low..btn— higher.#save— higher still (avoid styling with IDs).style="color:red"on the element — very high.
Debugging workflow
- Open DevTools → inspect the element.
- Find the winning declaration (often struck-through rules above it).
- Trace back to the file or block; fix specificity or remove duplicates.
Team habit
Prefer one clear class per component state (.btn.is-primary) instead of long chained selectors like main div ul li a.
Important interview questions and answers
- Q: What beats equal-specificity rules?
A: Source order—whichever rule appears last in the cascade. - Q: Why avoid `!important` in app CSS?
A: It breaks predictable overrides and leads to escalation wars. - Q: How do you lower specificity deliberately?
A: Replace IDs with classes, shorten selectors, split components.