這是最典型實用的「上中下」結構、並在中間分為三欄的布局,有兩個鮮明特點:
1. 中間三欄效果,可任意實現單欄背景色。
2. 整體最窄 770px、最寬 1024px,也就是說窗口小於 770px 時出現底部滾動條,大於 1024px 時自動屏幕居中。
已在 IE6.0 和 FF1.5 測試通過。
分析:
最外層的 wrapper 把所有內容都嵌套在裡邊,整體相對定位。min-width 和 max-width 已很好地控制了最窄最寬值,但對 IE 沒作用。如果沒有其他布局穿插,這一層其實寫在 body 內就可以,能少一層嵌套。
#wrapper { width:auto; border:1px solid #000; min-width:770px; max-width:1024px; text-align:left; margin-left:auto; margin-right:auto; position:relative; }
wrapper 下級的 outer、header、footer 中,header 絕對定位,footer 相對定位;outer 分別對左右留出 130px 的外邊距,這是兼容非 IE 的關鍵。
#outer { margin-left:130px; margin-right:130px; background:silver; border-left:1px solid #000; border-right:1px solid #000; color:#000; }
#header { position:absolute; top:0; left:0; width:100%; height:70px; line-height:70px; border-bottom:1px solid #000; overflow:hidden; background:#0ff; text-align:center; font-size:xx-large; }
#footer { width:100%; clear:both; line-height:50px; border-top:1px solid #000; background:#ffc; color:#000; text-align:center; position:relative; }
outer 下級的 clearheader、outerwrap、right、clearer 中,clearheader 用來填補 header 的空白,clearer 是一個常用的填充 hack 用法。outerwrap 寬度為什麼是 99% 而不是 100%?因為它的上層 outer 有邊框,100% 寬再加兩個邊框像素就會撐大,在 FF 中有明顯效果。right 的處理很經典:IE 下解析為定位,FF 下則為浮動,負邊距的處理剛好用上 outer 留出的空白。
#clearheader { height:72px; }
.outerwrap { float:left; width:99%; }
#right { position:relative; width:130px; float:right; left:1px; margin-right:-129px; }
* html #right { margin-right:-130px; margin-left:-3px; }
.clearer { height:1px; overflow:hidden; margin-top:-1px; clear:both; }
outerwrap 內的 centre、content、left、clearer 思路類似。此外,透過 IE 條件註釋配合 expression 方法,可實現對 IE5.0 及以上版本的寬度條件控制:
body { width:expression( documentElement.clientWidth < 770 ? (documentElement.clientWidth == 0 ? (body.clientWidth < 770 ? "770" : "auto") : "770px") : "auto" ); }
#wrapper { width:expression( documentElement.clientWidth > 1024 ? (documentElement.clientWidth == 0 ? (body.clientWidth > 1024 ? "1024" : "auto") : "1024px") : "auto" ); }
最初只想搞清楚如何在居中同時實現 min/max,沒想到最後靠的是 expression,略顯失望,其實這裡用腳本控制更好。總的來說,這是一個融合了許多經典用法與定義的複雜布局示例,同時很傳統、很實用。類似的複雜布局,四層嵌套實現對於傳統布局來說還是頗具優勢的。
參考來源:3 col layout with equalising columns and footer
https://www.pmob.co.uk/temp/min-max-3col.htm