css问题:display:flex布局+justify-content: space-between; 最后一行不能左对齐

解决方法1: display: flex;+margin: 10px var(--leftRight);

 --leftRight: 动态计算一行减去item的宽度后剩下的间距 

解决方法2:网格布局 display: grid;grid-template-columns: repeat(5, 1fr);+margin: 10px auto; 

完整代码:

<template>

  <div class="flex">

    <h1>问题:flex布局+justify-content: space-between; 最后一行不能左对齐</h1>

    <div class="box">

      <div class="box_item" v-for="(v, i) in list" :key="i"></div>

    </div>

    <h1>方法1:display: flex;+margin: 10px var(--leftRight);</h1>

    <div class="box1">

      <div class="box1_item" v-for="(v, i) in list" :key="i"></div>

    </div>

    <h1>方法2:网格布局 display: grid;grid-template-columns: repeat(5, 1fr);+margin: 10px auto;</h1>

    <div class="box2">

      <div class="box2_item" v-for="(v, i) in list" :key="i"></div>

    </div>

  </div>

</template>

<script>

export default {

  data () {

    return {

      list: 12

    }

  },

  mounted () { },

  methods: {},

}

</script>

<style lang="scss" scoped>

.flex {

  width: 100%;

  height: calc(100vh - 90px);

  padding: 5px;

  h1 {

    padding-left: 10px;

  }

  .box {

    display: flex;

    justify-content: space-between;

    flex-wrap: wrap;

    width: 100%;

    padding-top: 20px;

    .box_item {

      width: 50px;

      height: 50px;

      background-color: blue;

      border: solid 1px #000;

      box-sizing: border-box;

      --n: 5;

      /* 一行几个 */

      --space: calc(100% - var(--n) * 50px);

      /* 一行减去item的宽度后剩下的间距 */

      --leftRight: calc(var(--space) / var(--n) / 2);

      /* 每个item左右的间距 */

      margin: 10px var(--leftRight);

    }

  }

  .box1 {

    display: flex;

    flex-wrap: wrap;

    width: 100%;

    padding-top: 20px;

    .box1_item {

      width: 50px;

      height: 50px;

      background-color: blue;

      border: solid 1px #000;

      box-sizing: border-box;

      --n: 5;

      /* 一行几个 */

      --space: calc(100% - var(--n) * 50px);

      /* 一行减去item的宽度后剩下的间距 */

      --leftRight: calc(var(--space) / var(--n) / 2);

      /* 每个item左右的间距 */

      margin: 10px var(--leftRight);

    }

  }

  .box2 {

    display: grid;

    grid-template-columns: repeat(5, 1fr);

    width: 100%;

    padding-top: 20px;

    .box2_item {

      width: 50px;

      height: 50px;

      background-color: blue;

      border: solid 1px #000;

      box-sizing: border-box;

      margin: 10px auto;

    }

  }

}</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值