471

What does!important mean in CSS?

Is it available in CSS 2? CSS 3?

Where is it supported? All modern browsers?

SOLO's user avatar
SOLO
8789 silver badges19 bronze badges
askedFeb 12, 2012 at 0:32
Itay Moav -Malimovka's user avatar
5

6 Answers6

442

It means, essentially, what it says; that 'this is important, ignore subsequent rules, and any usualspecificity issues, applythis rule!'

In normal use a rule defined in an external stylesheet is overruled by a style defined in thehead 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 thehead or the external stylesheet) it willstill override the less-specific selector (in-line style attributes willalways override the 'more-', or the 'less-', specific selector as it'salways 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's user avatar
DavidRR
19.8k27 gold badges114 silver badges202 bronze badges
answeredFeb 12, 2012 at 0:35
David Thomas's user avatar
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 ! meansnot.
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 W3calls it a "rule".
at least it's not sarcastic and saysimportant! (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 havethe rules you feel are most crucial always be applied. A rule that hasthe !important property will always be applied no matter where thatrule 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 countingspecificity)

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's user avatar
Westy92
21.6k6 gold badges78 silver badges65 bronze badges
answeredFeb 12, 2012 at 0:39
nicosantangelo's user avatar

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 asimportant!, or maybeIMPORTANT!, 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.

answeredFeb 12, 2012 at 0:36
Cyclone's user avatar

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

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:

answeredFeb 12, 2012 at 0:40
Fabian Barney's user avatar

1 Comment

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

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

answeredFeb 12, 2012 at 0:38
Don Roby's user avatar

Comments

-1

Before @layer was introduced, it was commonly used to override specificty and force a rule to be applied. It was, and is, confusing and can be difficult to control. With layered rules, which are supported by all major browsers, it becomes much much easier to control how rules are applied. If you're starting a new project you should definitely use layers. Converting to layers is very doable but non-trivial. I converted all my projects to use the layered approach, don't use !important at all and have found it extremely easy to confrol and understand. See the description athttps://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@layer

answeredNov 17 at 18:15
mike traveller's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.