css - Flexbox - 第二行垂直居中

标签 css flexbox vertical-alignment centering

我想将第二行放在容器的中心,但我不知道该怎么做。 align-self 仅在水平方向上有效 - 即使 flex-direction 设置为“column”。

.first, .second {
  height: 50px;
  width: 100%;
}

.first {
  background-color: wheat;
}

.second {
  align-self: center;
  background-color: purple;
}

.container {
  background-color: silver;
  height: 300px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}
<div class="container">
  <div class="first">
    first
  </div>
  <div class="second">
    second
  </div>
</div>

最佳答案

很难看出实际发生了什么,因为 .first.second 的宽度设置为 100%。如果将它们设为 50x50 正方形,您可以看到 align-self 在父容器的横轴上工作,因此在本例中它使 .second 水平居中。

一个可行的解决方案是在 .second 中嵌套另一个 flexbox,如下所示:https://jsfiddle.net/Lygdk4xo/

关于css - Flexbox - 第二行垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52433117/

相关文章:

html - 如何使用 CSS 为所有浏览器垂直居中 div 元素?

jquery - 需要对我的响应式 slider 进行一些编辑

javascript - 如何检查表的父类

html - 从哪里获得能够为 IE 5,6、firefox 3 设置按钮样式的 CSS 样式?

html - Flexbox 等高不工作

html - 垂直居中表格最好是 CSS

css - 我的类别页面中的对象未垂直对齐

javascript - Bootstrap 选项卡事件选项卡颜色

html - 让 flexbox 行的高度由其子元素之一确定

html - 仅CSS的砌体布局