html - 如何在 flex 布局中换行?

标签 html css flexbox

我有第一个、第二个和第三个元素,然后我希望第四个元素转到下一行,无论空间有多宽。

.box {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-around;
  align-items: flex-start;
}
.it {
  max-width: 420px;
}
<div class="box">
  <div class="it">1</div>
  <div class="it">2</div>
  <div class="it">3</div>
  <div class="it">4</div>
</div>

最佳答案

可以在合适的位置插入宽伪元素:

.flex-container {
  display: flex;
  flex-wrap: wrap;
}
.flex-container::after {
  content: '';
  width: 100%;
}
.flex-item:last-child { /* or `:nth-child(n + 4)` */
  order: 1;
}
<div class="flex-container">
  <div class="flex-item">1</div>
  <div class="flex-item">2</div>
  <div class="flex-item">3</div>
  <div class="flex-item">4</div>
</div>

关于html - 如何在 flex 布局中换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755460/

相关文章:

html - 什么会导致 CSS 在网页上显示为文本?

javascript - jQuery - 遍历样式

javascript - 使用 jquery 的上下文菜单

php - 样式表未加载。 URL 重复点击

html - XML/XSLT - XML 解析错误 : no element found

javascript - 不知道为什么日期选择器小部件不起作用,也不知道为什么 id 选择器不能自动填充结果字段

JavaScript try catch 语句

html - 将 flexbox 容器缩小到内容大小

html - Flexbox:使内联组件扩展父容器的整个宽度

CSS Validator 无法识别 flex-wrap : wrap;