[ 好文分享 ] CSS佈局終極方案之--聖盃佈局(兼容IE6+,現代瀏覽器)

聖盃佈局--很好很巧妙的佈局方式,每次都要翻看一下,乾脆轉過來css

轉自:http://my.oschina.net/jsan/blog/368543html

最終效果:

圖片描述

左側欄固定寬度,右側自適應

html:chrome

<div class="bd-lft">
    <div class="main">
        <p>主內容欄自適應寬度</p>
    </div>
 
    <div class="aside">
        <p>側邊欄固定寬度</p>
    </div>
</div>

css:ide

.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%;
}

右側欄固定寬度,左側自適應

html:佈局

<div class="bd-rgt">
    <div class="main">
        <p>主內容欄自適應寬度</p>
    </div>
 
    <div class="aside">
        <p>側邊欄固定寬度</p>
    </div>
</div>

css:ui

.bd-rgt {
        zoom:1;
        overflow:hidden;
        padding-right:210px;
    }
    .bd-rgt .aside {
        float:left;
        width:200px;
        margin-left:-200px; /* = -this.width */
 
        position:relative;
        right:-210px; /* = -parantNode.paddingRight */
    }
    .bd-rgt .main {
        float:left;
        width:100%;
    }

左中右 三欄自適應

html:this

<div class="bd-3-lr">
    <div class="main">
        <p>主內容欄自適應寬度</p>
    </div>

    <div class="aside-1">
        <p>側邊欄1固定寬度</p>
    </div>

    <div class="aside-2">
        <p>側邊欄2固定寬度</p>
    </div>
</div>

css:spa

.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;
        right: -210px;
    }

都在左邊,右側自適應

html:.net

<div class="bd-3-ll">
    <div class="main">
        <p>主內容欄自適應寬度</p>
    </div>
 
    <div class="aside-1">
        <p>側邊欄1固定寬度</p>
    </div>
 
    <div class="aside-2">
        <p>側邊欄2固定寬度</p>
    </div>
</div>

css:code

.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*/
}

都在右邊,左側自適應

html:

<div class="bd-3-rr">
    <div class="main">
        <p>主內容欄自適應寬度</p>
    </div>
 
    <div class="aside-1">
        <p>側邊欄1固定寬度</p>
    </div>
 
    <div class="aside-2">
        <p>側邊欄2固定寬度</p>
    </div>
</div>

css:

.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;
    right: -210px;
}
.bd-3-rr .aside-2 {
    float: left;
    width:200px;
    margin-left: -200px;
 
    position:relative;
    right: -420px;
}

最終代碼:

<!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;
        right: -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;
        right: -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;
        right: -210px;
    }
    
    .bd-3-rr .aside-2 {
        float: left;
        width: 200px;
        margin-left: -200px;
        position: relative;
        right: -420px;
    }
    </style>
</head>

<body>
    <div id="hd">頭部</div>
    <div class="bd-lft">
        <div class="main">
            <p>主內容欄自適應寬度</p>
        </div>
        <div class="aside">
            <p>側邊欄固定寬度</p>
        </div>
    </div>
    <div class="bd-rgt">
        <div class="main">
            <p>主內容欄自適應寬度</p>
        </div>
        <div class="aside">
            <p>側邊欄固定寬度</p>
        </div>
    </div>
    <div class="bd-3-lr">
        <div class="main">
            <p>主內容欄自適應寬度</p>
        </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">
            <p>主內容欄自適應寬度</p>
        </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">
            <p>主內容欄自適應寬度</p>
        </div>
        <div class="aside-1">
            <p>側邊欄1固定寬度</p>
        </div>
        <div class="aside-2">
            <p>側邊欄2固定寬度</p>
        </div>
    </div>
    <div id="ft">底部</div>
</body>

</html>
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息