CSS Specificity Conflict Fix
In this tutorial, you'll learn about CSS Specificity Conflict Fix. We cover key concepts, practical examples, and best practices.
The Problem
A CSS rule does not apply even though it appears later in the stylesheet. A conflicting selector with higher specificity overrides it. Specificity is calculated as a four-part value: inline styles, IDs, classes/attributes, and elements.
Quick Fix
Step 1: Calculate selector specificity
/* Wrong — high-specificity ID selector overrides class */
#header .nav-item {
color: blue;
}
/* This class selector never applies */
.nav-item.active {
color: red; /* lower specificity */
}
Fix by matching or exceeding specificity:
/* Right — match specificity */
#header .nav-item.active {
color: red;
}
Expected output: The active color applies correctly.
Step 2: Avoid inline styles for override problems
Inline styles have the highest specificity and cannot be overridden by external CSS:
<!-- Wrong -->
<div style="color: blue;">Text</div>
Move inline styles to classes:
.text-primary { color: blue; }
.text-highlight { color: red; }
Step 3: Remove !important to restore maintainability
/* Wrong — difficult to override */
.button {
background: blue !important;
}
.button.warning {
background: red; /* does not apply */
}
/* Right — use specificity */
.button-warning {
background: red;
}
Expected output: The warning style applies without !important.
Step 4: Use BEM naming to keep specificity flat
/* Wrong — deep nesting increases specificity */
.card .card__header .card__title {
font-size: 18px;
}
/* Right — BEM keeps single-class specificity */
.card__title {
font-size: 18px;
}
Expected output: All selectors have equal specificity, making overrides predictable.
Step 5: Debug with DevTools
Open the Elements panel and check the Styles tab. Conflicting rules are shown with strikethrough. The Computed tab shows which rule wins and why.
Prevention
- Use class selectors instead of IDs for styling
- Adopt BEM or utility-first CSS (Tailwind) to avoid specificity escalation
- Never use
!importantin application code - Keep nesting depth to 3 levels maximum in preprocessors
Common Mistakes with specificity conflict
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world CSS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro