【问题标题】:Don't show wide gap between items when they don't fill the row当项目没有填满行时,不要在项目之间显示很大的差距
【发布时间】:2017-09-18 13:27:56
【问题描述】:

请检查fiddle,当您调整窗口大小并且卡片向下时,卡片的底部看起来很奇怪,它们之间的空间更大。我希望卡片的底部部分与上述卡片对齐。

我知道这可以通过 display: inline-block 轻松完成,但我正在尝试使用 flexbox 来做到这一点,即 display: flex

.parent {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

.child {
  min-width: 200px;
  width: 200px;
  height: 200px;
  box-shadow: 0px 0px 0px 1px black;
}
<div class="parent">
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
</div>

【问题讨论】:

  • justify-content 更改为justify-content: flex-start; 似乎可以解决问题。可以使用margin-bottommargin-right 添加额外的间距。
  • @LennartKloppenburg 然后在调整大小时右侧会有奇怪的间隙..
  • 也许这个话题有帮助:stackoverflow.com/questions/18744164/…

标签: html css flexbox css-grid


【解决方案1】:

对齐 flex 容器中的最后一项可能很棘手。目前 flexbox 规范中没有内置的属性或方法来定位行/列中的最后一项。

但是有很多技巧,包括使用不可见的弹性项目和伪元素来占据空间,从而实现所需的对齐。

这在下面的帖子中都有介绍:


不过,您的布局可以通过 Flex Layout 的姊妹技术 Grid Layout 轻松实现:

.parent {
  display: grid;                                                  /* 1 */
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));   /* 2 */
  grid-auto-rows: 200px;                                          /* 3 */
  grid-gap: 10px;                                                 /* 4 */
}
.child {
  box-shadow: 0px 0px 0px 1px black;
}
<div class="parent">
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
</div>

codepen

注意事项:

  1. 建立块级网格容器。 (spec reference)
  2. 启用网格以创建尽可能多的列以适应屏幕 (auto-fill)。每列的最小宽度可以是 200px,最大宽度可以是1fr(即空闲空间的比例;类似于flex-grow: 1)。 (spec reference)
  3. 网格将自动创建新行以适应环绕网格项。每行的高度为 200px。 (spec reference)
  4. grid-column-gapgrid-row-gap 的缩写。在每个网格项目周围设置 10 像素的“边距”。 (spec reference)

浏览器支持 CSS 网格

  • Chrome - 自 2017 年 3 月 8 日起提供全面支持(版本 57)
  • Firefox - 自 2017 年 3 月 6 日起提供全面支持(版本 52)
  • Safari - 自 2017 年 3 月 26 日起全面支持(版本 10.1)
  • Edge - 自 2017 年 10 月 16 日起提供全面支持(版本 16)
  • IE11 - 不支持当前规范;支持过时版本

这是完整的图片:http://caniuse.com/#search=grid

【讨论】:

  • 感谢您提供这些知识 :)
  • 看起来很棒..有详细的解释。再次感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
  • 2023-01-02
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
相关资源
最近更新 更多