CSS Grid 重复网格布局

标签 css 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>

最佳答案

首先我简化了你的代码:

  • 仅使用 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>

关于CSS Grid 重复网格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56053054/

相关文章:

html - CSS 网格 : divs are spreading out down the column not across the row

html - 在 Chrome 中不会显示超过 999 行

CSS 网格 - 如何使元素响应

chrome 上的 Jquery .css 问题

html - 子菜单前有空白,怎么回事?

php - 如何使用 CSS 为第一个单词着色

jQuery 切换与 div 层次结构

javascript - 那里有任何 JS polyfill 会采用 RGBA 等属性并将它们转换为粗略的 IE 等价物吗?

CSS 网格 : Start column at specific x position

html - 调整表格中标签/输入的高度