参考链接:
干货!各种常见布局实现+知名网站实例分析
前端布局基础概述
https://juejin.im/post/599970...
https://juejin.im/post/5bbcd7...
http://elevenbeans.github.io/...
一些有趣的 CSS 魔法和布局(上)

等高布局

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

流式布局

特点:等宽不等高

参考链接:
纯css实现瀑布流(multi-column多列及flex布局)
原生js实现瀑布流效果
https://mfs-mark.github.io/lo...
https://www.w3cplus.com/css/p...


zhouzhou
1.5k 声望76 粉丝

web前端