Conditional Comments: Targeting Internet Explorer

Microsoft introduced conditional comments with version 5 of Internet Explorer for Windows, as a way of presenting a richer user experience to users of their browser.

A conditional comment is a HTML comment with a condition structure that evaluates the version of Internet Explorer and compares it to the version number specified within the condition:

If the condition evaluates to true then the instructions within the comment are executed. In this way code can be targeted toward specific versions of IE (“downlevel-hidden”).

<!--[if IE 5]> instructions targeted at IE5 <![endif]--> <!--[if gte IE 6]> instructions targeted at IE6 or later <![endif]-->

Only IE5+ for Windows understands this structure, so all other browsers treat the contents between the opening and closing comment tags as a comment block and ignore it.

Microsoft also added a “downlevel-revealed” conditional comment that ‘lesser’ browsers would not ignore:

<![if !IE 5]> instructions intended for browsers other than IE5 <![endif]>

But this is not a properly formed HTML comment, and it will prevent pages from validating.

Endnote

Although Microsoft introduced conditional comments as a way of delivering a richer user experience to IE, many people now use them as a way of targeting that browser's shortcomings.

It is therefore ironic that the Microsoft documentation refers to browsers that do not understand these comments as being ‘down-level’. That may have been true when IE5 introduced conditional comments into the web developer's toolbox, but it's hardly the case now.