The Truth About !important in CSS: When to Use It (and When to Run Away)
We've all done it. You change a color in CSS, refresh the page, and... nothing happens. You try
again. Nothing. Frustrated, you add !important to the end of the line. It works! You
commit the code and go to lunch.
Congratulations, you just planted a time bomb in your codebase.
The Specificity Wars
To understand why !important is dangerous, you need to understand
Specificity. It's the score browsers use to decide which rule wins.
- Tags (h1, p): 1 point
- Classes (.btn): 10 points
- IDs (#header): 100 points
- Inline Styles: 1000 points
If you have a conflict, the higher score wins. But !important is the nuclear option. It
overrides everything, even inline styles.
Why It's a Trap
When you use !important, you break the natural cascading order of CSS. The only way to
override an !important rule is with another !important rule.
This leads to "Specificity Wars," where your stylesheet becomes a mess of !important
tags fighting each other. It makes your code impossible to maintain.
The Only Valid Use Case
Is there ever a good time to use it? Yes: Utility Classes.
If you have a class called .hidden, you want it to hide the element no matter
what. Even if that element has an ID or inline styles.
.hidden {
display: none !important;
}
This is acceptable because the intent is absolute. For everything else, use better selectors.
How to Fix It Properly
Instead of reaching for the nuclear button, increase your specificity.
Bad:
/* Won't work if #sidebar .link is defined elsewhere */
.link {
color: red !important;
}
Good:
/* More specific, no hacks needed */
#sidebar .link {
color: red;
}
Experiment safely. Use our Live CSS Editor to play with specificity and see how different selectors interact without breaking your real project.