CSS 跨瀏覽器相容是老生常談,網上教學到處都有。以下內容算不上新穎,純粹是个人實戰的整理,希望對初學者有些幫助。
IE7 開始支援 !important,因此這招如今只針對 IE6。注意宣告要寫在前面。
<style>
#wrapper {
width:100px!important; /* IE7 + FF */
width:80px; /* IE6 */
}
</style>
這兩個前綴屬 IE 專屬語法,Firefox 不支援;其中 *+html 又只為 IE7 所用。
<style>
#wrapper { width:120px; } /* Firefox */
* html #wrapper { width:80px; } /* IE6 */
*+html #wrapper { width:60px; } /* IE7,注意順序 */
</style>
前提:HTML 頂端必須有以下聲明,否則 *+html 對 IE7 不生效。
<!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。