What are the implications of using "!important" in CSS?
A little late to this question but it's saying "no matter what other styles are applied, ignore them and use this one". You may find it a little confusing with the !
infront (of the !important
) because thats a not operator in javascript but in css it's called a delimiter token that just says to take priority. Heres an example.
Without !important
:
.set-color{
color: blue;
}
.set-color{
color: red;
}
outputs RED
!important
on bottom attribute (of same)
.set-color{
color: blue;
}
.set-color{
color: red !important;
}
outputs RED (would have been red anyways)
!important
on top attribute (of same)
.set-color{
color: blue !important;
}
.set-color{
color: red;
}
outputs BLUE (says ignore red, use blue)
Yes, I'd say your example of using !important
is bad practice, and it's very likely it would cause undesired effects further down the line. That doesn't mean it's never okay to use though.
What's wrong with !important
:
Specificity is one of the main forces at work when the browser decides how CSS affects the page. The more specific a selector is, the more importance is added to it. This usually coincides with how often the selected element occurs. For example:
button {
color: black;
}
button.highlight {
color: blue;
font-size: 1.5em;
}
button#buyNow {
color: green;
font-size: 2em;
}
On this page, all buttons are black. Except the buttons with the class "highlight", which are blue. Except that one unique button with the ID "buyNow", which is green. The importance of the entire rule (both the color and font-size in this case) is managed by the specificity of the selector.
!important
, however, is added at a property level, not a selector level. If, for instance, we used this rule:
button.highlight {
color: blue !important;
font-size: 1.5em;
}
then the color property would have a higher importance than the font-size. In fact, the color is more important than the color in the button#buyNow
selector, as opposed to the font-size (which is still governed by the regular ID vs class specificity).
An element <button class="highlight" id="buyNow">
would have a font-size of 2em
, but a color blue
.
This means two things:
- The selector does not accurately convey the importance of all the rules inside it
- The only way to override the color blue is to use another
!important
declaration, for example in thebutton#buyNow
selector.
This not only makes your stylesheets a lot harder to maintain and debug, it starts a snowball effect. One !important
leads to another to override it, to yet another to override that, et cetera. It almost never stays with just one. Even though one !important
can be a useful short-term solution, it will come back to bite you in the ass in the long run.
When is it okay to use:
- Overriding styles in a user stylesheet.
This is what !important
was invented for in the first place: to give the user a means to override website styles. It's used a lot by accessibility tools like screen readers, ad blockers, and more.
- Overriding 3rd party code & inline styles.
Generally I'd say this is a case of code smell, but sometimes you just have no option. As a developer, you should aim to have as much control over your code as possible, but there are cases when your hands are tied and you just have to work with whatever is present. Use !important
sparingly.
- Utility classes
Many libraries and frameworks come with utility classes like .hidden
, .error
, or .clearfix
. They serve a single purpose, and often apply very few, but very important, rules. (display: none
for a .hidden
class, for example). These should override whatever other styles are currently on the element, and definitely warrant an !important
if you ask me.
Conclusion
Using the !important
declaration is often considered bad practice because it has side effects that mess with one of CSS's core mechanisms: specificity. In many cases, using it could indicate poor CSS architecture.
There are cases in which it's tolerable or even preferred, but make sure you double check that one of those cases actually applies to your situation before using it.
I think it is important that we touch on this again, here at the end of 2014 when more rules are being added into the working draft, it is so important not to impose restrictions upon your users. I have wrtten this small example to explain a possible scenario in which such a thing takes place. This is taken from a REAL website I have visited on the web, and I see it, sadly, more often than not.
Form Text Editor Fields
You have a website, and your website depends on filling out forms and editing text. To try to minimize eye strain you try to go with a style you think everyone will be okay with, and since your users mainly visit at night, you decide to make your background color to be white, and then make the text color to be black. The form is blank by default, so you type in text (this time) to make sure it works:
...
background-color: black; /* I forgot important here, but it works fine on its own */
color: white !important;
...
A few months later, users complain that white-on-black is too eye straining but the other half love it, what do you do? you add a custom theme that uses !important to override the internal styles so that BOTH parties are happy, which is what it is SUPPOSED to be used for:
...
background-color: white !important;
color: black !important;
...
Now, what happens here? What did you expect. If you properly remember the !important tags in the first set, you would have noticed it didn't work and probably remembered that you needed to delete the !important tags off. BUT you forgot one..
RIGHT, you get white text on white background, and the user can no longer read your webpage at all if they try to use this new style (assuming you kept it).
Of course, You don't realize this because the textbox is empty. And You ASSUME that it will work! (Assumption is a developer's worst enemy, it's right up there with pride and arrogance).
Make and Follow Self Implied Rules
So, the basic rule should be only use !important when designing OVERRIDES to an original complete set of css rules. Remember that it is meant for exceptions and disturbs the natural order and meaning of css in the first place. In MOST cases you will not need to use it at all.
Know How Users Customize Their Colors
With the existence of such tools as extensions and the presence of sites like 'userstyles.org' and it's stylish counterpart, you run the risk of alienating your users by using the !important tag at all! Keep in mind that stylish will not override them.
Never Restrict Your Visitors
If you use !important on a style, make sure you make it possible for the user to turn that style off (and i dont mean via the web browser's internal 'on/off' switch). And for heavens sake don't make it DEFAULT.
Ads
People are less and less using stylish for ad removal, so I don't think protecting ads with !important really is an issue here. It will just annoy someone trying to customize your site.
!important
forces the statement to always apply, doing these things:
- even if the selector is less specific and lower level, it now beats higher specificity selectors
- if there are further occurrences of that statement that would normally override that one, it does not override the important statement any more
Most of the time, !important
can be avoided because specificity of selectors handles which one takes effect, which is the idea of cascading styles. However, there are some situations (can't quite remember the exact one) where the specificity isn't that logical and I have to force !important
on the statement.
Reasons not to use/avoid !important
?
- prevents users from using custom stylesheets (which now can't override the rules that are marked as important) which breaks accessibility for some people
- makes code more difficult to read because the mind normally takes specificity quite easily, but remembering what is
!important
makes it harder to read