改進思想:css
經過對主內容區main增長一個額外包裹層div,將原來在父節點上的 padding-left|right 設置轉移到包裹層上使用 margin-left|right 替代,達到更加靈活的佈局設置。html
原始聖盃佈局實現:http://my.oschina.net/jsan/blog/368543chrome
一樣的效果:瀏覽器
CSS 和 DOM 代碼以下:ide
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>改進版聖盃佈局</title> <style type="text/css"> body {background-color: #ffffff; font-size:14px;} #hd, #ft {padding:20px 3px; background-color: #cccccc; text-align: center;} .bd-lft, .bd-rgt, .bd-3-lr, .bd-3-ll, .bd-3-rr {margin:10px 0; } .main {background-color:#ececec;} .main-wrap {background-color: #03a9f4; color:#ffffff; min-width:200px;} .aside, .aside-1, .aside-2 {background-color: #00bcd4; color:#ffffff;} p {margin:0; padding:20px; text-align: center;} /* 左側欄固定寬度,右側自適應 */ .bd-lft { zoom:1; overflow:hidden; } .bd-lft .aside { float:left; width:200px; margin-left:-100%; /*= -100%*/ } .bd-lft .main { float:left; width:100%; } .bd-lft .main-wrap { margin-left: 210px; } /* 右側欄固定寬度,左側自適應 */ .bd-rgt { zoom:1; overflow:hidden; } .bd-rgt .aside { float:left; width:200px; margin-left:-200px; /* = -this.width */ } .bd-rgt .main { float:left; width:100%; } .bd-rgt .main-wrap { margin-right: 210px; } /* 左中右 三欄自適應 */ .bd-3-lr { zoom:1; overflow:hidden; } .bd-3-lr .main { float:left; width:100%; } .bd-3-lr .main-wrap { margin: 0 210px 0 210px; } .bd-3-lr .aside-1 { float: left; width:200px; margin-left: -100%; } .bd-3-lr .aside-2 { float: left; width:200px; margin-left: -200px; } /* 都在左邊,右側自適應 */ .bd-3-ll { zoom:1; overflow:hidden; } .bd-3-ll .main { float:left; width:100%; } .bd-3-ll .main-wrap { margin-left:420px; } .bd-3-ll .aside-1 { float: left; width:200px; margin-left: -100%; } .bd-3-ll .aside-2 { float: left; width:200px; margin-left: -100%; position:relative; left: 210px; } /* 都在右邊,左側自適應 */ .bd-3-rr { zoom:1; overflow:hidden; } .bd-3-rr .main { float:left; width:100%; } .bd-3-rr .main-wrap { margin-right:420px; } .bd-3-rr .aside-1 { float: left; width:200px; margin-left: -410px; } .bd-3-rr .aside-2 { float: left; width:200px; margin-left: -200px; } </style> </head> <body> <div id="hd">頭部</div> <div class="bd-lft"> <div class="main"> <div class="main-wrap"> <p>主內容欄自適應寬度</p> </div> </div> <div class="aside"> <p>側邊欄固定寬度</p> </div> </div> <div class="bd-rgt"> <div class="main"> <div class="main-wrap"> <p>主內容欄自適應寬度</p> </div> </div> <div class="aside"> <p>側邊欄固定寬度</p> </div> </div> <div class="bd-3-lr"> <div class="main"> <div class="main-wrap"> <p>主內容欄自適應寬度</p> </div> </div> <div class="aside-1"> <p>側邊欄1固定寬度</p> </div> <div class="aside-2"> <p>側邊欄2固定寬度</p> </div> </div> <div class="bd-3-ll"> <div class="main"> <div class="main-wrap"> <p>主內容欄自適應寬度</p> </div> </div> <div class="aside-1"> <p>側邊欄1固定寬度</p> </div> <div class="aside-2"> <p>側邊欄2固定寬度</p> </div> </div> <div class="bd-3-rr"> <div class="main"> <div class="main-wrap"> <p>主內容欄自適應寬度</p> </div> </div> <div class="aside-1"> <p>側邊欄1固定寬度</p> </div> <div class="aside-2"> <p>側邊欄2固定寬度</p> </div> </div> <div id="ft">底部</div> </body> </html>