Tutorial Categories:

HTML/CSS JavaScript/AJAX Server-Side Marketing General Comp-Sci

Conditional Comments for Specifying Styling for IE Only

By Justin Poirier

When HTML comments have special code in the opening and closing tags that we will describe below, they are actually read in whole or part by IE version 5 and up. The code within the opening tag is always read, and specifies which of these browsers should continue reading the contents of the comment.

If the pair of hyphens appearing in both the opening and closing tags of HTML comments are removed, it will cause a syntax error in versions of IE before 5, by which these versions will ignore the tag, causing the contents of the comment to be read.

The two facts described above provide a technique for specifying a separate stylesheet for any number of versions of IE (versions below 5 are treated as one version). The stylesheets, or link elements referencing them, should all appear in each relevant HTML document. Each should be inside its own comment. For versions of IE below 5, it should be indicated whether a given stylesheet applies or doesn't apply by respectively omitting or not omitting the hyphens in the comment tags. For each version after 5, whether a given stylesheet applies will be defined by the code in the opening comment tag. The code appears right after the hyphens and is in the following format:

[if optionally, an operator IE optionally, a value]

The value specifies a specific version number. If no value is given, all versions of IE after 5 is implied. The operator specifies how to compare the user's browser with the version(s) of IE specified by the value or lack thereof. Possible operators include lt (less than) and gt (greater than) which assume the user has some version of IE and compares its version number with the value; ! (not); and several others. If no operator is specified the comparison tests for equality.

The code in the closing tag of a comment used as we've described should appear right before the two hyphens, and consists of the string:


These comments are called conditional comments.