使 CSS 网格中的列跨越所有行(包括隐式行)

fre*_*ill 7 html css css-grid

我正在尝试使网格列跨越每个行,包括隐式行。

我遇到了这个问题,询问如何跨越所有网格行。第二个答案有一个更正,说明了更好的解决方案。这似乎可行,但我自己的示例以及对第二个答案的评论表明它不起作用。

W3规格给出了一个非常接近的例子。

我的代码是否有问题,或者这可能是 Firefox、ChromeSafari 中的错误?

我在 CodePen 中也有这个例子

* {
  box-sizing: border-box;
}

.container {
  border: 1px solid #666;
  max-width: 1000px;
  padding: 10px;
  display: grid;
  grid-template-columns: 150px 1fr 300px;
  /* grid-template-rows: repeat(auto) [rows-end]; Doesn't seem to help */
  /* grid-template-rows: [rows-start] repeat(auto) [rows-end]; Doesn't seem to help */
  grid-template-rows: repeat(auto);
  grid-gap: 10px;
  margin: 10px auto;
  grid-auto-flow: row dense;
  /*   justify-items: stretch; */
  /*   align-items: stretch; */
}

.container>* {
  grid-column: 2 / 3;
  padding: 10px;
  outline: 1px solid #666;
}

.pop {
  grid-column: 1 / 2;
  /* grid-column: 1 / -1; If I switch to this, this div will span the full width of the grid, which is exactly what I'm trying to do with rows*/
}

.tertiary {
  grid-column: 1 / 2;
}

.secondary {
  grid-column: 3 / 3;
  grid-row: 1 / -1;
  /* Doesn't work */
  /* grid-row: rows-start / rows-end; Doesn't work */
  /* grid-row: 1 / rows-end; Also doesn't work */
  /* grid-row: 1 / span 7; This works, but I need to span an unknown number of rows*/
  /* grid-row: 1 / span 99; This is gross and creates 99 rows */
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="secondary">Secondary - why doesn't this span all the way to the bottom of the grid?</div>
  <div class="tertiary">Tertiary</div>
  <div class="tertiary">Tertiary</div>
  <div class="tertiary">Tertiary</div>
  <div>Primary</div>
  <div>Primary</div>
  <div>Primary</div>
  <div class="pop">Span tertiary and primary</div>
  <div>Primary</div>
  <div class="tertiary">Tertiary</div>
  <div>Primary</div>
  <div>Primary</div>
</div>
Run Code Online (Sandbox Code Playgroud)

Mic*_*l_B 5

你的路上有两个障碍。

首先,规则中的这行 CSS 代码.container

grid-template-rows: repeat(auto);
Run Code Online (Sandbox Code Playgroud)

该代码无效。符号中的参数repeat()必须以正整数开头,它指定重复次数。你没有这个,所以代码不起作用。规格中的更多详细信息

其次,即使上面的代码是正确的,我们也可以说:

grid-auto-rows: auto; (which happens to be the default setting anyway)
Run Code Online (Sandbox Code Playgroud)

您的列仍然不会跨越所有行。

这是因为,正如您在引用的其他答案中可能已经看到的那样,可以将轨道定义设置为仅覆盖显式网格中的所有垂直轨道所有垂直轨道。

所以这会起作用:

grid-template-rows: repeat(6, auto);
Run Code Online (Sandbox Code Playgroud)

修改后的演示

问题的其余部分已在您引用的其他答案中详细介绍。

  • 感谢您对无效行代码的澄清。所以我回到了我实际的原始需求 - 当存在“未知”行数时,有没有办法使第三列一直延伸到底部? (2认同)