Nifty one-line IE6 CSS hack

Sometimes if there is no other way to fix UI issue we are forced to use CSS hacks. Even though they are not valid from W3C point of view, they simple do the job and save plenty of time.

In this article I will present my favourite CSS hack where we can specifically target IE6 or IE7 or any other browser.

Preview Code
.mySquare { width:100px; height:100px; background:red;
*background:green; _background:yellow;}
<div class="mySquare"></div>

And we're done! Code above displays:

  • Yellow square on IE6,
  • Green square on IE7,
  • Red square on all other popular browsers (Internet Explorer 8, Firefox, Chrome, Opera, Safari)

The beauty about this particular hack is that CSS is kept within one CSS property. You don’t need to look around or open separate CSS files to check if style was already applied for other browser somewhere else (like when you use <--[if IE 6]-->).

The down side - even though it fixes the problem this is not valid CSS so W3C validator will return error.

Did you like my article? Share it!
Share/Bookmark

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <csharp>, <css>, <html>, <javascript>, <vb>, <vbnet>, <xml>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.