CSS Grid Brickwall

Brickwall砖墙布局典型的案例是照片墙,砖墙布局过去往往会使用绝对定位来实现,但这样做难以维护且每个块的位置都会影响到另一个,最为致命的是无法自适应宽度。

照片墙

使用Grid网格布局实现Brickwall砖墙布局

砖墙布局
<style>
.brickwall {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    grid-auto-flow: row dense;
    grid-auto-rows: minmax(120px, auto);
    grid-gap: 0.25em;
}
.item{height:100%; background-color:#222; color:#fff;}
.item:nth-of-type(9n + 1){grid-column: auto / span 2;}
.item:nth-of-type(9n + 2){grid-column: auto / span 2;}
.item:nth-of-type(9n + 3){grid-column: auto / span 3;}
.item:nth-of-type(9n + 4){grid-column: auto / span 2;}
.item:nth-of-type(9n + 5){grid-column: auto / span 3;}
.item:nth-of-type(9n + 6){grid-column: auto / span 2;}
.item:nth-of-type(9n + 7){grid-column: auto / span 3;}
.item:nth-of-type(9n + 8){grid-column: auto / span 2;}
.item:nth-of-type(9n + 9){grid-column: auto / span 2;}
</style>
<div class="container">
  <h2 class="title">Resposive Brickwall</h2>
  <div class="brickwall">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
  </div>
</div>

定义砖墙布局的弹性容器

.brickwall {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    grid-auto-flow: row dense;
    grid-auto-rows: minmax(120px, auto);
    grid-gap: 0.25em;
}

完整代码

.container{
  height:100vh;
  display: grid;
  grid-template-columns: 50vw;
  justify-content: center;
  padding: 3em;
  background-color:#eee;
}
.title{
  justify-self: center;
  margin: 3em 0;
  font: 1em monospace;
  background-color:#ddd;
}
.brickwall {
    display: grid;
    grid-gap: 0.25em;
    grid-template-columns: 1fr;
    grid-auto-flow: row dense;
    grid-auto-rows: minmax(133px, 20vmin);
}

.brickwall .item {
    height: 100%;
    background: #222;
    color: #ddd;
}
@media (min-width: 512px) {
    .brickwall {
        grid-template-columns: repeat(7, 1fr);
    }
    .brickwall .item:nth-of-type(9n + 9) {
        grid-column: auto / span 2;
    }
    .brickwall .item:nth-of-type(9n + 8) {
        grid-column: auto / span 2;
    }
    .brickwall .item:nth-of-type(9n + 7) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(9n + 6) {
        grid-column: auto / span 2;
    }
    .brickwall .item:nth-of-type(9n + 5) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(9n + 4) {
        grid-column: auto / span 2;
    }
    .brickwall .item:nth-of-type(9n + 3) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(9n + 2) {
        grid-column: auto / span 2;
    }
    .brickwall .item:nth-of-type(9n + 1) {
        grid-column: auto / span 2;
    }
}

@media (min-width: 512px) and (min-width: 1024px) {
    .brickwall {
        grid-template-columns: repeat(14, 1fr);
    }
    .brickwall .item:nth-of-type(15n + 15) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 14) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 13) {
        grid-column: auto / span 2;
    }
    .brickwall .item:nth-of-type(15n + 12) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 11) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 10) {
        grid-column: auto / span 2;
    }
    .brickwall .item:nth-of-type(15n + 9) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 8) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 7) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 6) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 5) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 4) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 3) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 2) {
        grid-column: auto / span 3;
    }
    .brickwall .item:nth-of-type(15n + 1) {
        grid-column: auto / span 2;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值