Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does !important mean in CSS?

Tags:

css

What does !important mean in CSS?

Is it available in CSS 2? CSS 3?

Where is it supported? All modern browsers?

like image 749
Itay Moav -Malimovka Avatar asked Feb 12 '12 00:02

Itay Moav -Malimovka


People also ask

How do you say important in CSS?

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.

Why using important in CSS is bad?

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.

Where do you put important CSS?

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)


1 Answers

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).

like image 182
David Thomas Avatar answered Oct 02 '22 20:10

David Thomas