針對不同瀏覽器/版本對 CSS 支持差異,編寫特定樣式代碼以實現統一頁面效果的技術手段。
*html、*+html)_、*、\9)<!--[if IE]>...<![endif]-->.box {
background: #0000FF; /* 所有瀏覽器 */
background: #00FF00 !important; /* IE7+、現代瀏覽器 */
*background: #FF0000; /* IE7 識別 */
_background: #FFFF00; /* IE6 識別 */
}
優先級:IE6 讀最後一行(不識別 !important),IE7 讀第三行,現代瀏覽器讀第二行。
/* IE6 only */
* html .box { color: red; }
/* IE7 only */
*+html .box { color: blue; }
/* IE6/IE7 共用 */
* html .box, *+html .box { color: green; }
<!--[if lt IE 9]>
<script src="html5shiv.min.js"></script>
<script src="respond.min.js"></script>
<![endif]-->
lt 小於、lte 小於等於、gt 大於、gte 大於等於
/* 通用觸發 */
.fix { zoom: 1; }
/* IE6 專用 */
.fix { height: 1%; } /* 需配合 overflow:visible 以外屬性 */
/* IE7 專用 */
.fix { min-height: 0; }
引入 ie-css3.htc 行為文件:
.box {
behavior: url(ie-css3.htc);
border-radius: 10px;
box-shadow: 5px 5px 10px #000;
}
div {
margin: 30px !important; /* 現代瀏覽器 */
margin: 28px; /* IE6 讀此行 */
}
順序不可顛倒,利用 IE6 不識別 !important 特性。
.float-box {
float: left;
margin-left: 20px;
display: inline; /* 修復 IE6 雙倍 margin */
}
.container {
min-width: 600px;
_width: expression(document.body.clientWidth < 600 ? "600px" : "auto"); /* IE6 */
}
.min-height-box {
min-height: 500px;
height: auto !important;
height: 500px; /* IE6 當作 min-height */
}
<!DOCTYPE html>| 場景 | 推薦標籤 | 避免使用 |
|---|---|---|
| 頁面標題 | <h1>-<h6> | <div class="title"> |
| 導航菜單 | <nav> | <div class="nav"> |
| 文章塊 | <article> | <div class="article"> |
| 側邊欄 | <aside> | <div class="sidebar"> |
| 頁頭/頁腳 | <header>/<footer> | <div class="header/footer"> |
| 強調文本 | <em>/<strong> | <i>/<b> |
| 表單標籤 | <label for="id"> | 純文本 + input |