【前端基础】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>
相关推荐
十八朵郁金香8 分钟前
深入解析:ES6 中 class 与普通构造器的区别
前端·ecmascript·es6
索迪迈科技19 分钟前
CommonJS与ES6模块的区别
前端·ecmascript·es6
前端Hardy21 分钟前
12个被低估的 CSS 特性,让前端开发效率翻倍!
前端·javascript·css
前端Hardy30 分钟前
HTML&CSS:精美的3D折叠卡片悬停效果
前端·javascript·css
nightunderblackcat30 分钟前
新手向:中文语言识别的进化之路
前端·javascript·easyui
Spider_Man1 小时前
打造属于你的前端沙盒 🎉
前端·typescript·github
用户47949283569151 小时前
🤫 你不知道的 JavaScript:`"👦🏻".length` 竟然不是 1?
前端·javascript·面试
掘金一周1 小时前
凌晨零点,一个TODO,差点把我们整个部门抬走 | 掘金一周 9.11
前端·人工智能·后端
用户8174413427481 小时前
kubernetes核心概念 Service
前端
东北南西2 小时前
Web Worker 从原理到实战 —— 把耗时工作搬到后台线程,避免页面卡顿
前端·javascript