Specificity determines which matching selector wins when multiple declarations target the same property.
Specificity order
- Inline styles (highest among normal author styles).
- ID selectors.
- Class/attribute/pseudo-class selectors.
- Element/pseudo-element selectors.
Debug this snippet
.btn { color: #111827; }\n#checkout .btn { color: #dc2626; }\n.btn { color: #1d4ed8; }
The ID-based selector still wins. Later source order only wins when specificity ties.
Important interview questions and answers
- Q: Why avoid IDs in component CSS?
A: Creates high-specificity locks that are hard to override cleanly. - Q: What breaks specificity ties?
A: Later source order. - Q: Practical strategy for manageable specificity?
A: Class-based selectors with shallow nesting and consistent component conventions.
Pitfall: Check cascade order—author stylesheet loses to inline styles and !important surprises.