CSS 跨瀏覽器相容已經是老生常談,網路上的教程不勝枚舉。以下內容沒太多新奇之處,純屬個人實務總結,希望對初學者有幫助。
以下兩種方法幾乎能解決現今所有 hack 需求。
1. !important
隨著 IE7 開始支援 !important,這個方法現在只用來 hack IE6。注意宣告位置需要提前:
<style>
#wrapper {
width: 100px !important; /* IE7 + Firefox */
width: 80px; /* IE6 */
}
</style>
2. 用 * html 與 *+html 分別 hack IE6 與 IE7
* html 與 *+html 是 IE 特有的選取寫法,Firefox 不支援;而 *+html 又為 IE7 所特有。
<style>
#wrapper { width: 120px; } /* Firefox */
* html #wrapper { width: 80px; } /* IE6 fixed */
*+html #wrapper { width: 60px; } /* IE7 fixed,注意順序 */
</style>
要注意的是,*+html 對 IE7 的 hack 必須保證 HTML 頂部有以下 DOCTYPE 聲明:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/HTML4/loose.dtd">
把以下代碼加入全域 CSS,並為需要閉合的 div 加上 class="clearfix" 即可,屢試不爽:
<style>
/* ClearFix */
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix { display: inline-block; }
/* Hide from IE Mac */
.clearfix { display: block; }
/* End hide from IE Mac */
/* end of clearfix */
</style>
cursor: pointer;,hand 只適用於 IE。