【前端基础】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>
相关推荐
Bigger6 分钟前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude
涵涵(互关)20 分钟前
Naive-ui树型选择器只显示根节点
前端·ui·vue
BY组态29 分钟前
Ricon组态系统最佳实践:从零开始构建物联网监控平台
前端·物联网·iot·web组态·组态
BY组态34 分钟前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·物联网·iot·web组态·组态
SoaringHeart35 分钟前
Flutter进阶:OverlayEntry 插入图层管理器 NOverlayZIndexManager
前端·flutter
放下华子我只抽RuiKe51 小时前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架
IT_陈寒2 小时前
Redis缓存击穿把我整不会了,原来还有这手操作
前端·人工智能·后端
idcu3 小时前
深入 Lyt.js 组件系统:L2 渲染引擎层的核心
前端·typescript
这是程序猿3 小时前
Spring Boot自动配置详解
java·大数据·前端
文心快码BaiduComate3 小时前
干货|Comate Harness Engineering工程实践指南
前端·后端·程序员