使用"display: grid"使父级元素扩展到子元素的宽度。

5

编辑:不确定这是否为重复问题?我不希望背景颜色超出容器的宽度,我希望容器会扩展到其display:grid子元素的大小。更新示例以更好地解释我的问题。

我正在构建一个表格,其中每行都是CSS网格。我这样做是为了利用每个单元格的minmax(),使整个表格在单元格无法再缩小时可滚动,同时允许表格在有更多空间时增长。

这个功能很好,除了行的样式只适用于容器的宽度之外。

请查看这个示例:

.container {
  width: 430px;
  background: grey;
  overflow-x: scroll;
}

.row {
  display: grid;
  grid-template-columns: minmax(200px, 1fr) minmax(200px, 1fr) minmax(200px, 1fr) minmax(100px, 1fr);
  background: red;
  height: 3rem;
  margin: 0.5rem;
}

.cell {
  border: 1px solid black;
}
Scroll to the right inside the box:
<div class="container">
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
</div>
Elements with the <code>.row</code> class should expand to fit all the cells.

有没有什么办法可以解决这个问题?如果需要的话,我可以添加额外的元素!


为什么不在每个单元格中使用红色背景,而不是整行? - Αntonis Papadakis
每一行都应该有渐变背景和圆角边框,这是我的设计要求。如果可能的话,将样式设置为整个行会更容易。 - Amygdaloideum
2个回答

3
将行的显示方式更改为 inline-grid 看起来会有所帮助:

.container {
  width: 430px;
  background: grey;
  overflow-x: scroll;
}

.row {
  display: inline-grid;
  grid-template-columns: minmax(200px, 1fr) minmax(200px, 1fr) minmax(200px, 1fr) minmax(100px, 1fr);
  background: red;
  height: 3rem;
}

.cell {
  border: 1px solid black;
}
Scroll to the right inside the box:
<div class="container">
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
</div>
The red background should cover the whole row.

更新 1

为避免在宽容器和窄子元素处于一行的问题(如上述解决方案),您可以使用更复杂的解决方案,该方案使用父元素上的display: flex; flex-direction: column,并附加align-items: start,强制行元素具有完整的宽度(与默认值stretch相反,后者使行宽度不超过容器)。

.container {
  width: 430px;
  background: grey;
  overflow-x: scroll;
  display: flex;
  flex-direction: column;
  align-items: start;
}

.container.container-wide{
  width: 1000px;
}

.row {
  display: grid;
  grid-template-columns: minmax(200px, 1fr) minmax(200px, 1fr) minmax(200px, 1fr) minmax(100px, 1fr);
  background: red;
  height: 3rem;
}

.cell {
  border: 1px solid black;
}
Scroll to the right inside the box:
<div class="container">
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
</div>
Wide container:
<div class="container container-wide">
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
</div>
The red background should cover the whole row.

更新2

如果行宽大于所有列的宽度之和,为了使行可以延展至容器的全宽度,可以通过将Update 1中的display: flex替换为display: grid来调整解决方案,如下所示:

.container {
  width: 430px;
  background: grey;
  overflow-x: scroll;
  display: grid;
}

.container.container-wide{
  width: 1000px;
}

.row {
  display: grid;
  grid-template-columns: minmax(200px, 1fr) minmax(200px, 1fr) minmax(200px, 1fr) minmax(100px, 1fr);
  background: red;
  height: 3rem;
}

.cell {
  border: 1px solid black;
}
Scroll to the right inside the box:
<div class="container">
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
</div>
Wide container:
<div class="container container-wide">
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
  <div class="row">
    <span class="cell">cell 1</span>
    <span class="cell">cell 2</span>
    <span class="cell">cell 3</span>
    <span class="cell">cell 4</span>
  </div>
</div>
The red background should cover the whole row.


问题在于,如果容器变得足够宽,行将会相互靠近。请参见此fiddle:https://jsfiddle.net/3n0ca6hy/ - Amygdaloideum
添加了一个更新,使用不同的解决方案,而不是使用 inline-*,而是使用容器的 display: flex - Piotr Wicijowski
谢谢!但现在的问题是,表格不会像没有使用align-items: start时那样扩展到单元格的最小宽度之外。也许我想要实现的是不可能的。 - Amygdaloideum
我又尝试了一次这个问题,请看我的更新。 - Piotr Wicijowski

0
如果您需要使用单元格宽度填充grid-template-columns,您可以尝试以下方法:
.row {
  display: grid;
  grid-template-columns: auto auto auto auto;
  background: red;
  height: 3rem;
}

每个单元格的宽度为200像素,只需添加:

.cell {
    min-width: 200px;
    border: 1px solid black;
}

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