XML technologies / CSS / CSS Cookbook / Hacks, workarounds and debugging / Conditional comments

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Give particular versions of Internet Explorer for Windows their own style definitions.

Solution

Use conditional comments to distinguish between browser versions:

<!--[if IE]>
  <p>Sie sehen diesen Satz, weil Sie Internet Explorer benutzen.</p>
<![endif]-->

To distinguish individual browser versions, extend the syntax above by specifying the version number for which the code is intended:

<!--[if IE 5]>
  <p>Sie sehen diesen Satz, weil Sie Internet Explorer 5 benutzen.</p>
<![endif]-->
<!--[if IE 5.0]>
  <p>Sie sehen diesen Satz, weil Sie Internet Explorer 5.0 benutzen.</p>
<![endif]-->
<!--[if IE 5.5]>
  <p>Sie sehen diesen Satz, weil Sie Internet Explorer 5.5 benutzen.</p>
<![endif]-->
<!--[if IE 6]>
  <p>Sie sehen diesen Satz, weil Sie Internet Explorer 6 benutzen.</p>
<![endif]-->
<!--[if IE 7]>
  <p>Sie sehen diesen Satz, weil Sie Internet Explorer 7 benutzen.</p>
<![endif]-->

If the code is intended for Internet Explorer 5 for Windows or later, use the following:

<!--[if gte IE 5]>
  <p>Sie sehen diesen Satz, weil Sie Internet Explorer 5 oder höher benutzen.</p>
<![endif]-->

If the code should run only in Internet Explorer 5.5 for Windows and earlier versions, write the following:

<!--[if lte IE 5.5]>
  <p>Sie sehen diesen Satz, weil Sie Internet Explorer 5.5 oder niedriger benutzen.</p>
<![endif]-->

Target versions earlier than Internet Explorer 6 for Windows as follows:

<!--[if lt IE 6]>
  <p>Sie sehen diesen Satz, weil Sie Internet Explorer in einer Version kleiner als 6 benutzen.</p>
<![endif]-->

Discussion

Microsoft developed its own system based on special HTML comments to display HTML content tailored to particular versions of Internet Explorer for Windows. Because other browsers do not interpret HTML comments, this is an extremely effective and safe way to separate CSS for Internet Explorer and its various versions from general definitions.

Because conditional comments are based on HTML comments, you can insert only HTML. This makes it possible to define particular CSS instructions specifically for different Internet Explorer browsers. The following code loads a separate stylesheet for Internet Explorer 5.x by inserting a separate tag between the appropriate conditional comments: link.

<link rel="stylesheet" type="text/css" media="screen, presentation" href="/_assets/css/screen/screen.css" />
<link rel="stylesheet" type="text/css" media="aural" href="/_assets/css/aural.css" />
<!--[if lt IE 6]>
  <link rel="stylesheet" type="text/css" media="screen, presentation" href="/_assets/css/screen/ie.css" />
<![endif]-->

Internal stylesheets can also be defined for particular versions this way:

<!--[if lt IE 6]>
 <style type="text/css">
  h1 {
   font-family: Verdana, Arial, Helvetica, sans-serif;
   font-size: 36px;
  }
 </style>
<![endif]-->

Conditional comments can also be used with a “smart hacking system” (see the recipe A smart system for managing CSS hacks).

When using conditional comments, note the small but important difference between IE 5 and IE 5.0.

If your code should apply only to Internet Explorer 5.0, write IE 5.0. To target all version 5 releases (5.x), use IE 5.

The additional markers lt and gt mean “less than” and “greater than.” The markers lte and gte mean “less than or equal” and “greater than or equal,” respectively.

  

 

 

 

back to top