导航栏常用这种布局, 常见于导航栏
可以兼容比较老的浏览器
实现步骤:
text-align: justify
display: inline-block;
, 或者可能需要修复text-align::after
为最后一行添加强制换行<style>
.grid {
background: #ddd;
padding: 0 100px;
width: 500px;
text-align: justify;
}
.grid::after {
content: '';
display: inline-block;
width: 100%;
}
.grid-item {
background: purple;
width: 30%;
height: 50px;
margin-bottom: 50px;
display: inline-block;
}
</style>
<div class="grid">
<div class="grid-item">111</div>
<div class="grid-item">222222222222222</div>
<div class="grid-item">333</div>
<div class="grid-item">444444444444444</div>
<div class="grid-item">333</div>
<div class="grid-item">333</div>
<div class="grid-item">333</div>
<div class="grid-item">444444444444444</div>
</div>
如果不需要考虑IE9及之前的版本,这个方法简单方便
display: flex;
justify-content: space-between;
flex-wrap: wrap;
<style>
.grid {
background: #ddd;
padding: 0 10px;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.grid-item {
background: purple;
height: 50px;
padding: 0 20px;
margin-bottom: 50px;
}
</style>
<div class="grid">
<div class="grid-item">3333</div>
<div class="grid-item">3333</div>
<div class="grid-item">3333</div>
<div class="grid-item">3333</div>
<div class="grid-item">3333</div>
<div class="grid-item">3333</div>
<div class="grid-item">3333</div>
</div>