這是一個很有代表性的案例:一個看似簡單的「左側欄+右側兩塊」版型,卻在 IE6/7/8、Opera、Firefox、Chrome 之間邊界對不齊,還伴隨著難以捉摸的 2px 落差。下面把排查與重構的過程完整走一遍。建議先自己想一遍結構該怎麼搭、問題可能出在哪,讀起來會更有收穫。
這個頁面是典型的「一行兩列」:最左是一欄分類列表,右側上下堆兩個新聞表格。要做出左右並排,通常有兩種做法——浮動或絕對定位。原作者的選擇是浮動,這是最常見的 CSS 排版手段。
原始結構大致如下(節錄):
<div id="main"> <div id="category" class="commonfrm"> … </div> <!-- 左列 --> <div id="newnews" class="commonfrm"> … </div> <!-- 右上 --> <div id="hotnews" class="commonfrm"> … </div> <!-- 右下 --> </div>
關鍵的幾段樣式:
#top, #search, #main, #footer {
margin: 10px auto 10px auto;
width: 760px;
}
.commonfrm {
border: 1px solid #DEDFDE;
float: left;
}
#category {
width: 180px;
height: 368px !important;
height: 378px;
margin-right: 5px;
margin-bottom: 10px;
}
#newnews, #hotnews {
width: 560px;
margin-left: 5px;
margin-bottom: 10px;
}
#main 用 margin: 10px auto 完成整體置中並與視窗上下留 10px,是很常見的寫法。其實不套外層 div,直接給各子塊定義寬高與 margin 也能達到同樣效果。
#category 的兩行 height 是刻意為之:
#category {
height: 368px !important;
height: 378px;
}
結果是 IE7 與 Firefox 採用 368px,IE6 則採用 378px。因為 !important 只有符合標準的瀏覽器才認得,IE6 會直接忽略那一行。這類寫法雖然有效,但屬於典型的 Hack,維護時容易混淆——這也是後面重構要優先處理的項目。
1. 右側兩塊外面應該再包一層容器。這樣控制起來方便得多,只要讓 #rightbar 浮動即可,不必讓兩個子塊各自浮動、各自算 margin。
2. 精簡 margin 寫法。 margin: 10px auto 10px auto; 可簡寫為 margin: 10px auto;。
3. 去掉 !important Hack。把 height 統一改為 height: 368px;。
4. 間距只定義一次。原本左列的 margin-right: 5px 加上右列的 margin-left: 5px 才湊出 10px 間距,這種「一人一半」的寫法很難維護。直接把左列的 margin-right 改為 10px,右列就不再需要 margin-left。
先挑一個順手的編輯器,把結構一層層搭出來:
<div id="wrapper">
<div id="content">
<div id="leftbar"> … </div>
<div id="rightbar">
<div id="rightnews_a"> … </div>
<div id="rightnews_b"> … </div>
</div>
</div>
</div>
命名要點:id 代表這個塊在頁面中只出現一次;若會重複出現,請改用 class。命名只要有規範、自己看得懂即可。
各層含義:#wrapper 是最外層容器,作用近似 body。這裡有個建議:不要對 body 與 html 套用任何樣式,因為這兩個標籤「很難控制」——試著給 body 定義寬高再加背景色,你就會明白。#content 是內容區的起點,不嵌套也無妨,分這一層純粹為了讓結構清楚。#leftbar 是左列,#rightbar 是右列,右列內部再分上下兩塊。
左列內部用 .left_subnav 包住導航,導航一律用 ul 無序列表搭建,這符合 Web 標準:
<div id="leftbar">
<ul class="left_subnav">
<li><a href="#">首 頁</a></li>
<li><a href="#">體育新聞</a></li>
<li><a href="#">財經新聞</a></li>
<li><a href="#">社會新聞</a></li>
</ul>
</div>
右列上半部的新聞清單,我改用定義列表(dl):dt 放欄位名稱,dd 內嵌 span 承載欄位內容。下半部結構相同,直接複製即可。
(1)通用樣式:重置瀏覽器預設值
IE 與其他標準瀏覽器對 CSS 的理解不同,所以一開始就要重置預設樣式:
body, div, dl, dt, dd, ul, li, h3, p {
padding: 0; margin: 0; border: 0;
}
body { background: #fff; color: #333; font: 12px 宋體, 微軟雅黑, Arial; }
a:link, a:visited { text-decoration: none; }
a:hover { text-decoration: underline; }
h3 { background: #7BAEEF; color: #fff; font-size: 14px;
padding-left: 10px; line-height: 26px; }
有人會問:不是常用萬用符 * 嗎?的確可以,但 * 會選中所有標籤。打個比方,做網頁像在白紙上作畫,用 * 等於先把整張紙刷黑,之後某些地方想改成別的顏色,就得再定義一次樣式把黑色洗掉重刷——瀏覽器的解析過程就是這樣反覆覆寫,造成資源浪費與效能下降。小站感覺不明顯,入口級網站就能感受到差別。列出實際用到的標籤,是更精準的做法。
(2)定義各區塊寬高
#wrapper { width: 760px; margin: 10px auto; } /* 外圍容器 */
#content { width: 760px; height: 378px; } /* 內容容器 */
#leftbar { width: 180px; height: 378px; float: left;
border: 1px solid #DEDFDE; margin: 0 10px 0 0; }
#rightbar { float: left; width: 560px; } /* 右列 */
(3)左列細節
ul li { font-size: 14px; list-style: none;
text-align: center; margin: 20px; }
(4)右列兩塊
#rightnews_a { border: 1px solid #DEDFDE; margin: 0 0 10px;
height: 178px; overflow: hidden; }
#rightnews_b { border: 1px solid #DEDFDE; height: 188px; }
overflow: hidden 是為了把超出的子內容隱藏起來。
(5)右列內部:用浮動對齊欄位
#rightbar dl { text-align: center; }
#rightbar dt { float: left; font-weight: bold;
font-size: 14px; line-height: 26px; }
#rightbar .t1 { width: 100px; padding: 0 5px; }
#rightbar .t2 { width: 280px; padding: 0 5px; }
#rightbar .t3 { width: 148px; padding: 0 5px; }
#rightbar dd { line-height: 26px; font-size: 14px; }
#rightbar .dd1 { width: 100px; padding: 0 5px; float: left; }
#rightbar .dd2 { width: 280px; padding: 0 5px; float: left; }
一個標籤浮動之後,最好給它一個合適的寬度,否則容易塌陷。
p,別拿 div 代替;導航就用 ul。不要讓標籤的語義與它實際表達的內容脫節。用 Web 標準重構一個頁面,其實沒那麼困難。動手試試看吧。