465

What does !important mean in CSS?

Is it available in CSS 2? CSS 3?

Where is it supported? All modern browsers?

5

5 Answers 5

437

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
  • 276
    It is also confusing for many developers as in many programming languages the prefix ! means not.
    – rustyx
    Apr 1, 2014 at 14:33
  • 22
    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.
    – Noumenon
    Apr 29, 2014 at 10:29
  • 1
    Officially the W3 calls it a "rule".
    – JD Smith
    May 30, 2014 at 16:46
  • 7
    at least it's not sarcastic and says important! (important NOT) Oct 19, 2016 at 0:24
  • 2
    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.
    – jlguenego
    Nov 3, 2017 at 13:58
142

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
  • 1
    IE4+, actually, with bugs, up to and including 6.
    – BoltClock
    Feb 12, 2012 at 10:41
  • 20
    The confusion happens as ! is a symbol for NOT in some languages but it's clearer now.
    – Si8
    Mar 13, 2014 at 21:00
  • 2
    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. Oct 9, 2015 at 1:00
  • 4
    @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. Sep 7, 2018 at 17:13
25

!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
  • 5
    !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.
    – BoltClock
    Feb 12, 2012 at 10:40
15

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:

1
8

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.