卡片布局自适应:网格布局(display: grid;)

1. 卡片定宽,两侧间距根据容器宽度变化

适合应用在内容上方都是图片的卡片列表上

<template>
  <div class="wrapper">
    <div class="card-list-layout">
      <div v-for="item in cardList" :key="item.id" class="card-item">

      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cardList: []
    }
  },

  created() {
    for (let i = 0; i < 30; i++) {
      this.cardList.push({
        id: i + 1
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  width: 100%;
  height: 100%;
  padding: 10px 0;

  .card-list-layout {
    width: 100%;
    height: 100%;
    display: grid;
    grid-gap: 16px;
    grid-template-columns: repeat(auto-fill, 355px);
    justify-content: center;
    align-content: flex-start;
    overflow: auto;
  }

  .card-item {
    width: 355px;
    height: 172px;
    background: rgb(255, 255, 255);
    border: 1px solid rgba(0, 0, 0, 0.12);
    border-radius: 4px;
    padding: 16px;
  }
}
</style>

2. 两侧间距不变,卡片宽度根据容器宽度变化

适合应用于全文本或图片在左侧的卡片列表上

<template>
  <div class="wrapper">
    <div class="card-list-layout">
      <div v-for="item in cardList" :key="item.id" class="card-item">

      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cardList: []
    }
  },

  created() {
    for (let i = 0; i < 30; i++) {
      this.cardList.push({
        id: i + 1
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  width: 100%;
  height: 100%;
  padding: 20px;

  .card-list-layout {
    width: 100%;
    height: 100%;
    /* 网格布局 */
    display: grid;
    /* 网格行和网格列之间的间距 */
    grid-gap: 16px;
    /* 网格会自动生成多列,每列最小356px,最大可以平分一行的剩余空间。 */
    grid-template-columns: repeat(auto-fill, minmax(356px, 1fr));
    /* grid-template-columns: repeat(auto-fit, minmax(356px, 1fr)); */
    align-content: flex-start;
    overflow: auto;
  }

  .card-item {
    display: flex;
    padding: 16px;
    background: rgb(255, 255, 255);
    border: 1px solid rgba(0, 0, 0, 0.12);
    border-radius: 4px;
    height: 172px;
  }
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值