Print this page

Internet Explorer Conditional Statements

Posted on Monday, 17th December 2007.

Sometimes it is necessary to reveal elements of XHTML to Microsoft Internet Explorer to fix issues that it has with (as an example) CSS and JavaScript. Internet Explorer recognises conditional comment tags and dictates logic based on the version of IE. An example of a basic conditional comment tag is:

<!--[if IE]> Display this text if the visitor’s browser is IE <![endif]-->

As you can see, the above code block looks like standard XHTML comments; therefore it is simply ignored by Firefox and Safari. These tags also allow you to detect particular versions of IE. See below for some examples:

<!--[if IE 5]> Display this text if the visitor’s browser is IE version 5 <![endif]--> <!--[if IE 5.5]> Display this text if the visitor’s browser is IE version 5.5 <![endif]--> <!--[if lt IE 7]> Display this text if the visitor’s browser is less than IE 7 <![endif]--> <!--[if gte IE 5]> Display this text if the visitor’s browser is greater
than or equal to IE 5 <![endif]--> <!--[if lte IE 6]> Display this text if the visitor’s browser is less than
or equal to IE 6 <![endif]-->

This code is particularly handy if you want to override styles for IE 6 for example:

<!--[if IE 6]> <style type=”text/css”> @import “css/ie6.css”; </style> <![endif]-->

Back to Web Development Articles.