What does !important mean in CSS?
Is it available in CSS 2? CSS 3?
Where is it supported? All modern browsers?
-
4w3.org/TR/CSS2/cascade.html#important-rulesj08691– j086912012年02月12日 00:35:20 +00:00Commented Feb 12, 2012 at 0:35
-
29... something to avoid whenever possible.Scott– Scott2012年02月12日 00:38:18 +00:00Commented Feb 12, 2012 at 0:38
-
3possible duplicate of How do you read !important in CSS?, What are the implications of using "!important" in CSS? and also see: when to use !important property in css?, When to use "!important" to save the day (when working with CSS)Cody Gray– Cody Gray ♦2012年02月12日 11:36:21 +00:00Commented Feb 12, 2012 at 11:36
-
4Not important. (Web developer joke).Rounin– Rounin2021年03月14日 15:07:41 +00:00Commented Mar 14, 2021 at 15:07
-
With the introduction of CSS layers in near future, this can become be used less if the code is organized properly.m4n0– m4n02022年05月30日 07:20:25 +00:00Commented May 30, 2022 at 7:20
5 Answers 5
It means, essentially, what it says; that 'this is important, ignore subsequent rules, and any usual specificity issues, apply this rule!'
In normal use a rule defined in an external stylesheet is overruled by a style defined in the head of the document, which, in turn, is overruled by an in-line style within the element itself (assuming equal specificity of the selectors). Defining a rule with the !important 'attribute' (?) discards the normal concerns as regards the 'later' rule overriding the 'earlier' ones.
Also, ordinarily, a more specific rule will override a less-specific rule. So:
a {
/* css */
}
Is normally overruled by:
body div #elementID ul li a {
/* css */
}
As the latter selector is more specific (and it doesn't, normally, matter where the more-specific selector is found (in the head or the external stylesheet) it will still override the less-specific selector (in-line style attributes will always override the 'more-', or the 'less-', specific selector as it's always more specific.
If, however, you add !important to the less-specific selector's CSS declaration, it will have priority.
Using !important has its purposes (though I struggle to think of them), but it's much like using a nuclear explosion to stop the foxes killing your chickens; yes, the foxes will be killed, but so will the chickens. And the neighbourhood.
It also makes debugging your CSS a nightmare (from personal, empirical, experience).
11 Comments
important! (important NOT)The !important rule is a way to make your CSS cascade but also have the rules you feel are most crucial always be applied. A rule that has the !important property will always be applied no matter where that rule appears in the CSS document.
So, if you have the following:
.class {
color: red !important;
}
.outerClass .class {
color: blue;
}
the rule with the important will be the one applied (not counting specificity)
I believe !important appeared in CSS1 so every browser supports it (IE4 to IE6 with a partial implementation, IE7+ full)
Also, it's something that you don't want to use pretty often, because if you're working with other people you can override other properties.
4 Comments
! is a symbol for NOT in some languages but it's clearer now.!important. CSS is different enough from other languages that it's easy to forget how to use certain things.important!, or maybe IMPORTANT!, rather than !important. I wonder if anyone (who might read this) knows why they defined it with the punctuation in front? Obviously, it's way too late to change it now.!important is a part of CSS1.
Browsers supporting it: IE5.5+, Firefox 1+, Safari 3+, Chrome 1+.
It means, something like:
Use me, if there is nothing important else around!
Cant say it better.
1 Comment
!important isn't limited to Safari 3+ only; it has supported it from the very beginning like all other non-IE browsers. IE understands it from version 4 onward, but it only supports it bug-free starting from version 7.It is used to influence sorting in the CSS cascade when sorting by origin is done. It has nothing to do with specificity like stated here in other answers.
Here is the priority from lowest to highest:
- browser styles
- user style sheet declarations (without !important)
- author style sheet declarations (without !important)
- !important author style sheets
- !important user style sheets
After that specificity takes place for the rules still having a finger in the pie.
References:
1 Comment
!important is a modifier for the cascading order developer.mozilla.org/en-US/docs/Web/CSS/Cascade (see the table for reference).It changes the rules for override priority of css cascades. See the CSS2 spec.