views:

57

answers:

1

Does !important not works in IE6 ? If we need IE6 compatibility too then shouldn't we use !important?

+1  A: 

IE6 supports !important when the rule with !important is the last rule for that property within a selector.

This sounds a little confusing, but if you declare an !important height, it must be the last declaration for 'height' within a given selector.

As such consider these examples:

#selector { height: 100px; height: 150px !important; }
Result: All browsers: 150px;

#selector { height: 100px; height: 150px !important; }
td#selector { height: 200px; }
Result: All browsers: 150px;

#selector { height: 100px; height: 150px; }
td#selector { height: 70px; height: 200px !important; }
Result: All browsers: 200px;

#selector { height: 100px !important; height: 150px; }
Result: All modern browsers but ie6: 100px; ie6: 150px;

#selector { height: 100px !important; height: 150px; }
td#selector { height: 200px; }
Result: All modern browsers but ie6: 100px; ie6: 200px;

#selector { height: 100px; height: 150px; }
td#selector { height: 70px !important; height: 200px; }
Result: All modern browsers but ie6: 70px; ie6: 200px;

What ie6 does is parse the 'height' value for each selector as the last 'height' declaration present within the selector (other rules may apply but I believe this is your standard case). Then it selects the most specific 'height' out of all of these based on !important and selector specificity rules. It effectively ignores the 'height' declarations that are not the last within their selector.

Other browsers instead will take into account '!important' when parsing the 'height' value for each selector, before comparing selectors based on !important and specificity rules.

One "benefit" of this is that every other reasonable browser will use your "important" style, while ie6 will pick the last rule declaration within that selector.

You would be much better off with an ie6 specific stylesheet though unless there is only a very small number of ie6 tweaks and you want to css comment each one as an ie6 exploit.

Mental Example

Imagine picking a CSS rule is a matter of 1) getting all the rules that match the element and 2) deciding which of those rules to use. Pretend the inline style tag is just another selector unless I mention otherwise.

In every other browser if you want to get the 'height' for an element it does this approximately:

  1. For each selector choose the last !important height if there is one, otherwise just the last height.

  2. Pick the most specific selector where the selected 'height' is an !important height, otherwise the inline style height, otherwise the most specific selector.

In ie6 it does this approximately:

  1. For each selector choose the last height.

  2. Pick the most specific selector where the selected 'height' is an !important height, otherwise the inline style height, otherwise the most specific selector.

Graphain
This is slightly inaccurate. It works _as long as it's not the last rule declared for that property_ in this selector. In this example, reverse the two `height` rules and you'll see that, indeed, the height is `100px` (even if a different selector would normally have overridden it).
John Feminella
Actually, that has more to do with the way that the rules are getting interpreted -- the last rule typically wins (lots of hacks are based on this principle).
Mike Tierney
Thanks John - I think I assumed as much but was just copying the Googled answer. I've updated my answer to reflect and might clarify it a little more.
Graphain
@Graphain - what about cascading and overriding on other rules's properties. which are in different selectors but nested.
metal-gear-solid
My example already covered that. I added a further clarification of how it selects the rule. Please let me know if this clears things up. Basically there are two issues: 1) which rule is picked per selector and 2) which selector is picked (which is based on 1.) 2) is the same for all browsers, but 1) differs for ie6 (it doesn't always pick the !important rule for a selector which means that selector may not get picked).
Graphain