css布局方式,实现四宫格,九宫格,16宫格等切换

1.借助absolute方位值,实现自适应的网格布局,用伪元素间隔

absolute+四个方位值撑开局面、float+宽度百分比实现横向排列。高度百分比实现自适应。
最外层的父元素使用absolute负责占位,给子元素们把空间拉开,或者固定宽高也行

效果图
在这里插入图片描述

ul{
      height: 100%;
      padding: 0;
      position:absolute;
      left:0;
      top:0px;
      right:0;
      bottom:0;
      li{
        float:left;
        height:33.3%;
        width:33.3%;
        position:relative;
        /*background-color: #FE6278;*/
        &::before{
          content:'';
          position:absolute;
          left:6px;
          right:6px;
          top:6px;
          bottom:6px;
          border-radius:6px;
          background-color: #666666;
        }
      }
    }

2.直接用grid网格布局

效果图

<ul :class="['box', 'box_9']" ref="container">
        <li :class="['item', i==selectedGridIndex?'item_active':'']"
          v-for="(item, i) in grids" >
        </li>
</ul>
.box{
      display: grid;
      box-sizing: border-box;
      height: 100%;
      padding: 0;
      .item{
        box-sizing: border-box;
        background-color: #666666;
        position: relative;
        border: 1px solid rgba(255, 255, 255, 0.21);
        cursor: pointer;
        position: relative;
        &_active{
          border: 1px solid #FE6278;
        }
      }
    }
    .box_1{
      grid-template-columns: repeat(1, 100%); //1列,占100%
      grid-template-rows: repeat(1, 100%); //1行,占100%
    }
    .box_4{
    //四宫格
      grid-template-columns: repeat(2, 50%); //2列,各占50%
      grid-template-rows: repeat(2, 50%);//2行,各占50%
    }
    .box_9{
    //9宫格
      grid-template-columns: repeat(3, 33.33%);//3列,各占33.33%
      grid-template-rows: repeat(3, 33.33%);//3行,各占33.33%
    }
    .box_16{
      grid-template-columns: repeat(4, 25%);//4列,各占25%
      grid-template-rows: repeat(4, 25%);//4行,各占25%
    }
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值