Conditional Comments

I talk about about using con­di­tional com­ments to serve a stylesheet spe­cific­ally aimed at resolv­ing Inter­net Explorer unique inter­pret­a­tion of CSS, how­ever, until recently this blog was more a case of do what I say, not do what I do. In the rush the to get the blog up and run­ning I used a couple of hacks, mainly the !import­ant hack to serve dif­fer­ent val­ues to IE and other browsers.

After read­ing Tantek Celik’s Pandora’s Box (Model) of CSS Hacks And Other Good Inten­tions I decided it was about time I prac­tised what I preached. Con­di­tional com­ments are the best way to serve CSS spe­cific­ally to IE. They spe­cific­ally tar­get IE only, all other browsers see the con­di­tional com­ment as a com­ment and treat it as such. So it a clean, effi­cient way, that does not require javas­cript or hacks (that might be inter­preted dif­fer­ently by another browser) and is valid HTML (It is a com­ment to every other browser except IE).

For those of you who have not seen a con­di­tional com­ment in action, here is the code from this page:


<!-- for microsoft browsers -->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="/xtras/ieonly.css" media="screen" />
<![endif]-->

Added Decem­ber 19: Warn­ing, do not cut and paste this code into your HTML. Word­Press does not handle code the way I (or prob­ably you ) would expect, so get it from this text file (right click and save as).

So all ver­sions of IE get the same addi­tional stylesheet. If I need to serve dif­fer­ent stylesheets to IE6 and IE5.5 and below, because IE5.5 and below uses incor­rect box model while IE6 in standards-compliant mode uses the same box model as every­body else. So the code in your head sec­tion of the page would include.


<!-- for IE6 only -->
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="/xtras/ie6only.css" media="screen" />
<![endif]-->
<!-- for IE 4, 5 & 5.5 -->
<!--[if lt IE 6]>
<link rel="stylesheet" type="text/css" href="/xtras/old.css" media="screen" />
<![endif]-->

Added Decem­ber 19: Warn­ing, do not cut and paste this code into your HTML. Word­Press does not handle code the way I (or prob­ably you ) would expect, so get it from this text file (right click and save as).

For more on con­di­tional com­ments visit the MSDN Lib­rary.

Updated 10 Decem­ber just mod­i­fied /wp-includes/functions-formatting.php file so — renders as — and not as &#8211

Updated 7 March 2006 typo I missed, and it was a big one, thanks Jen Williams

12 Responses to “Conditional Comments”

  1. Nick Cowie » IE7 beta2 Says:

    […] I was pleas­antly sur­prised, other than this blog, all the other sites are work­ing fine. Work­Life Bal­ance which uses a png back­ground to get the semi trans­par­ent menu bar in all other browsers except IE6 and below. Which gets feed by con­di­tional com­ments an addi­tional stylesheet which replaces the png with a solid back­ground and then uses a IE only fil­ter to reduce the opa­city of that div. IE7 beta2 flew through that site and most of the other sites built in the last 15 months or so. Because those sites feed IE6 and below an addi­tional stylesheet by con­di­tional com­ments and IE7 gets the same style sheets as Fire­fox, Safari, Opera and every­body else and appears to behave as a good CSS citizen. […]

  2. Erde Says:

    Thanks for this code.

  3. CableGuy Says:

    Handy little code. Thanks

  4. Ottilie Says:

    Thank you very much for the great code. It work very well. Very help­ful article.

  5. The Happy Rock Says:

    Thanks a ton. I was pulling my hair out try­ing to get my con­di­tional com­ment to work. I finally tried you text file cut and paste, and it worked. Even though the code was exactly the same before. I really don’t under­stand what happened, but oh well I guess. 2 hours wasted!

    Thanks again

  6. Tim Says:

    Thanks so much from me too, worked out pretty good for me

  7. joern Says:

    very good code!
    thanx

  8. Computer Freak Says:

    Per­son­ally, I dont like Con­di­tional Com­ments. There are enough hacks to make pages work in all browsers, so I had never to use them.

    But non­ethe­less a nice post and well explained :)

  9. André Says:

    I agree to “Com­puter Freak” — I mis­like the idea of con­di­tional hacks at all… But if there´s no wayto avoid them, this peace of code may solve a prob­lem nicely

  10. Web Mixed Grill » Blog Archive » Opacity vs RGBA - Transparency with CSS Says:

    […] of course is not valid CSS, so use it and ignore the val­id­ator or hide it behind con­di­tional com­ments (up to you, but you get the desired res­ults). Also it looks like the ele­ment needs to HasLay­out for […]

  11. Sticker Says:

    hi, i tested it its still work­ing and helped me a lot. go on like that.
    greet­ings tom sticker

  12. Nick Says:

    Thanks a ton. I was pulling my hair out try­ing to get my con­di­tional com­ment to work.

Affiliates

Google
text advertising by
Powered by Reseller Zoom