title says it all, just wondering if I should avoid the "!important " feature/hack because my Site need to support IE6 in this case

  • Need some refresh, what does "!important" do? Marcel almost 10 years ago

5 answers

kemie 247

!important is not a hack in itslef, but you're probably referring to using it to feed ie6 specific rules, right?

I'd just say use it with caution, be aware of potential rule conflicts, but no reason to avoid it.

Answered almost 10 years ago by kemie

Well, !important is a valid CSS property, but as kemie said, use it with caution. Here's the w3c's guide to how to use it: Assigning Property Values, Cascading, and Inheritance
In the paragraph after this link, they give the calculations for specificity of your selectors. To be honest, I usually try to jack up the specificity of a selector in order to make it work for edge cases (IE6, for example).
But, if you're using it to get around the min-height bugs, I think you're best sticking with !important. EG:

#myDiv {
  height: auto !important;

For browsers that can't figure out min-height, they'll default to the height. Once you stretch beyond the need for those pixels, the auto will take over, which works the same way as min-height does. This is an acceptable hack, but it is definitely a hack. If you're using the stylesheet to over-ride an inline style or something, that's more of the intended use.

Answered almost 10 years ago by NateDSaint
  • This is a hack only in the loosest sense of the word - although it is used specifically to trick IE6 into treating height as min-height, it is forward-compatible with IE7 and will not cause issues in any other browsers. Dustin Diaz discovered it and it is called the 'min-height fast hack' danwellman almost 10 years ago
  • But it is a hack, nonetheless. Like I said, it's an acceptable hack because it does not have negative impact on other browsers, but it's definitely a hack in that you're using !important for something it wasn't meant to do. We're just lucky that the browser doesn't assume that means make it auto and ignore the height declaration, because it is a duplicate parameter. NateDSaint over 9 years ago

Even better, don't support IE6! (tongue in cheek...) Seriously, IE6 has to die; it's lived WAAAAAY beyond its time.

Answered almost 10 years ago by tahdhaze09
danwellman 5600

!important is used to ensure that a particular style is used regardless of other overriding selectors with higher specificity, for example, a style specified using the style attribute of an element always overrides a style set using CSS so if you have the following style rule:

p { color: black; }

and the following HTML:

<p style="color:red;">

The <p> will be red and not black. However, if you change the CSS so that it is:

p { color:black !important; }

Then the paragraph element will be black

Answered almost 10 years ago by danwellman
Rob 230

Use "conditional comments" so you can restrict what you write to IE6 and not have to worry about how it affects other browsers. Don't use hacks.

Answered almost 10 years ago by Rob
  • Avoiding hacks is probably a good idea, but !important is not a hack. It is valid CSS. Abinadi Ayerdis almost 10 years ago
  • Hacks are ok if they should affect IE6! ;) Takes alot less time to support that crap. ;) Marcel almost 10 years ago
  • If anything, conditional comments are a hack by Microsoft to avoid standards compliance. NateDSaint almost 10 years ago
  • I never said !important is a hack. I said use conditional comments and avoid hacks. Yes, conditional comments are proprietary to Microsoft, and that's why they're useful to help fix that turd of a browser but, as I said, then you don't have to worry about that markup affecting other browsers like straight hacks will. Rob almost 10 years ago
  • Conditional comments are not a hack implemented by MS to avoid standards compliance. MS avoids standards compliance because they disagree with the standards (and because they are powerful enough to get away with it). Obviously this is wrong, but think of how much harder it would be to create a web site if conditional comments did not exist ;) danwellman almost 10 years ago