How to repeat a CSS grid layout pattern?

前端 未结 1 1392
星月不相逢
星月不相逢 2020-12-21 01:55

I\'m trying to make a grid layout with repeating 2 vertical, 2 horizontal sections. Then 2 horizontal, 2 verticals, and we repeat again.

Like this:

相关标签:
1条回答
  • 2020-12-21 02:25

    You may consider many grid container that you repeat like below:

    .container {
      max-width: 940px;
      margin: 0 auto;
      counter-reset: div;
    }
    
    .grid-container {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-auto-rows: 100px;
      grid-gap: 20px;
      margin-bottom:20px;
    }
    .grid-container:nth-child(2n+1) {
      grid-template-areas: 
      "v1 v2 h1 h1" 
      "v1 v2 h2 h2"
    }
    .grid-container:nth-child(2n) {
      grid-template-areas: 
      "v1 v1 h1 h2 " 
      "v2 v2 h1 h2 "
    }
    
    .item-a {
      grid-area: v1;
    }
    
    .item-b {
      grid-area: v2;
    }
    
    .item-c {
      grid-area: h1;
    }
    
    .item-d {
      grid-area: h2;
    }
    
    .grid-container>div {
      box-shadow: 0 0 11px 0 #b9b9b9;
    }
    
    .grid-container>div:before {
      counter-increment: div;
      content: counter(div);
      font-size: 40px;
    }
    <div class="container">
      <div class="grid-container">
        <div class="item-a"></div>
        <div class="item-b"></div>
        <div class="item-c"></div>
        <div class="item-d"></div>
      </div>
      <div class="grid-container">
        <div class="item-a"></div>
        <div class="item-b"></div>
        <div class="item-c"></div>
        <div class="item-d"></div>
      </div>
      <div class="grid-container">
        <div class="item-a"></div>
        <div class="item-b"></div>
        <div class="item-c"></div>
        <div class="item-d"></div>
      </div>
      <div class="grid-container">
        <div class="item-a"></div>
        <div class="item-b"></div>
        <div class="item-c"></div>
        <div class="item-d"></div>
      </div>
      <div class="grid-container">
        <div class="item-a"></div>
        <div class="item-b"></div>
        <div class="item-c"></div>
        <div class="item-d"></div>
      </div>
    
    </div>

    In case you need to have only one grid you can consider the fact that you have a pattern that repeat each 8 elements and use nth-child() like below

    .grid-container {
      max-width: 940px;
      margin: 0 auto;
      counter-reset: div;
      
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-auto-rows: 100px;
      grid-auto-flow: dense;
      grid-gap: 20px;
    }
    
    .grid-container> :nth-child(8n + 1),
    .grid-container> :nth-child(8n + 2),
    .grid-container> :nth-child(8n + 7),
    .grid-container> :nth-child(8n + 8) {
      grid-row: span 2;
    }
    
    .grid-container> :nth-child(8n + 3),
    .grid-container> :nth-child(8n + 4),
    .grid-container> :nth-child(8n + 5) {
      grid-column: span 2;
    }
    
    .grid-container> :nth-child(8n + 6) {
      grid-column: 1/span 2;
    }
    
    
    .grid-container>div {
      box-shadow: 0 0 11px 0 #b9b9b9;
    }
    .grid-container>div:before {
      counter-increment: div;
      content: counter(div);
      font-size: 40px;
    }
    <div class="grid-container">
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </div>

    0 讨论(0)
提交回复
热议问题