471

What does !important mean in CSS?

Is it available in CSS 2? CSS 3?

Where is it supported? All modern browsers?

SOLO
8789 silver badges19 bronze badges
asked Feb 12, 2012 at 0:32
5

5 Answers 5

442

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

DavidRR
19.7k27 gold badges114 silver badges202 bronze badges
answered Feb 12, 2012 at 0:35
Sign up to request clarification or add additional context in comments.

11 Comments

It is also confusing for many developers as in many programming languages the prefix ! means not.
One purpose for !important would be in a GreaseMonkey script where you are purposely overriding other people's CSS that's likely more specific than yours.
Officially the W3 calls it a "rule".
at least it's not sarcastic and says important! (important NOT)
You wrote : 'In normal use a rule defined in an external stylesheet is overruled by a style defined in the head of the document'. It is wrong.
|
144

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.

Westy92
21.6k6 gold badges78 silver badges65 bronze badges
answered Feb 12, 2012 at 0:39

4 Comments

IE4+, actually, with bugs, up to and including 6.
The confusion happens as ! is a symbol for NOT in some languages but it's clearer now.
I'm especially glad that you included the syntax for using !important. CSS is different enough from other languages that it's easy to forget how to use certain things.
@Si8 - Yes, because of that confusion, I've always thought "they" should have defined it as 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.
26

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

answered Feb 12, 2012 at 0:36

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

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:

  1. browser styles
  2. user style sheet declarations (without !important)
  3. author style sheet declarations (without !important)
  4. !important author style sheets
  5. !important user style sheets

After that specificity takes place for the rules still having a finger in the pie.

References:

answered Feb 12, 2012 at 0:40

1 Comment

As @fabian-barney pointed out !important is a modifier for the cascading order developer.mozilla.org/en-US/docs/Web/CSS/Cascade (see the table for reference).
9

It changes the rules for override priority of css cascades. See the CSS2 spec.

answered Feb 12, 2012 at 0:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.