CSS网格重复网格布局

时间:2019-05-09 06:00:35

标签: css css3 css-grid

我正在创建一个重复网格系统,在该系统中,我需要重复与前7个项目相同的结构。 div 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 个答案:

答案 0 :(得分:1)

首先,我简化了您的代码:

  • 仅使用 nth-child 逻辑进行行列大小

  • 删除了condition,并命名了网格线,

我们现在遇到的问题是,grid-template-rowsE框在行中不合适:

F
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>F移至最后两列,然后使用grid-column: 2 / 4将其拉起-请参见下面的演示

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