CSS网格重复网格布局

3
我正在创建一个重复的网格系统,在该系统中我需要重复前7个项目的相同结构。Divs A到G生成了我想要的结果,而所有其他div都以正确的列顺序出现,但只有H和M(在新行中的第一项和第六项)没有达到所需高度。
H需要等于I和J结合的高度,M需要等于K和L结合的高度,与A和F相同。

body {
  margin: 40px;
}

.wrapper {
  display: grid;
  grid-template-columns: repeat(3, [col] 1fr);
  grid-template-rows: repeat(10, [row] auto);
  grid-gap: 1em;
  background-color: #fff;
  color: #444;
}

.box {
  background-color: #444;
  color: #fff;
  border-radius: 5px;
  padding: 20px;
  font-size: 150%;
  display: flex;
  align-items: center;
}

.box:nth-of-type(7n+1) {
  grid-column: col / span 2;
}

.box:nth-of-type(7n+3) {
  grid-column: col 3 / span 1;
}

.box:nth-of-type(7n+4),
.box:nth-of-type(7n+5) {
  grid-column: col 1 / span 1;
}

.box:nth-child(7n+6) {
  grid-column: col 2 / span 2;
}

.box:nth-child(7n+7) {
  grid-column: col 1 / span 3;
}

.box:first-child {
  grid-row: row / span 2;
}

.box:nth-child(2) {
  grid-row: row;
}

.box:nth-child(3) {
  grid-row: row 2;
}

.box:nth-child(4) {
  grid-row: row 3;
}

.box:nth-child(5) {
  grid-row: row 4;
}

.box:nth-child(6) {
  grid-row: row 3 / span 2;
}
<div class="wrapper">
  <div class="box">A</div>
  <div class="box">B</div>
  <div class="box">C</div>
  <div class="box">D</div>
  <div class="box">E</div>
  <div class="box">F</div>
  <div class="box">G</div>

  <!--   items with same spans need to be repeted  -->
  <div class="box">H</div>
  <div class="box">I</div>
  <div class="box">J</div>
  <div class="box">K</div>
  <div class="box">L</div>
  <div class="box">M</div>
  <div class="box">N</div>
</div>

1个回答

4

首先我简化了你的代码:

  • 仅使用 nth-child 逻辑来设置行列大小,

  • 删除了 grid-template-rows 和网格线的命名,

现在我们遇到的问题是方块 EF 不在正确的行上:

body {
  margin: 40px;
}

.wrapper {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  /* grid-template-rows: repeat(10, [row] auto); */
  grid-gap: 1em;
  background-color: #fff;
  color: #444;
}

.box {
  background-color: #444;
  color: #fff;
  border-radius: 5px;
  padding: 20px;
  font-size: 150%;
  display: flex;
  align-items: center;
}

.box:nth-of-type(7n+1) {
  grid-column: span 2;
  grid-row: span 2;
}

.box:nth-child(7n+6) {
  grid-column: span 2;
  grid-row: span 2;
}

.box:nth-child(7n+7) {
  grid-column: span 3;
}
<div class="wrapper">
  <div class="box">A</div>
  <div class="box">B</div>
  <div class="box">C</div>
  <div class="box">D</div>
  <div class="box">E</div>
  <div class="box">F</div>
  <div class="box">G</div>

  <!--   items with same spans need to be repeted  -->
  <div class="box">H</div>
  <div class="box">I</div>
  <div class="box">J</div>
  <div class="box">K</div>
  <div class="box">L</div>
  <div class="box">M</div>
  <div class="box">N</div>
</div>

现在您可以使用grid-column: 2 / 4F 移动到最后两列,然后使用grid-auto-flow: dense将其上移 - 请参见下面的演示:

body {
  margin: 40px;
}

.wrapper {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  /*grid-template-rows: repeat(10, [row] auto);*/
  grid-auto-flow: dense; /* fills in the spaces */
  grid-gap: 1em;
  background-color: #fff;
  color: #444;
}

.box {
  background-color: #444;
  color: #fff;
  border-radius: 5px;
  padding: 20px;
  font-size: 150%;
  display: flex;
  align-items: center;
}

.box:nth-of-type(7n+1) {
  grid-column: span 2;
  grid-row: span 2;
}

.box:nth-of-type(7n+5) {
  grid-column: 1;
}

.box:nth-child(7n+6) {
  grid-column: 2 / 4; /* changed */
  grid-row: span 2;
}

.box:nth-child(7n+7) {
  grid-column: span 3;
}
<div class="wrapper">
  <div class="box">A</div>
  <div class="box">B</div>
  <div class="box">C</div>
  <div class="box">D</div>
  <div class="box">E</div>
  <div class="box">F</div>
  <div class="box">G</div>
  <!--   items with same spans need to be repeted  -->
  <div class="box">H</div>
  <div class="box">I</div>
  <div class="box">J</div>
  <div class="box">K</div>
  <div class="box">L</div>
  <div class="box">M</div>
  <div class="box">N</div>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接