DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
前輩整理的 CSS Hack 寫法與常見相容技巧
編輯:CSS詳解     

不同瀏覽器對 CSS 的支援與解析結果並不一致,加上樣式優先順序的規則,於是我們可以針對特定瀏覽器寫出只有它認得(或只有它不認得)的語法。這個過程就叫 CSS hack。

關鍵在於各瀏覽器的辨識能力差異:IE6 能辨識底線「_」與星號「*」,IE7 只認星號「*」而不認底線,Firefox 兩者都不認。書寫順序的一般原則是:辨識能力越強的瀏覽器,樣式寫在越後面。

技巧一:!important

!important 的作用是提高指定樣式規則的優先權。IE7 與所有標準瀏覽器都認得它,IE6 則不認。這正好可以用來區分 IE6 與其他瀏覽器:

.browserTest {
  border: 20px solid #60A179 !important;
  border: 20px solid #00F;
}

在 Firefox 與 IE7 中,!important 的優先級生效,顯示 #60A179;在 IE6 中!important 被忽略,於是顯示 #00F。

技巧二:星號 *

IE 系列都認得屬性前的星號,標準瀏覽器(如 Firefox)則不認。

區別 IE6 與 Firefox:

.browserTest {
  border: 20px solid #60A179;
  *border: 20px solid #00F;
}

區別 IE7、IE6 與 Firefox:

.browserTest {
  border: 20px solid #60A179;
  *border: 20px solid #00F !important;
  *border: 20px solid #333;
}

技巧三:底線 _

只有 IE6 支援底線前綴,IE7 與 Firefox 都不支援:

.browserTest {
  border: 20px solid #60A179;
  *border: 20px solid #00F;
  _border: 20px solid #333;
}
/* 順序一律是:Firefox 寫前面,IE7 寫中間,IE6 寫最後 */

技巧四:* html 與 *+html

* html 與 *+html 是 IE 特有的選擇器寫法,Firefox 不支援;其中 *+html 又只有 IE7 認得:

.browserTest { width:120px; }   /* Firefox */
* html .browserTest { width:80px; }  /* IE6 */
*+html .browserTest { width:60px; }  /* IE7 */

注意:*+html 要對 IE7 生效,HTML 檔頭必須具備完整的 DOCTYPE 宣告,例如:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">

其他常用相容技巧

  1. Firefox 下為 div 設定 padding 會連帶增加 width 與 height,IE 不會(可用 !important 處理)。
  2. 垂直居中:把 line-height 設成與該 div 相同的高度,再用 vertical-align: middle(前提是內容不換行)。
  3. 水平居中:margin: 0 auto(並非萬能,通常需要搭配寬度)。
  4. 要為 a 標籤內的內容加樣式時,記得設 display: block,導航列最常見。
  5. Floa 雙倍邊距:IE 下,一個設了 float 的 div 再加上 margin,會出現雙倍的 margin。解法是幫該 div 加上 display: inline。
#float {
  float: left;
  margin: 5px;      /* IE 會理解為 10px */
  display: inline;  /* IE 於是又理解回 5px */
}

順帶一提,block 元素的特性是總在新行開始,寬、高、行高、邊距都可控制;inline 元素則與其他元素同行排列,寬高不可控。

  1. 盒子模型的差異:
#browserTest {
  width: 650px !important;
  width: 648px;
  padding-left: 2px;
  background: #fff;
}

IE 的盒子總寬 = width + padding + border + margin;Firefox 的盒子總寬就是 width,padding、border、margin 都算在 width 之內。所以一個 width:300px、padding:5px 的區塊,在 IE 中寬度是 310,在 Firefox 中是 300。若區塊帶有內距,建議寫成:

BOX { width: 300px !important; width: 290px; }
  1. ul 在 Firefox 下預設自帶 list-style 與 padding,最好事先重置,避免不必要的差異(導航列與內容列表最常遇到)。
  2. 作為外層容器的 div 不要寫死高度,建議加上 overflow: hidden 讓高度自適應。
  3. 頁面最小寬度:min-width 是很方便的指令,但 IE 不認得,反而會把 width 當最小寬度使用。可以放一個 div 在 body 之下,然後這樣寫:
#container {
  min-width: 600px;
  width: expression(document.body.clientWidth < 600 ? "600px" : "auto");
}

第一行是標準寫法;第二行的 width 用了 JavaScript,只有 IE 認得,會讓 HTML 文件變得不那麼正規,但實際上就是靠 JS 判斷來達成最小寬度。

技巧十:萬能 float 閉合

把以下程式碼放進全域樣式表,需要閉合浮動的 div 加上 clearfix 類別即可:

<style>
/* ClearFix */
.clearfix:after {
  content: ".";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}
* html .clearfix { height: 1%; }
*+html .clearfix { height: 1%; }
.clearfix { display: inline-block; }
/* Hide from IE Mac */
.clearfix { display: block; }
/* End hide from IE Mac */
/* end of clearfix */
</style>

<div id="wrap">
  <div class="column_left"><h1>Float left</h1></div>
  <div class="column_right"><h1>Float right</h1></div>
</div>

#wrap { border: 6px #ccc solid; overflow: auto; _height: 1%; }
.column_left  { float: left;  width: 20%; padding: 10px; }
.column_right { float: right; width: 75%; padding: 10px; border-left: 6px #eee solid; }
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved