如何使用flexbox垂直对齐页面上的div

Eri*_*icC 14 html css css3 flexbox

以下内容不是将div与浏览器选项卡中的文本垂直对齐(它正确地水平对齐):

<div style="height: 100%; display: flex; align-items: center; justify-content: center;">

  <div>
    Some vertical and horizontal aligned text
  </div>

</div>
Run Code Online (Sandbox Code Playgroud)

怎么了?

Sya*_*lai 30

问题在于您给父容器的高度.在height :100%不占用整个高度.改变100vh或喜欢那样

这是更新的演示

.container{
  height: 100vh; 
  display: flex; 
  align-items: center; 
  justify-content: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div>
    Some vertical and horizontal aligned text
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)