透過微軟獨有的條件註解,可以針對特定 IE 版本套用樣式:
<!--[if ie]> 所有 Windows 內建的 IE 都可識別 <![endif]-->
<!--[if ie 5.0]> 只有 IE5.0 可識別 <![endif]-->
<!--[if gt ie 5.0]> IE5.0 以上(含 5.5)可識別 <![endif]-->
<!--[if lt ie 6]> IE6 以下可識別 <![endif]-->
<!--[if gte ie 6]> IE6 及以上可識別 <![endif]-->
<!--[if lte ie 7]> IE7 及以下可識別 <![endif]-->
<!--[if gte ie 7]> IE7 及以上可識別 <![endif]-->
<!--[if ie 8]> 僅 IE8 可識別 <![endif]-->
<!--[if ie 9]> 僅 IE9 可識別 <![endif]-->
其中 if 後搭配 lt、gte 等運算子會有不同效果:
<!--[if ie 8]>:僅 IE8 可識別<!--[if lt ie 8]>:IE7 或更低版本<!--[if gte ie 8]>:IE8 及以上版本實例一:讓 IE6–IE9 顯示不同內容
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>div if 條件實例</title>
</head>
<body>
你正在使用:
<!--[if ie 7]> <h2>ie7</h2> <![endif]-->
<!--[if ie 6]> <h2>ie6</h2> <![endif]-->
<!--[if ie 8]> <h2>ie8</h2> <![endif]-->
<!--[if ie 9]> <h2>ie9</h2> <![endif]-->
<br><br>
<strong>說明</strong>:瀏覽器版本為多少,即會顯示對應的 ie 數字,可針對 IE6–IE9 測試
</body>
</html>
實例二:讓 IE6–IE9 顯示不同 CSS 樣式
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>css if 條件 hack 實例</title>
<!--[if ie 6]> <style type="text/css"> .divcss { color: #f00; } </style> <![endif]-->
<!--[if ie 7]> <style type="text/css"> .divcss { color: #ff0; } </style> <![endif]-->
<!--[if ie 8]> <style type="text/css"> .divcss { color: #00f; } </style> <![endif]-->
<!--[if ie 9]> <style type="text/css"> .divcss { color: #000; } </style> <![endif]-->
</head>
<body>
<div class="divcss">
div css 實驗提示:<br>
我在 ie6 下是紅色,在 ie7 下是黃色,在 ie8 下是藍色,在 ie9 下是黑色
</div>
</body>
</html>
以上範例僅針對 IE6–IE9 的 if hack 進行實驗。