【问题标题】:How can I make flex items appear one after another vertically instead of equal height?如何使弹性项目垂直而不是等高出现?
【发布时间】:2026-02-17 18:00:01
【问题描述】:

出于某种原因,我在一个 flex 容器中的 3 个孩子每个都具有相同的高度,但期望的行为是让项目一个接一个地出现,而不是填满它们给定的空间。见:

.col-md-12 {
  position: relative;
  width: 100%;
  min-height: 1px;
  flex: 0 0 100%;
  max-width: 100%;
}

.container {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  flex-wrap: wrap;
  position: fixed;
  height: 100vh;
  width: 420px;
  background: #313131;
  padding: 0;
}

.item {
  display: flex;
}

.one {
  background: red;
}

.two {
  background: green;
}

.three {
  background: yellow;
}
<div class="container">
  <div class="item one col-md-12"></div>
  <div class="item two col-md-12"></div>
  <div class="item three col-md12"></div>
</div>

如您所见,三行各占父母身高的 33.3%。一个在顶部,一个在中间,一个在底部。

但它应该是一个接一个的 3 行。所以,由此(灰色是孩子的内在内容):

对此(背景灰色为父级):

我怎样才能做到这一点?

【问题讨论】:

  • 也许一张图片来描述您需要实现的目标会更容易理解。
  • @AlexYepes 刚刚做到了!
  • 那么,从你问题中的那两张图片中......你想实现底部的那一张吗?中间的那个大正方形代表占据其父级高度的 3 条线?

标签: css flexbox


【解决方案1】:

只需添加flex-flow: column

.col-md-12 {
  position: relative;
  width: 100%;
  height: 1px;
}

.container {
  display: flex;
  flex-flow: column;
  position: fixed;
  height: 100vh;
  width: 420px;
  background: #313131;
  padding: 0;
}

.one {
  background: red;
}

.two {
  background: green;
}

.three {
  background: yellow;
}
<div class="container">
  <div class="item one col-md-12"></div>
  <div class="item two col-md-12"></div>
  <div class="item three col-md-12"></div>
</div>

【讨论】:

  • 好吧,但是孩子们需要成为flex
  • 对。修正了上面的答案^
  • 我添加了flex-flow: column,这使得弹性元素在列中流动。我建议阅读文档以获取更多信息:developer.mozilla.org/en-US/docs/Web/CSS/flex-flow
  • 因为在子元素上删除了flex
  • 不,这与它没有任何关系。看,我改变了上面的答案,所以子元素有flex,它的工作原理是一样的。
【解决方案2】:

您可以将换行更改为不换行或将宽度更改为 33.3%

 .col{
   width: 100%; 
   height: 100%;
 }

.container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  flex-wrap: no-wrap;
  position: fixed;
  height: 100vh;
  width: 420px;
  background: #313131;
  padding: 0;display: flex;
}

.one {
  background: red;
}

.two {
  background: green;
}

.three {
  background: yellow;
}
<div class="container">
  <div class="col one"></div>
  <div class="col two"></div>
  <div class="col three"></div>
</div>

【讨论】:

  • 感谢您的回答,但正如我所指定的,它是“垂直”的,而且,我不能在孩子身上设置高度。
  • 我检查了一下,我需要做的就是 flex-direction: column;检查sn-p