【前端基础】flex布局中使用`justify-content`后,最后一行的布局问题

flex布局中使用justify-content后,最后一行的布局问题

1、问题

  • 想要的结果:

  • 使用 justify-content: space-between; 后实际结果:

2、解决方法

  1. 在最后追加 nspan 元素。n 的值 = 列数 - 2
  2. 设置span元素的宽度 = flex item 的宽度

span 换成 i 元素也行。

html 复制代码
<!DOCTYPE html>
<html>

  <head>
    <title>Document</title>
      <style>

        .container {
          height: 500px;
          width: 500px;
          background-color: antiquewhite;
          margin: 0 auto;

          display: flex;

          flex-wrap: wrap;
          justify-content: space-between;
        }

        .item {
          width: 150px;
          height: 150px;

          background-color: aquamarine;

        }

         .item1 {
          background-color: orange;
         }

        .item2 {
          background-color: red;
         }

         .item3 {
          background-color: blueviolet;

         }

         .container span {
          width: 150px;
         }
      </style>
    </head>

    <body>
      <div class="container">
          <div class="item item1">box1</div>
          <div class="item item2">box2</div>
          <div class="item item3">box3</div>
          <div class="item item4">box4</div>
          <div class="item item4">box5</div>
          <div class="item item3">box6</div>
          <div class="item item2">box7</div>
          <div class="item item1">box8</div>
          <!-- 列数- 2  = 追加的span个数 -->
          <span></span>
      </div>
    </body>
</html>
相关推荐
skywalk81635 分钟前
Kotti Next的tinyfrontend前端模仿Kotti 首页布局还是不太好看,感觉比Kotti差一点
前端
RopenYuan2 小时前
FastAPI -API Router的应用
前端·网络·python
走粥2 小时前
clsx和twMerge解决CSS类名冲突问题
前端·css
Purgatory0013 小时前
layui select重新渲染
前端·layui
weixin199701080163 小时前
《中国供应商商品详情页前端性能优化实战》
前端·性能优化
赵孝正5 小时前
学习的本质是一个工程闭环:从模仿到内化的四阶段方法论(附风电实战案例)
前端·数据库·学习
Panzer_Jack7 小时前
easy-live2d v0.4.0 — 全面进化的 Live2D Web 开发体验
前端
软弹7 小时前
输入URL之后,都发生了什么
前端