DEV Community

Cover image for Using !important in CSS

Using !important in CSS

Alvaro Montoro on June 13, 2020

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 an...
Collapse
 
alvaromontoro profile image
Alvaro Montoro

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.

Collapse
 
guitarino profile image
Kirill Shestakov

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.

Collapse
 
cguttweb profile image
Chloe • Edited

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.

Collapse
 
vuelancer profile image
Vuelancer

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!

Collapse
 
alvaromontoro profile image
Alvaro Montoro

Wait...!important will trump inline styles 🤔

Collapse
 
alvaromontoro profile image
Alvaro Montoro

You made me doubt for a second, so I created an example of it for another comment: !important does override inline styles.

Collapse
 
alohci profile image
Nicholas Stimpson

Actually I expect custom origins/cascade layers to eventually replace most !important use. But it'll be some years before we can use them.

Collapse
 
xowap profile image
Rémy 🤖

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.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

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.

Collapse
 
xowap profile image
Rémy 🤖

After seeing the title I was hoping so hard for this first paragraph to be there, thank you for making my dreams true.

Collapse
 
cguttweb profile image
Chloe

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.

Collapse
 
urielbitton profile image
Uriel Bitton • Edited

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.

Collapse
 
alvaromontoro profile image
Alvaro Montoro • Edited

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:

Collapse
 
urielbitton profile image
Uriel Bitton

Right i actually meant inline with !importa overrides styleseet !important

Collapse
 
ms314006 profile image
Clark

Hahaha, your "You are still here...", it is so cute 🤣