In CSS, the “!important” suffix was originally intended to provide a method of overriding author stylesheets. Users could define their own “user stylesheets” and could use this suffix to give their rules precedence over the author’s (website creator’s) styles.
Unfortunately, and quite predictably, its usage has spread massively, but not in the right direction. Nowadays, it’s used to counteract the pain of having to deal with CSS specificity, otherwise known as the set of rules which dictate that “div h1 a” is more specific selector than “div a”. Most people that use CSS on a daily basis don’t know enough about CSS specificity to solve their problems without using the “!important” suffix.
For reference, “specificity” is the name given to signify the precedence of a particular CSS selector. Here’s a quick rundown:
* {} /* a=0 b=0 c=0 -> specificity = 0 */ LI {} /* a=0 b=0 c=1 -> specificity = 1 */ UL LI {} /* a=0 b=0 c=2 -> specificity = 2 */ UL OL+LI {} /* a=0 b=0 c=3 -> specificity = 3 */ H1 + *[REL=up]{} /* a=0 b=1 c=1 -> specificity = 11 */ UL OL LI.red {} /* a=0 b=1 c=3 -> specificity = 13 */ LI.red.level {} /* a=0 b=2 c=1 -> specificity = 21 */ #x34y {} /* a=1 b=0 c=0 -> specificity = 100 */ /* Source: http://www.w3.org/TR/CSS2/cascade.html */ |
The specificity is calculated as follows:
- count the number of ID attributes in the selector (= a)
- count the number of other attributes and pseudo-classes in the selector (= b)
- count the number of element names in the selector (= c)
- ignore pseudo-elements.
(read more about it: http://www.w3.org/TR/CSS2/cascade.html#important-rules)
The power of CSS and its cascading nature, in my opinion, is only fully appreciated when one understands specificity. A lack of understanding only leads to bad practices, such as using the “!important” suffix. Of course, it’s not always a bad idea, sometimes it’s necessary; for example, the Google toolbar in browsers always insists on colouring certain input fields yellow to signify that an ‘autocomplete’ feature is available. To be blunt, this can make your design look crap! The only way to counteract this is to define an “!important” style, i.e. the only thing more specific than inline styles.
Apart from a few edge cases its use is, in my opinion, unfounded and mostly unnecessary. Not to mention the fact of how annoying it is for a user when they want to apply their own styles or enhancements; you better have a damn good reason for relinquishing such a rudimentary level of control!
The only thing that can override an “!important” rule is another one, which must be specified lower down and its selector must have the same (or a higher) level of specificity. You can also specify “!important” styles inline. Another method would be to remove the rule entirely, this can be achieved with JavaScript, by accessing and manually removing the rule in question: http://dev.opera.com/articles/view/dynamic-style-css-javascript/#addingandremovingrules.
Even though there are solutions, it is still a massive pain for users, so please, don’t use “!important” unless you have no other option!
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
The precedence is well explained with those Star Wars characters:
http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html
Sometimes, you can’t escape from the !important (min-height on IE6) but of course, you need to know what you’re doing.
I’ve had to use !important on ASP.NET websites that use custom controls that come packaged with their own CSS.
The control framework inserts markup requesting the control CSS after any CSS you’ve referenced in the head section (whether it’s by referencing a stylesheet or inline CSS – eugh 🙂 ).
The only way to override the controls’ CSS is by using important. It’s not something that I like doing, and this is a specific ASP.NET example, but it’s where I have had to use !important.
I agree about specificity. I use !important *only* to counteract very few FF/IE7/Safari/Chrome vs. IE6 issues when a CSS reset doesn’t seem to fix the inconsistency. I’ve never had problems with this method.
E.g.
margin: 2px 0 0 3px !important; /* good browsers */
margin: 7px 0 0 3px; /* IE6 only */
@Marin, Mark, I guess that’s another exception. Of course, you could always do something more hacky to target IE6, like:
I’m not sure which approach is better… ‘!important’ may be the better option here, to be honest.
@Shane, that really sucks about .NET. Can’t those “controls” be changed or configured somehow?
The CSS is packaged up in the control – you add a reference to your control and the framework automatically puts in the CSS reference after any you’ve put in.
No – in this particular instance, there’s no way around.
NICE! I couldn’t agree with you more!
Good advice. Unfortunately Sitepoint’s recent “10 fixes that solves IE6 problems” article specifically promoted the use of !important. One step forward, two steps back.
Hi James, great site!
Sometimes !important can be useful for overriding dynamically added inline styles (e.g. like when changing CSS rules with jQuery). That’s about the only time I ever use it, I never put it into a stylesheet 😀
Wouldn’t it be better to do the star hack?