DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
CSS Hack 兩大主力與 clearfix 的標準寫法
編輯:布局實例     

一、CSS Hack

以下兩種方法幾乎涵蓋了當時絕大多數的 hack 需求。

1. !important

自 IE7 開始支援 !important 之後,這個方法實際上只對 IE6 有效。寫法上有個關鍵:帶 !important 的宣告必須寫在前一行,才能讓後一行成為只有 IE6 讀得到的覆寫。

<style>
#wrapper {
  width: 100px !important;
  width: 80px;
}
</style>

IE7 及以上讀到第一行、套用 100px;IE6 忽略 !important,於是採用後面的 80px。

2. *html 與 *+html

這兩個是 IE 特有的標籤寫法,Firefox 不認;而 *+html 又是 IE7 獨有。

<style>
#wrapper { width: 120px; }
* html #wrapper { width: 80px; }
*+html #wrapper { width: 60px; }
</style>

注意:*+html 要對 IE7 生效,HTML 頂部必須具備完整的 DOCTYPE 宣告,否則 IE7 會落入 quirks 模式而失效:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">

二、float 閉合(清除浮動)

把下面這段放進全域樣式表,需要閉合的容器加上 class="clearfix" 即可:

<style>
.clearfix:after {
  content: ".";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}
.clearfix { display: inline-block; }
.clearfix { display: block; }
</style>

三行程式碼各對應一種情境::after 負責現代瀏覽器;兩行 display 宣告則是針對 IE/Mac 與 IE6/7 的 hack——舊 IE 靠 display: inline-block 觸發 hasLayout 來達到同樣效果,而 display: block 隨後把標準瀏覽器上的顯示還原。

今天的 clearfix

那段 hack 現在可以直接拿掉,只留下:

.clearfix::after {
  content: "";
  display: block;
  clear: both;
}

或者更乾脆:在容器上設 display: flow-root,不必偽元素、不必多餘的 DOM。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved