flex布局采用justify-content:space-between时,当为两个内容时中间被空出的解决方案

本文介绍了在CSS flex布局中,使用justify-content:space-between时遇到两个元素间出现空隙的问题。通过分析和示例代码,提出了解决这种布局样式不符合预期的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们在用flex布局的时候有时会用到justify-content:space-between属性,这个属性是让弹性容器内的元素向两端对齐。

    <div class="box">
       <div></div>
       <div></div>
       <div></div>
       <div></div>
       <div></div>
       <div></div>
       <div></div>
   </div>
     .box {
           width: 1000px;
           height: 700px;
           background-color: #999;
           display: flex;
           justify-content: space-between;
           flex-wrap: wrap;
           align-items: center;
       }

       .box div {
           width: 195px;
           height: 100px;
           background-color: aqua;
       }

这是上面代码实现的效果
在这里插入图片描述

但是我使用justify-content:space-between属性之后想要成这种效果 怎么办呢?
在这里插入图片描述

可以这样写

     <div class="content">
        <div  class="box"></div>
        <div  class="box"></div>
        <div  class="box"></div>
        <div  class="box"></div>
        <div  class="box"></div>
        <div  class="box"></div>
        <div  class="box"></div>
        
        <!-- 一行需要几个元素就写几个下面的标签 -->
        <div class="box" style="visibility: hidden;height: 0;margin: 0;"></div>
        <div class="box" style="visibility: hidden;height: 0;margin: 0;"></div>
        <div class="box" style="visibility: hidden;height: 0;margin: 0;"></div>
        <div class="box" style="visibility: hidden;height: 0;margin: 0;"></div>
        <div class="box" style="visibility: hidden;height: 0;margin: 0;"></div>
    </div>
     .content{
           width: 1000px;
           height: 700px;
           background-color: #999;
           display: flex;
           justify-content: space-between;
           flex-wrap: wrap;
           align-items: center;
       }

       .content .box  {
           width: 195px;
           height: 100px;
           background-color: aqua;
       }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值