CSS系列之常用佈局

zhouzhou發表於2018-03-07

兩列布局(一列定寬)

float +margin

<style>
    .parent {
         background-color: grey;
         width: 300px;
         height: 200px;
     }
     .left {
         float: left;
         width: 100px;      //需要定寬
         background-color: skyblue;
     }
    .right {
         margin-left: 110px;
         background-color: greenyellow;
     }
</style>
<html>
    <div class="parent">
        <div class="left">
             <p>left</p>
        </div>
        <div class="right">
             <p>right</p>
             <p>right</p>
        </div>
    </div>
</html>

BFC

<style>
    .parent {
         background-color: grey;
         width: 300px;
         height: 200px;
     }
     .left {
         float: left;
         width: 100px;      //需要定寬
         margin-right: 10px;
         background-color: skyblue;
     }
    .right {
         overflow: hidden;
         background-color: greenyellow;
     }
</style>
<html>
    <div class="parent">
        <div class="left">
             <p>left</p>
        </div>
        <div class="right">
             <p>right</p>
             <p>right</p>
        </div>
    </div>
</html>

table

//第一種,同時可以實現等高佈局
<style>
    .parent {
        background-color: grey;
        width: 300px;
        height: 200px;
        display: table;
        table-layout: fixed;
    }
    .left {
        display: table-cell;
        width: 100px;
        background-color: skyblue;

        border-right: 10px solid transparent;
        background-clip: padding-box;   //設定列之間的間距
    }
    .right {
        display: table-cell;
        background-color: greenyellow;
    }
</style>
<html>
    <div class="parent">
        <div class="left">
             <p>left</p>
        </div>
        <div class="right">
             <p>right</p>
             <p>right</p>
        </div>
    </div>
</html>

//第二種方法
<style>
    .parent {
        background-color: grey;
        width: 300px;
        height: 200px;
        display: table;
        table-layout: fixed;
    }
    .left-container {
        display: table-cell;
        width: 100px;
    }
    .left {
        margin-right: 10px;
        background-color: skyblue;
    }
    .right {
        display: table-cell;
        background-color: greenyellow;
    }
</style>
<html>
    <div class="parent">
        <div class="left-container">
             <div class="left">
                 <p>left</p>
             </div>
        </div>
        <div class="right">
             <p>right</p>
             <p>right</p>
        </div>
    </div>
</html>

三列布局

float + margin + calc

<style>
    .parent{
        overflow: hidden;
    }
    .left, .right{
        float: left;
        width: 100px;
    }
    .center{
        float: left; 
        width:calc(100% - 240px);
        margin: 0 20px;
    }
</style>
<html>
    <div class="parent" style="background-color: lightgrey;">
        <div class="left" style="background-color: lightblue;">
            <p>left</p>
        </div>    
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>    
        <div class="right"  style="background-color: lightgreen;">
            <p>right</p>
        </div>        
    </div>
</html>

table

<style>
    .parent{
        display: table; 
        width: 100%;
        table-layout: fixed;
    }
    .left,.right,.centerWrap{
        display:table-cell;
    }
    .left,.right{
        width: 100px;
    }
    .center{
        margin: 0 20px;
    }
</style>
<html>
    <div class="parent" style="background-color: lightgrey;">
        <div class="left" style="background-color: lightblue;">
            <p>left</p>
        </div>    
        <div class="centerWrap" style="background-color: orange;">
            <div class="center" style="background-color: pink;">
                <p>center</p>
                <p>center</p>
            </div>        
        </div>        
        <div class="right"  style="background-color: lightgreen;">
            <p>right</p>
        </div>            
    </div>
</html>

聖盃佈局

<style>
    .wrap{
        padding-left: 200px;
        padding-right: 150px;
    } 
    .main{
        position: relative;
        width: 100%;
        float: left;
        background: deeppink;
    }
    .aside{
        position: relative;
        width: 200px;
        float: left;
        margin-left: -100%;
        background: pink;
        right: 200px;
    }
    .ad{
        position: relative;
        width: 150px;
        float: left;
        margin-right: -150px;
        background: pink;
    }
</style>
<html>
    <div class="wrap">
        <div class="main"> main </div>
        <div class="aside"> aside </div>    
        <div class="ad"> ad </div>
    </div>
</html>

雙飛翼佈局

<style>
    .main{
        width: 100%;
        float: left;
    }
    .main > .inner{
        margin-left: 200px;
        margin-right: 150px;
        background: deeppink;
    }
    .aside{
        width: 200px;
        float: left;
        margin-left: -100%;
        background: pink;
    }
    .ad{
        width: 150px;
        float: left;
        margin-left: -150px;
        background: pink;
    }
</style>
<html>
    <div class="main">
        <div class="inner"> main </div>
    </div>
    <div class="aside"> aside </div>    
    <div class="ad"> ad </div>
</html>

等高佈局

負margin

<style>
    .parent{
        overflow: hidden;
    }
    .left,.centerWrap,.right{
        float: left;
        width: 50%;
        padding-bottom: 9999px;
        margin-bottom: -9999px;
    }
    .center{
        margin: 0 20px;
    }
    .left,.right{
        width: 25%;
    }
</style>
<html>
    <div class="parent" style="background-color: lightgrey;">
        <div class="left" style="background-color: lightblue;">
            <p>left</p>
        </div>  
        <div class="centerWrap">
            <div class="center" style="background-color: pink;">
                <p>center</p>
                <p>center</p>
            </div>         
        </div>
             
        <div class="right" style="background-color: lightgreen;">
            <p>right</p>
        </div>        
    </div>
</html>

table

<style>
    .parent{
        display: table;
        width: 100%;
        table-layout: fixed;
    }
    .left,.centerWrap,.right{
        display: table-cell;
    }
    .center{
        margin: 0 20px;
    }
</style>
<html>
    <div class="parent" style="background-color: lightgrey;">
        <div class="left" style="background-color: lightblue;">
            <p>left</p>
        </div>  
        <div class="centerWrap">
            <div class="center" style="background-color: pink;">
                <p>center</p>
                <p>center</p>
            </div>         
        </div> 
        <div class="right" style="background-color: lightgreen;">
            <p>right</p>
        </div>        
    </div>
</html>

position

<style>
    .parent{
        position: relative;
        height: 40px;
    }
    .left,.center,.right{
        position: absolute;
        top: 0;
        bottom: 0;
    }
    .left{
        left: 0;
        width: 100px;
    }
    .center{
        left: 120px;
        right: 120px;
    }
    .right{
        width: 100px;
        right: 0;
    }
</style>
<html>
    <div class="parent" style="background-color: lightgrey;">
        <div class="left" style="background-color: lightblue;">
            <p>left</p>
        </div>  
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>          
        <div class="right" style="background-color: lightgreen;">
            <p>right</p>
        </div>        
    </div>
</html>

兩端對齊佈局

負margin

<style>
    .container{
        margin:50px 10px;
        border-top:1px solid #000;
        overflow: hidden;
    }
    .main{
        margin-top:10px;
        margin-right:-2%;   
    }
    .item{
        width:23%;
        height:30px;
        line-height: 30px;
        text-align: center;
        margin-right:2%;
        box-sizing:border-box;
        float:left;
        border:1px solid #cbd1d0;
        margin-bottom:10px;
    }
</style>
<html>
    <div class="container">
        <div class="main">
            <div class="item">頭條</div>  
            <div class="item">推薦</div> 
            <div class="item">視訊</div> 
            <div class="item">娛樂</div>  
            <div class="item">體育</div>
            <div class="item">高考</div>
            <div class="item">汽車</div>
            <div class="item">科技</div>
            <div class="item">圖片</div>
        </div>
    </div>
</html>

text-align: justify

<style>
    .main {
       text-align: justify;
       font-size: 0;
     }
     .main li {
       display: inline-block;
       text-align: center;
       margin: 10px;
     }
</style>
<html>
    <div class="main">
        <ul>
             <li><div class="img"></div><span>1</span></li>
             <li><div class="img"></div><span>2</span></li>
             <li><div class="img"></div><span>3</span></li>
             <li><div class="img"></div><span>4</span></li>
             <li><div class="img"></div><span>5</span></li>
             <li><div class="img"></div><span>6</span></li>
             <li><div class="img"></div><span>7</span></li>
             <li><div class="img"></div><span>8</span></li>
       </ul>
     </div>
</html>

相關文章