Conditional Comments

Date Published:
Last updated:

Back

Conditional comments are a special HTML comment syntax, introduced by Microsoft, that allows targeting of specific versions of Internet Explorer (on Windows). These proprietary tags will allow you to do specific things for users of Internet Explorer, whilst being safely ignored by other browsers (as they parse it like a standard HTML comment).

It is for this reason that they are truly useful in web development: when fixing IE-only bugs. All those CSS problems you had only in the Special Browser can now be solved by supplying a style sheet of IE fixes! This will make your standard CSS cleaner and probably help improve world peace.

Examples of the syntax

Note: the explanatory text in each example would be replaced with whatever you want your target to receive.

<!--[if IE]>
I target all IE versions
<![endif]-->

<!--[if !(IE 7)]>
I target all IE versions except IE7
<![endif]-->

<!--[if IE 7]>
I target IE7 only
<![endif]-->

<!--[if gt IE 7]>
I target all IE versions newer than IE7
<![endif]-->

<!--[if lt IE 7]>
I target all IE versions older than IE7
<![endif]-->

<!--[if gte IE 7]>
I target IE7 and all newer versions
<![endif]-->

<!--[if lte IE 7]>
I target IE7 and all older versions
<![endif]-->

<!--[if (gte IE 5) & (lt IE 8)]>
I target all IE versions from IE5 and up to (but not including) IE8
<![endif]-->

<!--[if (IE 6) | (IE 7)]>
I target either IE6 or IE7
<![endif]-->

Further reading and sources