实现CSS等分布局的5种方式

前面的话

  等分布局是指子元素平均分配父元素宽度的布局方式,本文将介绍实现等分布局的5种方式

 

float 

【思路一】float

  缺点:结构和样式存在耦合性,IE7-浏览器下对宽度百分比取值存在四舍五入的误差

【1】float padding background-clip

  使用padding来实现子元素之间的间距,使用background-clip使子元素padding部分不显示背景

<style>
body,p{margin: 0;}
.parentWrap{
    overflow: hidden;
}
.parent{
    margin-right: -20px;
    overflow: hidden;
}
.child{
    float: left;
    height: 100px;
    width: 25%;
    padding-right: 20px;
    box-sizing: border-box;
    background-clip: content-box;
}
</style>
<div class="parentWrap">
    <div class="parent" style="background-color: lightgrey;">
        <div class="child" style="background-color: lightblue;">1</div>
        <div class="child" style="background-color: lightgreen;">2</div>
        <div class="child" style="background-color: lightsalmon;">3</div>
        <div class="child" style="background-color: pink;">4</div>                
    </div>    
</div>

style="width: 100%; height: 120px;" src="https://demo.xiaohuochai.site/css/dengfen/d1.html" frameborder="0" width="320" height="240">

【2】float margin calc

  使用margin实现子元素之间的间距,使用calc()函数计算子元素的宽度

<style>
body,p{margin: 0;}
.parentWrap{
    overflow: hidden;
}
.parent{
    overflow: hidden;
    margin-right: -20px;
}
.child{
    float: left;
    height: 100px;
    width: calc(25% - 20px);
    margin-right: 20px;
}
</style>
<div class="parentWrap">
    <div class="parent" style="background-color: lightgrey;">
        <div class="child" style="background-color: lightblue;">1</div>
        <div class="child" style="background-color: lightgreen;">2</div>
        <div class="child" style="background-color: lightsalmon;">3</div>
        <div class="child" style="background-color: pink;">4</div>                
    </div>    
</div>

style="width: 100%; height: 120px;" src="https://demo.xiaohuochai.site/css/dengfen/d2.html" frameborder="0" width="320" height="240">

【3】float margin (fix)

  使用margin实现子元素之间的间距,通过增加结构来实现兼容

<style>
body,p{margin: 0;}
.parentWrap{
    overflow: hidden;
}
.parent{
    overflow: hidden;
    margin-right: -20px;
}
.child{
    float: left;
    width: 25%;
}
.in{
    margin-right: 20px;
    height: 100px;
}
</style>
<div class="parentWrap">
    <div class="parent" style="background-color: lightgrey;">
        <div class="child" style="background-color: blue;">
            <div class="in" style="background-color: lightblue;">1</div>
        </div>
        <div class="child" style="background-color: green;">
            <div class="in" style="background-color: lightgreen;">2</div>
        </div>
        <div class="child" style="background-color: orange;">
            <div class="in" style="background-color: lightsalmon;">3</div>
        </div>
        <div class="child" style="background-color: red;">
            <div class="in" style="background-color: pink;">4</div>
        </div>                
    </div>    
</div>

style="width: 100%; height: 120px;" src="https://demo.xiaohuochai.site/css/dengfen/d3.html" frameborder="0" width="320" height="240">

 

inline-block

【思路二】inline-block

  缺点:需要设置垂直对齐方式vertical-align,则需要处理换行符解析成空格的间隙问题。IE7-浏览器不支持给块级元素设置inline-block属性,兼容代码是display:inline;zoom:1;

【1】inline-block padding background-clip

<style>
body,p{margin: 0;}
.parentWrap{
    overflow: hidden;
}
.parent{
    font-size: 0;
    margin-right: -20px;
    overflow: hidden;
}
.child{
    display:inline-block;
    vertical-align: top;
    width: 25%;
    padding-right: 20px;
    box-sizing: border-box;
    background-clip: content-box;
    font-size: 16px;
}
</style>
<div class="parentWrap">
    <div class="parent" style="background-color: lightgrey;">
        <div class="child" style="background-color: lightblue;">1</div>
        <div class="child" style="background-color: lightgreen;">2</div>
        <div class="child" style="background-color: lightsalmon;">3</div>
        <div class="child" style="background-color: pink;">4</div>                
    </div>    
</div>

style="width: 100%; height: 120px;" src="https://demo.xiaohuochai.site/css/dengfen/d4.html" frameborder="0" width="320" height="240">

【2】inline-block margin calc

<style>
body,p{margin: 0;}
.parentWrap{
    overflow: hidden;
}
.parent{
    margin-right: -20px;
    font-size: 0;
}
.child{
    display: inline-block;
    vertical-align: top;
    font-size: 16px;
    height: 100px;
    width: calc(25% - 20px);
    margin-right: 20px;
}
</style>
<div class="parentWrap">
    <div class="parent" style="background-color: lightgrey;">
        <div class="child" style="background-color: lightblue;">1</div>
        <div class="child" style="background-color: lightgreen;">2</div>
        <div class="child" style="background-color: lightsalmon;">3</div>
        <div class="child" style="background-color: pink;">4</div>                
    </div>    
</div>

style="width: 100%; height: 120px;" src="https://demo.xiaohuochai.site/css/dengfen/d5.html" frameborder="0" width="320" height="240">

【3】inline-block margin (fix)

<style>
body,p{margin: 0;}
.parentWrap{
    overflow: hidden;
}
.parent{
    margin-right: -20px;
    font-size: 0;
}
.child{
    display: inline-block;
    vertical-align: top;
    font-size: 16px;
    width: 25%;
}
.in{
    margin-right: 20px;
    height: 100px;
}
</style>
<div class="parentWrap">
    <div class="parent" style="background-color: lightgrey;">
        <div class="child" style="background-color: blue;">
            <div class="in" style="background-color: lightblue;">1</div>
        </div>
        <div class="child" style="background-color: green;">
            <div class="in" style="background-color: lightgreen;">2</div>
        </div>
        <div class="child" style="background-color: orange;">
            <div class="in" style="background-color: lightsalmon;">3</div>
        </div>
        <div class="child" style="background-color: red;">
            <div class="in" style="background-color: pink;">4</div>
        </div>                
    </div>    
</div>

style="width: 100%; height: 120px;" src="https://demo.xiaohuochai.site/css/dengfen/d6.html" frameborder="0" width="320" height="240">

 

table

【思路三】table

  缺点:元素被设置为table后,内容撑开宽度。若要兼容IE7-浏览器,需要改为<table>结构。table-cell元素无法设置margin,设置padding及background-clip也不可行

<style>
body,p{margin: 0;}
.parentWrap{
    overflow: hidden;
}
.parent{
    display: table;
    width: calc(100%   20px);
    table-layout: fixed;
}
.child{
    display: table-cell;
    padding-right: 20px;
}
.in{
    height: 100px;
}
</style>
<div class="parentWrap">
    <div class="parent" style="background-color: lightgrey;">
        <div class="child" style="background-color: blue;">
            <div class="in" style="background-color: lightblue;">1</div>
        </div>
        <div class="child" style="background-color: green;">
            <div class="in" style="background-color: lightgreen;">2</div>
        </div>
        <div class="child" style="background-color: orange;">
            <div class="in" style="background-color: lightsalmon;">3</div>
        </div>
        <div class="child" style="background-color: red;">
            <div class="in" style="background-color: pink;">4</div>
        </div>                
    </div>    
</div>

style="width: 100%; height: 120px;" src="https://demo.xiaohuochai.site/css/dengfen/d7.html" frameborder="0" width="320" height="240">

 

flex

【思路四】flex

<style>
body,p{margin: 0;}
.parent{
    display: flex;
}
.child{
    flex:1;
    height: 100px;
}
.child   .child{
    margin-left: 20px;
}
</style>
<div class="parent" style="background-color: lightgrey;">
    <div class="child" style="background-color: lightblue;">1</div>
    <div class="child" style="background-color: lightgreen;">2</div>
    <div class="child" style="background-color: lightsalmon;">3</div>
    <div class="child" style="background-color: pink;">4</div>                
</div>    

style="width: 100%; height: 120px;" src="https://demo.xiaohuochai.site/css/dengfen/d8.html" frameborder="0" width="320" height="240">

 

grid

【思路五】grid

<style>
body,p{margin: 0;}
.parent{
    display: grid;
    grid-template-columns:repeat(4,1fr);
    grid-gap:20px;
    height: 100px;
}
</style>
<div class="parent" style="background-color: lightgrey;">
    <div class="child" style="background-color: lightblue;">1</div>
    <div class="child" style="background-color: lightgreen;">2</div>
    <div class="child" style="background-color: lightsalmon;">3</div>
    <div class="child" style="background-color: pink;">4</div>                
</div> 

style="width: 100%; height: 120px;" src="https://demo.xiaohuochai.site/css/dengfen/d99.html" frameborder="0" width="320" height="240">

 


更多专业前端知识,请上 【猿2048】www.mk2048.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值