【前端基础】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>
相关推荐
@PHARAOH11 分钟前
WHAT - Remix 入门和基本实践:理解两套产物
前端·remix
mayaairi1 小时前
Vue2 生命周期完全指南:从创建到销毁的8个钩子函数
前端·javascript·vue.js
xy34532 小时前
axure9.0 如何打造一个计时器(简单版)
前端·ui·html·axure·原型·产品设计
IMPYLH2 小时前
HTML 的 <pre> 元素
前端·javascript·html
tsumikistep3 小时前
【前端】Vue + Axios 跨域问题实战:7070 代理转发 8080 + 401 报错分析(黑马外卖)
前端·javascript·vue.js
lichenyang4533 小时前
鸿蒙首页从等高商品 Grid 到双列瀑布流:同一张图也能做出小红书式浏览节奏
前端
里欧跑得慢3 小时前
Flutter主题与样式详解
前端·css·flutter·web
toooooop83 小时前
智慧文旅数字人体验平台demo html
css·css3
计算机魔术师3 小时前
TPU推理性价比翻50%,NVIDIA的护城河要见底了?
前端
IT_陈寒4 小时前
Python的多线程就是个假把式,我算是体验到了
前端·人工智能·后端