CSS系列之經常使用佈局

參考連接:
乾貨!各類常見佈局實現+知名網站實例分析
前端佈局基礎概述
https://juejin.im/post/599970...
https://juejin.im/post/5bbcd7...
http://elevenbeans.github.io/...
一些有趣的 CSS 魔法和佈局(上)javascript

等高佈局

負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

只適用於單列,若是多列,最後一行也能夠用加入多個空元素的方法。css

<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>

流式佈局

特色:等寬不等高html

參考連接:
純css實現瀑布流(multi-column多列及flex佈局)
原生js實現瀑布流效果
https://mfs-mark.github.io/lo...
https://www.w3cplus.com/css/p...前端

相關文章
相關標籤/搜索