Don't.
You are still here, so I guess you were expecting a more detailed explanation... here it goes:
The use of !important
is considered an anti-pattern and bad practice. !important
overrides all other declarations and makes the CSS code more difficult to maintain and debug. The only thing that can override an !important
is another !important
, and once you go down that road, it never stops.
From a Web Accessibility perspective, the use of !important
is negative because it would override the end-user defined styling. And there are multiple cases in which users –and especially users with disabilities– may want to override some of the CSS properties (e.g. to enlarge the font size, or change the font-family, or even change the distribution and position of elements in a page.)
What can developers do instead of using !important
?
- Use more specific selectors: add the element tag before the class/id name, or a new class name... anything that increases the specificity of the selector.
- Take advantage of the CSS cascade: if needed, restructure the CSS and move the CSS rules, as if two rules have the same specificity, the one that appears later "wins."
Note: as specified in the comments, there are some cases in which !important
may be useful (e.g. forcing immutability in utility classes, styling email campaigns, or in the end-user styles), but in general –and for the purpose of accessibility– it is better to avoid !important
because it takes away power from the users to customize the experience to better fit their needs.
Avoid using !important
in CSS.
Top comments (15)
I agree that there are some cases in which
!important
could be interesting (and it will most definitely need to be used in the user-defined styles to override the ones from the site). But from an accessibility perspective, the idea of immutability (from the developer side) is dangerous, as it takes away power from the users to customize the experience to better fit their needs. Let me recheck the article, and I'll edit and add a note about it.I believe there is no valid use cases for
!important
. Your suggestions on the alternatives are top-notch, and following these usually makes your code cleaner.You've clearly never built an HTML email :) inline styles for Outlook so need to use
!important
to overwrite them... horrible but in this case necessary.inline styles will replace !important
I learnt this by the video trivia by web dev simplified and kevin powell!
And kevin as a CSS Mastermind lost to this question... Very bad!
But, Kevin is the CSS Master!
Wait...
!important
will trump inline styles 🤔You made me doubt for a second, so I created an example of it for another comment:
!important
does override inline styles.Actually I expect custom origins/cascade layers to eventually replace most !important use. But it'll be some years before we can use them.
Look I'm not saying that
!important
is a dangerous anti-pattern, but in 10 years developing websites as my main revenue source the only times I've had to use it is when someone else used it before (by example in a WordPress theme, if I had to pick at random).I get the point of your article about utility classes being all alone and depraved of specificity thus needing a bit of help from
!important
but that is so edge case, it should definitely be a last resort after you've exhausted your other last resort options.Important is not bad it's the situation that leads to this being used that's bad. In a JavaScript context it has some uses I suppose.
After seeing the title I was hoping so hard for this first paragraph to be there, thank you for making my dreams true.
Agree however I do use !important a fair bit in the work I do with HTML emails because Outlook..., but I tend to avoid in anything else.
Sometimes you need to apply styles to elements abstractly so you can't get specific with your selectors.
Also online styles will override !important if you add !important to the online style itself.
When I find elements that are too generic to style, I normally solve it by adding a class/id to the elements or one of their ancestors, which allows for easier and more specific selectors. Do you have an example of applying styles to elements abstractly and how did you fix it with
!important
?Also, inline styles do not override
!important
as it can be seen in this example:Right i actually meant inline with !importa overrides styleseet !important
Hahaha, your "You are still here...", it is so cute 🤣