【问题标题】:Specify number of columns and still have row wrap in CSS Grid指定列数并在 CSS Grid 中仍然有换行
【发布时间】:2019-01-08 23:36:03
【问题描述】:

我应该使用 12 列的 CSS 网格。当视口变小时,我也应该将表格堆叠在一列中。

这可以通过 CSS 网格实现吗?我阅读了换行,我必须使用auto-fillauto-fit。但我使用这两个中的任何一个,我不能指定 12 列。

body {
  margin: 40px;
}

.wrapper {
  display: grid;
  grid-gap: 2px;
  grid-template-columns: repeat(12, 1fr);
  background-color: #fff;
  color: #444;
}

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

.header {
  grid-column: 1 /12;
}

.table1 {
  grid-column: 1 / 7;
  grid-row: auto / span 20;
}

.table2,
.table3 {
  grid-column: 7/ 12;
  grid-row: auto;
}
<div class="wrapper">
  <div class="box header">header</div>
  <div class="box table1">table 1</div>


  <div class="box table2">table 2</div>
  <div class="box table3">table 3</div>

</div>

【问题讨论】:

  • 使用媒体查询并更改小屏幕上的列
  • 我希望尽可能避免这种情况。

标签: css css-grid


【解决方案1】:

您不能使用auto-fillauto-fit 指定固定数量的轨道。

§ 7.2.2.2. Repeat-to-fill: auto-fill and auto-fit repetitions

auto-fillauto-fit作为重复数时,如果网格 容器在相关轴上有一个确定的大小或最大大小,然后 重复次数是最大可能的正整数 这不会导致网格溢出其网格容器。

如果任何重复次数会溢出,则重复 1 次。

考虑到the current (Level 1) version of CSS Grid 的限制,实现布局的最佳方式是使用带有自己的set of limitationsflexbox (demo),或者使用粗略和不优雅老式媒体查询的强大功能。

【讨论】:

猜你喜欢
  • 2017-09-25
  • 2017-06-14
  • 2020-06-09
  • 2022-01-11
  • 2021-09-27
  • 1970-01-01
  • 2017-12-17
  • 2021-09-12
  • 1970-01-01
相关资源
最近更新 更多