CSS Hacks

Syntax
The syntax for conditional comments is as follows:

Positive
<!–[if condition]> HTML <![endif]–>
Negative
<!–[if !condition]><![IGNORE[–><![IGNORE[]]> HTML <!–<![endif]–>
condition is one of the following:

IE
Any version of IE
lt IE version
Versions less than version
lte IE version
Versions less than or equal to version
IE version
Only version version
gte IE version
Versions greater than or equal to version
gt IE version
Versions greater than version
version is the version of Internet Explorer, typically 5, 5.5, 6, or 7

HTML is the HTML to be included if the condition does or doesn’t match, depending on the type of conditional comment. When included, the HTML is placed right where the conditional comment is in the source.

For negative conditions, <![IGNORE[–><![IGNORE[]]> can be replaced with –> if the condition is simply IE. The longer version is only needed when Internet Explorer might parse the contents.

The <![IGNORE[ … ]]> directive is not available in XML, so it is illegal to use it in XHTML. A solution would be to split it up into two special conditional comments: <!–[if !condition]> XHTML <![endif]–> <!–[if !IE]>–> XHTML <!–<![endif]–> where XHTML is the same both places. Note that Internet Explorer 7 and below don’t yet recognize XHTML as a form of XML, so this is merely forward-looking.

Fixing stand-alone versions of Internet Explorer
Internet Explorer was not designed to allow multiple versions to be installed at once, and Microsoft doesn’t officially support any such configurations. If you use one of the hacked third party packages that attempts to do this, you will experience problems with version-specific conditional comments, among other things. This is because the different stand-alone copies still rely on a common centralized registry for certain data, including version information.

Although there is no simple way to cut through all of the issues with stand-alone versions of Internet Explorer, it is possible to force them to look elsewhere for their version information, thus fixing this issue with conditional comments. The trick is to remove the normal centralized version indicator. To do this, first open up regedit.exe from the “Run…” dialog. Navigate to HKEY_LOCAL_MACHINE/Software/Microsoft/Internet Explorer/Version Vector/ (If HKEY_LOCAL_MACHINE doesn’t exist, try HKLM instead). In the right pane, you should see a row with a Name value of IE. Rename this by clicking on it and changing it to zIE (or anything unique and different). Restart Internet Explorer to see the effects. Now when it looks for the IE key for its version information, the key will be missing and it will be forced to determine the correct version number from its own module.

Stand-alone versions of Internet Explorer have a number of other issues, and it therefore may be better to instead use a separate virtual machine for each version of Internet Explorer to ensure that what you see is what your users will see. I recommend VMware Server, which is completely free of charge and fairly easy to set up.

Conditional comments as a CSS hack
Conditional comments can be used as a CSS hack by including links to stylesheets based on the layout engine. Here is an example of how stylesheets can be separated in this way:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html lang=”en”>
<head>
<title>Test</title>
<link href=”all_browsers.css” rel=”stylesheet” type=”text/css”>
<!–[if IE]> <link href=”ie_only.css” rel=”stylesheet” type=”text/css”> <![endif]–>
<!–[if lt IE 7]> <link href=”ie_6_and_below.css” rel=”stylesheet” type=”text/css”> <![endif]–>
<!–[if !lt IE 7]><![IGNORE[–><![IGNORE[]]> <link href=”recent.css” rel=”stylesheet” type=”text/css”> <!–<![endif]–>
<!–[if !IE]>–> <link href=”not_ie.css” rel=”stylesheet” type=”text/css”> <!–<![endif]–>
</head>
<body>
<p>Test</p>
</body>
</html>

http://www.javascriptkit.com/dhtmltutors/csshacks.shtml