美文网首页
CSS Grid Brickwall

CSS Grid Brickwall

作者: JunChow520 | 来源:发表于2020-05-20 21:58 被阅读0次

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;
    }
}

相关文章

  • CSS Grid Brickwall

    Brickwall砖墙布局典型的案例是照片墙,砖墙布局过去往往会使用绝对定位来实现,但这样做难以维护且每个块的位置...

  • CSS Grid 布局

    参考资料 CSS Grid 布局完全指南(图解 Grid 详细教程) CSS Grid 系列(上)-Grid布局完...

  • CSS Grid 布局完全指南1-grid基础知识

    CSS Grid 布局是 CSS 中最强大的布局系统。与 flexbox 的一维布局系统不同,CSS Grid 布...

  • 网格布局

    CSS Grid现在已经被W3C纳入到CSS3的一个布局模块当中,被称为**CSS Grid Layout Mod...

  • 快速开始grid布局

    Grid布局概念 CSS Gird已经被W3C纳入到css3的一个布局模块中,被称为CSS Grid Layout...

  • CSS Grid 网格布局

    CSS Grid 网格布局教程

  • Creating Your Own CSS Grid Syste

    Creating Your Own CSS Grid System CSS Grids have been aro...

  • 为什么要使用CSS Grid?

    关注的几个博客上最近关于CSS Grid的文章挺多。今天看了Jen Simmons关于CSS Grid的一个演讲之...

  • CSS Grid网格布局

    参考资料 CSS Grid 网格布局教程 - 阮一峰 概述 网格布局(Grid)是最强大的 CSS 布局方案。 它...

  • 2019-03-17学了啥

    CSS Grid布局https://cssgridgarden.com/

网友评论

      本文标题:CSS Grid Brickwall

      本文链接:https://www.haomeiwen.com/subject/fczmohtx.html