使用纯CSS创建CSS网格布局 [英] Create CSS grid layout with pure CSS

查看:90
本文介绍了使用纯CSS创建CSS网格布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用CSS Grid来创建布局,例如图像(任何项目都是正方形的):

I trying to create a layout using CSS Grid like the image (any item is square):

我正在尝试的代码:

CSS

  .grid-container {
    padding: 20px;
    display: grid;
    grid-gap: 20px;
    grid-auto-rows: 1fr;
    grid-template-columns: repeat(2, 1fr);
  }

  .item {
    position: relative;
    background: #ccc;
  }

  /* Square */
  .item:after {
    content: '';
    display: block;
    padding-top: 100%;
  }

  @media screen and (min-width: 640px) and (max-width: 1023px) {
    /* 640 ~ 1023 */
    .grid-container {
      grid-template-columns: repeat(3, 1fr);
    }

    .item:nth-child(6n + 1) {
      grid-column: span 2 / 3;
      grid-row: span 2;
    }
    .item:nth-child(6n + 6) {
      grid-column: span 2 / 3;
      grid-row: span 2;
      grid-column: 2 / 4;
    }
    .item:nth-child(6n + 5) {
      grid-column: span 1 / 2;
    }
  }

  @media print, screen and (min-width: 1024px) {
    /* 1024+ */
    .grid-container {
      grid-template-columns: repeat(4, 1fr);
    }

    .item:nth-child(10n + 1) {
      grid-column: span 2 / 3;
      grid-row: span 2;
    }
    .item:nth-child(10n) {
      grid-column: span 2 / 3;
      grid-row: span 2;
      grid-column-end: 5;
    }
    .item:nth-child(10n + 8) {
      grid-column-start: 1;
    }
  }

您可以在这里找到我的代码: JSFiddle

You can find my code here: JSFiddle

结果显示:

我认为将position: absolute与JavaScript一起使用来计算网格的位置可以解决问题.

I think use position: absolute with JavaScript that calculate the girds position can solve problem.

如何使用纯CSS创建此布局?

How to create this layout use pure CSS?

推荐答案

您可以尝试以下操作.您几乎不错,想念grid-auto-flow:dense;不允许该项目填满所有空白.

You can try like below. You were almost good, missing grid-auto-flow:dense; to allow the item to fill all the spaces.

.grid-container {
  padding: 20px;
  display: grid;
  grid-gap: 20px;
  grid-auto-rows: 1fr;
  grid-template-columns: repeat(2, 1fr);
  grid-auto-flow:dense;
  counter-reset: albumList;
}

.item {
  position: relative;
  background: #ccc;
  display:flex;
}

/* Number */
.item:before {
  counter-increment: albumList;
  content: counter(albumList);
  margin:auto;
  font-size: 40px;
  color: #000000;
}

/* Square */
.item:after {
  content: '';  
  padding-top: 100%;
}

@media screen and (min-width: 40em) and (max-width: 63.99875em) {
  /* 640 ~ 1023 */
  .grid-container {
    grid-template-columns: repeat(3, 1fr);
  }
  .item:nth-child(6n + 1),
  .item:nth-child(6n + 6){
    grid-row:span 2;
    grid-column:span 2;
  }
  .item:nth-child(6n + 5) {
    grid-column:1;
  }

}

@media print, screen and (min-width: 64em) {
  /* 1024+ */
  .grid-container {
    grid-template-columns: repeat(4, 1fr);
  }
  .item:nth-child(10n + 1),
  .item:nth-child(10n + 10){
    grid-row:span 2;
    grid-column:span 2;
  }
  .item:nth-child(10n + 8) {
    grid-column:1;
  }
  .item:nth-child(10n + 9) {
    grid-column:2;
  }
}

<div class="grid-container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

这篇关于使用纯CSS创建CSS网格布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆