css - 堆叠式 flex 盒垂直居中在 Chrome 中不起作用

标签 css google-chrome height flexbox

以下情况在 Google Chrome 中不起作用;

<div class="flex-container">
    <div class="flex-item">
      <div>
        <div>
           <h2>This is a test</h2>
        </div>
       </div>
    </div>
    <div class="flex-item">
      <div>
        <div>
           <h2>This is a test</h2>
        </div>
       </div>
    </div>
    <div class="flex-item">
      <div>
        <div>
           <h2>This is a test</h2>
        </div>
       </div>
    </div>
</div>

SCSS

body,html {
  height:100%;
  background:#1D1F20;
  font-family:sans-serif;
  color:#fff;
}
.flex-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  height:100%;
  .flex-item {
    flex: 1 1 auto;
    align-self: center;
    background:#87BEB7;
    padding:0 10px;
    &:nth-child(2) {
      background:#ADBEBC;
    }
    &:nth-child(3) {
      background:#BE8A74;
    }
    > div {
      display: flex;
      justify-content: center;
      align-items: flex-start;
      height:100%; // This seems to be the issue
      div {
        flex: 0 1 auto;
        align-self: center;
        background:#5C726F;
        padding:10px
      }
    }
  }
}

我希望第二个 flexbox-container (.flex-item > div) 是 flex-child (.flex-item) 的完整高度,但它似乎不起作用。 (仅限 Chrome )

我的解决方法涉及使用绝对位置,但我不想使用它。

代码笔:http://codepen.io/anon/pen/QwpzLX

在 Firefox 中打开以查看所需结果,在 Chrome 中打开以查看当前情况。

如有任何帮助或建议,我们将不胜感激。

最佳答案

这是工作的 scss:

.flex-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    height:100%;
    .flex-item {
        flex: 1 1 auto;
        align-self: center;
        background:#87BEB7;
        padding:0 10px;
        display: flex;

        &:nth-child(2) {
            background:#ADBEBC;
        }
        &:nth-child(3) {
            background:#BE8A74;
        }
        > div {
            display: flex;
            justify-content: center;
            align-items: flex-start;

            div {
                flex: 0 1 auto;
                align-self: center;
                background:#5C726F;
                padding:10px
            }
        }
    }
}

您必须将 display:flex; 设置为 .flex-item 并删除 height: 100%

这是演示:http://jsfiddle.net/omgL91r8/1/

关于css - 堆叠式 flex 盒垂直居中在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27982046/

相关文章:

javascript - 午夜倒计时,每天刷新

javascript - 窗口警报或类似的电子邮件表单?

javascript - Chrome 浏览器 GCM - TOO_MANY_REGISTRATIONS - 如何取消注册/清除所有已注册的应用 ID (registration.pushManager.subscribe)

google-chrome - 当用户单击按钮时如何执行 JavaScript

twitter-bootstrap-3 - Bootstrap 网格偏移行高扩展

css - 如何在 wordpress 上添加 css 类?

html - Firefox 缩放自己的网站

javascript - 在 Google Chrome 中创建面板

javascript - 单击时可折叠的 div 高度问题

javascript - 如何获取图像高度并将其添加到包含的 Javascript 中