30

Looking at the CSS specificity specification, there is no mention about how many "points" the !important rule is worth.

When does one override another? What happens if one is declared after the other? Which rule is declared to be more important? Is there some sort of pattern?

From the looks of it, !important applies to what has more specificity points to begin with. But what will happen if I declare a bazillion id's stacked with classes and nested deeply? Will it override the rules set in another, less specified rule marked with !important?

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
Zirak
  • 35,198
  • 12
  • 75
  • 89
  • 5
    Make sure you're writing `property: value !important`, and not `important!` (like in your question title) or `property: value; !important` (like in [your first demo](http://jsfiddle.net/wRqDb/1/)). In [your second demo](http://jsfiddle.net/wRqDb/2/), you wrote `div#green` which is looking for `
    `, which does not exist in your demo. *My point* is that you need to ensure you understand more basic issues before worrying about the nuances of `!important`.
    – thirtydot Apr 27 '11 at 14:20

2 Answers2

40

Specificity in CSS only concerns selectors, not their associated declarations. !important applies to a declaration, so it alone plays no role in specificity.

However, !important influences the cascade, which is the overall calculation of styles for a certain element when more than one of the same property declaration applies to it. Or, as Christopher Altman succinctly describes:

  1. !important is a spades card. It trumps all specificity points.

But not only that: because it influences the cascade overall, if you have more than one !important selector with a rule containing an !important declaration matching the same element then selector specificity will continue to apply. Again, this is simply due to having more than one rule applying to the same element.

In other words, for two rules with unequal selectors in the same stylesheet (e.g. same user stylesheet, same internal author stylesheet, or same external author stylesheet), the rules with the most specific selector apply. If there are any !important styles, the one in the rule with the most specific selector wins. Finally, since you can only have something that's either important or not, that's quite as far as you can go in influencing the cascade.

Here's an illustration of the various uses of !important and how they're applied:

  • The !important declaration always overrides the one without it (except in IE6 and older):

    /* In a <style> element */
    #id {
        color: red !important;
        color: blue;
    }
    
  • If there is more than one !important declaration in a rule with the same level of specificity, the later-declared one wins:

    /* In a <style> element */
    #id {
        color: red !important;
        color: blue !important;
    }
    
  • If you declare the same rule and the same property in two different places, !important follows the cascading order if both declarations are important:

    /* In an externally-linked stylesheet */
    #id {
        color: red !important;
    }
    
    /* In a <style> element appearing after the external stylesheet */
    #id {
        color: blue !important; /* This one wins */
    }
    
  • For the following HTML:

    <span id="id" class="class">Text</span>
    

    If you have two different rules and one !important:

    #id {
        color: red;
    }
    
    .class {
        color: blue !important;
    }
    

    That !important always wins.

    But when you have more than one !important:

    #id {
        color: red !important;
    }
    
    .class {
        color: blue !important;
    }
    

    The #id rule has a more specific selector, so that one wins.

Community
  • 1
  • 1
BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
  • 3
    Just to clarify your 3rd paragraph, "For two rules with unequal selectors...". Only if there is more than 1 rule, targeting the same element, with an `!important` declaration, will specificity come into play. Otherwise, the `!important` declaration will win every time, regardless of specificity. – MrWhite Mar 28 '13 at 15:26
  • I used to think that inline style attribute overrides all including the `!important` also. – techie_28 Jul 12 '16 at 05:18
8

The way I think about it is this:

  1. !important is a spades card. It trumps all specificity points. To your specific question, the !important will override a bazillion id/classes.

  2. !important resets the cascade. So, if you use a !important below another !important, the second instance rules.

There is a more technical answer out there, but that is how I think about !important.

One more note, if you are using !important you need to step back and check if you are doing something wrong. !important implies you are working against the cascade of CSS. You should use !important in rare cases.

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
Christopher Altman
  • 4,690
  • 2
  • 28
  • 48
  • 4
    Christopher Altman is correct. Looks like you have the !important incorrectly on the wrong side of the semicolon... also, your second selector is not selecting the div at all... it would just be inheriting from that selector which is why the first one is being used. – Mark Steggles Apr 27 '11 at 13:52
  • +1 for the final point here -- if you're using `!important` a lot then you're probably doing something wrong. – Spudley Apr 27 '11 at 14:09
  • 2
    @zirak - What about it? Your second CSS selector is looking for a div with id="green", which doesn't exist in your example. What exactly are you trying (and failing) to prove? – My Head Hurts Apr 27 '11 at 14:12
  • Fine, change the id#green to id#id. It'll not change to red. That's why I don't get it. – Zirak Apr 27 '11 at 14:24
  • If you add a space between the id and the class then it will change red. The reason it stays green is because `div#id.class` is more specific than `div#id`. [Here is a link](http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html) to a site that explains specifity quite well. I've seen it linked on Stack Overflow once before. – My Head Hurts Apr 27 '11 at 14:53