CSS用flex布局两端对齐,列不满左对齐

版权声明:有用请收藏,转载请注明出处!不做伸手党,谢谢! https://blog.csdn.net/qq_42221334/article/details/89396356

       布局上要求两端对齐,且最后一行在列不满的情况下要求左对齐,使用flex的justify-content: space-between;
实现时发现最后一行不能左对齐,而是两端对齐方式。


# 网上查了一些资料,有两种方法可以实现效果:


**1.

添加几个空item**(对我来说最有效的,适用于大多数场景)
根据布局列数添加空item,比如每行最大n列,那么在最后添加n-2个空item即可

<html>
<style>
.box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}
.item {
    width: 30%;
    height: 50px;
    background-color: #f1f8ff;
    margin-bottom: 10px;
}
.placeholder {
    width: 30%;
    height: 0px;
}
</style>
<body>
  <div class="box">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="placeholder"></div>
  </div>
</body>
</html>

实现效果:

**2.

利于after或者before(适用于每行3或者4列)**

.box:after {
    display:block;
    content:"";
    width: 30%;
    height:0px;
}


 

猜你喜欢

转载自blog.csdn.net/qq_42221334/article/details/89396356