flex布局使列表子元素左右间距相等

2022-09-25  本文已影响0人  清风昙

flex布局使列表子元素左右间距相,可以有三种方法实现,效果如下:


image.png

1.使用justify-content:space-evenly

<div class="flex-wrap flex1">
    <div>1</div>
    <div>2</div>
    <div>3</div>
</div>
// css 前部分css共用
.flex-wrap{
    display: flex;
    background-color: #E1E1E1;
    height: 100px;
}
.flex-wrap div{
    background-color: gray;
    width: 30%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    text-align: center;
    color: #ffffff;
}
.flex1{
    justify-content: space-evenly;
}

2.使用justify-content:space-between和伪类

<div class="flex-wrap flex2">
    <div>1</div>
    <div>2</div>
    <div>3</div>
</div>
// css
.flex2{
    justify-content: space-between;
}
.flex2::before, .flex2::after{
    content: '';
    display: block;
}

3.配合使用子元素和第一个子元素

<div class="flex-wrap flex3">
    <div>1</div>
    <div>2</div>
    <div>3</div>
</div>
// css
.flex3{
    margin: 0 auto;
}
.flex3 div{
    margin: 0 auto;
    margin-left: 0;
}
.flex3 div:first-child{
    margin-left: auto;
}
上一篇 下一篇

猜你喜欢

热点阅读