使用纯CSS创建CSS网格布局

我试图创建一个布局使用 CSS 网格像图像(任何项目是正方形):

我试图创建一个布局使用 CSS 网格像图像(任何项目是正方形):

CSS grid layout pattern

我正在尝试的代码:

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

Result show: CSS grid layout pattern

我认为使用position: absolute与计算网格位置的 JavaScript 可以解决问题。

如何使用纯 CSS 创建这个布局?

8

你可以像一样尝试。你几乎很好,缺少grid-auto-flow:dense;以允许该项目填充所有空格。

.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: alList;
}
.item {
  aspect-ratio: 1;
  background: #ccc;
  display: flex;
}
/* Number */
.item:before {
  counter-increment: alList;
  content: counter(alList);
  margin: auto;
  font-size: 40px;
  color: #000000;
}
@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-area: span 2/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-area: span 2/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>

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(574)
如何使用WinCE5.0RTOS在ATL应用程序中加载第三方.dll(COMDLL )
上一篇
Excel条件格式与空白和今天 ()
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(40条)