CSS 跨瀏覽器相容是老話題,網上教學不勝枚舉。以下內容不算新奇,純粹是個人實戰整理,希望對入門者有些幫助。
下列兩招幾乎涵蓋目前所有常見情境。
自從 IE7 支援 !important 之後,這招就專門用來對付 IE6。注意宣告必須寫在前面。
<style>
#wrapper {
width:100px!important; /* IE7 + FF */
width:80px; /* IE6 */
}
</style>
*+html 與 *html 是 IE 特有的標籤寫法,Firefox 不支援;而 *+html 又只有 IE7 吃這一套。
<style>
#wrapper { width:120px; } /* Firefox */
* html #wrapper { width:80px; } /* IE6 */
*+html #wrapper { width:60px; } /* IE7,注意順序 */
</style>
提醒:*+html 對 IE7 生效的前提,是 HTML 頂端必須有以下宣告:
<!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>
vertical-align:middle(內容不可換行);水平置中:margin:0 auto(並非萬能)。display:block,導覽列常見。overflow:hidden 讓它自適應。cursor:pointer;hand 只有 IE 認得。