【问题标题】:CSS Grid - auto generate new rows breaks on auto-flow-columnCSS Grid - 在自动流列上自动生成新的行中断
【发布时间】:2020-12-16 12:39:59
【问题描述】:

我有一个包含一些链接的 CSS 网格。 它是一个三列网格,具有任意数量的行(取决于项目的数量)。
我希望每列中的项目数均匀分布。

列数和行数由以下方式设置:
grid-template-columns: repeat(3, 1fr); // 使其为三列宽
grid-template-rows: repeat(auto-fill); // 自动生成新行

这很好用,除非我尝试让网格按列而不是按行流动:
grid-auto-flow: column; // 这会破坏它

示例:

.container {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(auto-fill);
  /*grid-auto-flow: column;*/ 
}

a {
  height: 50px;
}
<div class="container">
  <a href="#">item one</a>
  <a href="#">item two</a>
  <a href="#">item three</a>
  <a href="#">item four</a>
  <a href="#">item fuve</a>
  <a href="#">item six</a>
  <a href="#">item seven</a>
  <a href="#">item eight</a>
  <a href="#">item nine</a>
  <a href="#">item ten</a>
  <a href="#">item eleven</a>
  <a href="#">item twelve</a>
  <a href="#">item thirteen</a>
  <a href="#">item fourteen</a>
  <a href="#">item fifteen</a>
</div>

如何在我的网格中保留三列,使其通过列而不是行流动,并让它在需要时自动生成这些行?

【问题讨论】:

  • 你需要相同的行距吗?
  • 嘿,感谢您的回答.... 理想情况下我愿意,但是您有什么想法?
  • 我的意思是grid-row-gap 规则
  • 这有什么帮助?我对此没有偏好,它可能是 0....
  • 根据您的问题,出于某种原因,我意识到您需要 grid-row-gap,因为它缩进了某些行。但是现在我不太明白你的问题..

标签: html css css-grid


【解决方案1】:

你不需要 CSS 网格,但 columns:

.container {
  column-count:3;
}

a {
  height: 50px;
  display:block;
}
<div class="container">
  <a href="#">item one</a>
  <a href="#">item two</a>
  <a href="#">item three</a>
  <a href="#">item four</a>
  <a href="#">item fuve</a>
  <a href="#">item six</a>
  <a href="#">item seven</a>
  <a href="#">item eight</a>
  <a href="#">item nine</a>
  <a href="#">item ten</a>
  <a href="#">item eleven</a>
  <a href="#">item twelve</a>
  <a href="#">item thirteen</a>
  <a href="#">item fourteen</a>
  <a href="#">item fifteen</a>
</div>

【讨论】:

  • 只有在所有项目的高度相同时才有效。
猜你喜欢
  • 1970-01-01
  • 2023-03-09
  • 2021-02-20
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
相关资源
最近更新 更多