Conditional Comments

This article was first published on (09-01-2005).

One of the most common operations performed in a Web page is to detect the browser type and version. [...]. Conditional comments offer certain advantages over scripted browser detection techniques and are also easier to use.

About Conditional Comments

I don’t agree with the first part of this opening paragraph from the MSDN website, but I think the last bit is true, CC are more reliable than scripted techniques and much easier to use (read this though).

CC have been available since MSIE v5, but were not that much used in their early days. I believe it is CSS that gave them a real chance for prime time. I mean, this thing is a real swiss army knife in the web designer’s toolbox. Implementing fixes for MSIE 5 Broken Box Model and MSIE 6 bugs is a snap. They even help to hide some not-so-kosher CSS declarations from the Validator. What else could we ask? Microsoft may have built broken browsers, but we should give them credit for delivering a great tool to fix them 🙂

How it works

Conditional Comments contain inner HTML blocks, there are of two types:

Downlevel-hidden conditional comments:
Internet Explorer 5 and later download and render the HTML content inside the comment only if the expression is evaluated as true; other browsers ignore that block altogether.
Downlevel-revealed conditional comments:
These CC are the opposite of downlevel-hidden conditional comments, they allow you to serve content to other browsers than MSIE 5+ (Win).

For proper syntax as well as information on Version Vectors, read the MSDN article.

What you should know

Further reading

Check this article by Manfred Staudinger about standalone versions of MSIE.