DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
DIV+CSS 同時兼容 IE6、IE7 與 Firefox 的通用做法
編輯:CSS詳解     

CSS 跨瀏覽器相容是老話題,網上教學不勝枚舉。以下內容不算新奇,純粹是個人實戰整理,希望對入門者有些幫助。

一、CSS Hack

下列兩招幾乎涵蓋目前所有常見情境。

1. !important

自從 IE7 支援 !important 之後,這招就專門用來對付 IE6。注意宣告必須寫在前面。

<style>
#wrapper {
  width:100px!important; /* IE7 + FF */
  width:80px;            /* IE6 */
}
</style>

2. 用 *+html 與 *html 分流 IE6/IE7 與 Firefox

*+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">

二、萬能 float 閉合

把這段加入全域 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>

三、其他實用技巧

  1. Firefox 下為 div 加上 padding 會撐大 width 與 height,IE 不會(可用 !important 修)。
  2. 垂直置中:把 line-height 設成與該 div 同高,再搭配 vertical-align:middle(內容不可換行);水平置中:margin:0 auto(並非萬能)。
  3. 要為 a 標籤內的內容套樣式,需設 display:block,導覽列常見。
  4. FF 與 IE 對 BOX 的理解差異會產生 2px 落差;設為 float 的 div 在 IE 下 margin 會加倍。
  5. ul 在 Firefox 預設自帶 list-style 與 padding,最好事先宣告歸零。
  6. 作為外層容器的 div 別寫死高度,加上 overflow:hidden 讓它自適應。
  7. 手形游標用 cursor:pointer;hand 只有 IE 認得。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved