html - 使用 Flexbox 强制每行最少元素

标签 html css flexbox

使用 CSS flex,是否可以强制每行包含相同数量的元素?

我能够让元素包装起来,但每行的数量不同 ( Fiddle )。

.container {
    display: flex;
    flex-flow: row wrap;
    position: relative;
}

.container > div {
    padding: 49px;
    flex: 1;
    box-sizing: border-box;
}

这是我要完成的工作的图表:

enter image description here

最佳答案

可能您正在寻找数量查询,您可以根据列表中的多少项更改样式

a list apart article

.container {
  display: flex;
  flex-flow: row wrap;
  border: solid 1px green;
}
.container > div {
  flex: 1 0;
  background-color: lightgreen;
  height: 30px;
  margin: 5px;
}


.item:first-child:nth-last-child(3),
.item:first-child:nth-last-child(3) ~ .item {
  flex-basis: 30%;  
}

.item:first-child:nth-last-child(4),
.item:first-child:nth-last-child(4) ~ .item {
  flex-basis: 40%;  
}
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

关于html - 使用 Flexbox 强制每行最少元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40618171/

相关文章:

javascript - jQuery 可调整大小的奇怪行为

python - Xpath点击根本不起作用

html - 如何将视差元素与标题分开?

iphone分辨率问题

html - 为幻灯片显示内联图像

html - 两行或多行显示柔性

html - 在 Bootstrap 模板中调整图像大小时遇到​​问题

css - Div 高度让我抓狂

css - 在 div 内水平滚动

html - 如何对齐 flexbox 中的元素?