詳解CSS的三列式聖杯布局方案完全解析

來源:互聯網
上載者:User
聖杯布局效果優美且對瀏覽器安全色性要求非常低,是一種非常給力的三列式頁面配置方案,接下來就來看看對CSS的三列式"聖杯布局"方案完全解析:

聖杯布局源自 Matthew Levine 在06年的一篇文章,其DOM結構如下:

<p class="container">    <p class="main"></p> <p class="sub"></p> <p class="extra"></p></p>

流程解說
接下來,讓我們一步一步地實現聖杯布局;

1、 首先分別浮動main、sub、extra三列, 然後利用負外邊距把sub列和extra列定位到左右兩邊。這時的CSS代碼如下:

.main {    float: left;    width: 100%;    height: 300px;    background-color: rgba(255, 0, 0, .5);   }   .sub {    float: left;    width: 200px;    height: 300px;    margin-left: -100%;    background-color: rgba(0, 255, 0, .5);   }   .extra {    float: left;    width: 180px;    height: 300px;    margin-left: -180px;    background-color: rgba(0, 0, 255, .5);   }

2、 完成上步後,sub和extra列已經到了正確的位置,但是sub和extra列卻覆蓋了main的兩邊,對於這個問題,聖杯布局的解決辦法是給容器container添加左、右內邊距,從而讓main列定位到正確的位置。

.container {    padding-left: 210px;    padding-right: 190px;   }

3、 完成第二步後又出現了新問題:sub、extra列也受到容器左右內邊距的影響位置發生了移動。為瞭解決這個問題,聖杯布局使用相對定位使sub、extra列回到正確的位置。新添加代碼如下:

.sub {    position: relative;    left: -210px;   }   .extra {    position: relative;    rightright: -190px;   }

4、 當瀏覽器縮小到一定程度時,這個布局可能會被破壞,可以在body上添加min-width屬性解決。最終的聖杯布局CSS代碼如下:

body {    min-width: 600px; /* 2*sub + extra */}   .container {    padding-left: 210px;    padding-right: 190px;   }   .main {    float: left;    width: 100%;    height: 300px;    background-color: rgba(255, 0, 0, .5);   }   .sub {    position: relative;    left: -210px;    float: left;    width: 200px;    height: 300px;    margin-left: -100%;    background-color: rgba(0, 255, 0, .5);   }   .extra {    position: relative;    rightright: -190px;    float: left;    width: 180px;    height: 300px;    margin-left: -180px;    background-color: rgba(0, 0, 255, .5);   }

完整執行個體

效果如下:

CSS 和 DOM 代碼如下:

<!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; min-width:400px;}       .main {background-color: #03a9f4; color:#ffffff;}       .aside, .aside-1, .aside-2 {background-color: #00bcd4; color:#ffffff;}       p {margin:0; padding:20px; text-align: center;}       /* 左側欄固定寬度,右側自適應 */    .bd-lft {           zoom:1;           overflow:hidden;           padding-left:210px;       }       .bd-lft .aside {           float:left;           width:200px;           margin-left:-100%; /*= -100%*/        position:relative;           left:-210px; /* = -parantNode.paddingLeft */        _left: 0; /*IE6 hack*/    }       .bd-lft .main {           float:left;           width:100%;       }       /* 右側欄固定寬度,左側自適應 */    .bd-rgt {           zoom:1;           overflow:hidden;           padding-right:210px;       }       .bd-rgt .aside {           float:left;           width:200px;           margin-left:-200px; /* = -this.width */        position:relative;           rightright:-210px; /* = -parantNode.paddingRight */    }       .bd-rgt .main {           float:left;           width:100%;       }       /* 左中右 三欄自適應 */    .bd-3-lr {           zoom:1;           overflow:hidden;           padding-left:210px;           padding-right:210px;       }       .bd-3-lr .main {           float:left;           width:100%;       }       .bd-3-lr .aside-1 {           float: left;           width:200px;           margin-left: -100%;           position:relative;           left: -210px;           _left: 210px; /*IE6 hack*/    }       .bd-3-lr .aside-2 {           float: left;           width:200px;           margin-left: -200px;           position:relative;           rightright: -210px;       }       /* 都在左邊,右側自適應 */    .bd-3-ll {           zoom:1;           overflow:hidden;           padding-left:420px;       }       .bd-3-ll .main {           float:left;           width:100%;       }       .bd-3-ll .aside-1 {           float: left;           width:200px;           margin-left: -100%;           position:relative;           left: -420px;           _left: 0px; /*IE6 hack*/    }       .bd-3-ll .aside-2 {           float: left;           width:200px;           margin-left: -100%;           position:relative;           left: -210px;           _left: 210px; /*IE6 hack*/    }       /* 都在右邊,左側自適應 */    .bd-3-rr {           zoom:1;           overflow:hidden;           padding-right:420px;       }       .bd-3-rr .main {           float:left;           width:100%;       }       .bd-3-rr .aside-1 {           float: left;           width:200px;           margin-left: -200px;           position:relative;           rightright: -210px;       }       .bd-3-rr .aside-2 {           float: left;           width:200px;           margin-left: -200px;           position:relative;           rightright: -420px;       }       </style>   </head>   <body>       <p id="hd">頭部</p>       <p class="bd-lft">           <p class="main">               <p>主內容欄自適應寬度</p>           </p>           <p class="aside">               <p>側邊欄固定寬度</p>           </p>       </p>       <p class="bd-rgt">           <p class="main">               <p>主內容欄自適應寬度</p>           </p>           <p class="aside">               <p>側邊欄固定寬度</p>           </p>       </p>       <p class="bd-3-lr">           <p class="main">               <p>主內容欄自適應寬度</p>           </p>           <p class="aside-1">               <p>側邊欄1固定寬度</p>           </p>           <p class="aside-2">               <p>側邊欄2固定寬度</p>           </p>       </p>       <p class="bd-3-ll">           <p class="main">               <p>主內容欄自適應寬度</p>           </p>           <p class="aside-1">               <p>側邊欄1固定寬度</p>           </p>           <p class="aside-2">               <p>側邊欄2固定寬度</p>           </p>       </p>       <p class="bd-3-rr">           <p class="main">               <p>主內容欄自適應寬度</p>           </p>           <p class="aside-1">               <p>側邊欄1固定寬度</p>           </p>           <p class="aside-2">               <p>側邊欄2固定寬度</p>           </p>       </p>       <p id="ft">底部</p>   </body>   </html>

聖杯布局的優點總結如下:
1.使主要內容列先載入。
2.允許任何列是最高的。
3.沒有額外的p。
4.需要的hack很少。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.