What does !important
mean in CSS?
Is it available in CSS 2? CSS 3?
Where is it supported? All modern browsers?
important means that “this is important”, ignore all the subsequent rules,apply ! important rule. The ! important keyword must be placed at the end of the line, immediately before the semicolon.
important is a bad CSS practice. It disrupts the natural flow in applying the css rules where in properties are applied from top to bottom. With ! important , the property will now give priority to the latest important value.
If for some particular reason you have to write the same property twice in the same declaration block, then add ! important to the end of the first one, the first one will have more weight in every browser except IE6 (this works as an IE6-only hack, but doesn't invalidate your CSS)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With