CSS Specificity Debugging: A Practical Guide
Thu Aug 14 2025
Overriding styles with !important
leads to chaos. Debug specificity properly.
1. Inspect the Element
Open DevTools, select element, view Computed panel:
- Which property value wins?
- Which selector applied it?
2. Calculate Specificity
Recall:
- Inline: 1000
- ID: 100
- Class/attr/pseudo-class: 10
- Element/pseudo-element: 1
3. Identify Unintended Overrides
Look for long nested selectors from utility frameworks or component libs.
4. Simplify the Source
Refactor heavy selectors to BEM-like small class tokens.
5. Introduce Utilities / Tokens
Favor Tailwind or utility classes over deep cascades.
6. Use Layering (if available)
With modern CSS @layer
, structure base, components, utilities.
7. Avoid Escalation
Do not increase specificity unless necessary. Resist adding IDs.
8. Add Regression Test (Visual)
Use Storybook snapshots or Percy to detect future clashes.
9. Remove Dead CSS
Run npx lightningcss --analyze
or PurgeCSS to spot unused selectors.
10. Style Hygiene Checklist
Selector causing issue: Expected vs actual: Root cause: Refactor applied: Follow-up cleanup:
Predictable styles scale teams.