html - 嵌套的 Flexbox 破坏父级

标签 html css flexbox

我一直在尝试实现一个简单的嵌套 flexbox 函数。我遇到了一个问题,我似乎可以有两个级别(垂直列 -> 水平行),但如果我试图超出它,(垂直 -> 水平 -> 垂直)它会破坏容器高度。

我有一个托管的 CodePen here这更好地描述了我的问题。 http://codepen.io/FrederickGeek8/pen/xGoPXd

编译后的 HTML:

<div class="workspace">
  <div class="vertical">
    <div class="item">
      <div class="horizontal">
        <div style="background:pink" class="item"><a>Up</a></div>
        <div class="item">
          <div class="vertical">
            <div style="background:red" class="item"><a>Left</a></div>
            <div style="background:orange" class="item"><a>Strange</a></div>
          </div>
        </div>
        <div style="background:blue" class="item"><a>Another</a></div>
      </div>
    </div>
    <div class="item"><a>Right</a></div>
    <div style="background:green" class="item"><a>Dang</a></div>
  </div>
</div>

编译后的 CSS(在 CodePen 上自动添加前缀):

.workspace {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.vertical {
  flex: 1;
  display: flex;
  flex-direction: row;
  background: grey;
  width: 100%;
  height: 100%;
}
.vertical > .item {
  flex: 1;
  display: flex;
  flex-direction: row;
  width: 100%;
  height: 100%;
  margin: auto;
  border-left: 1px solid #181a1f;
  border-bottom: 1px solid #181a1f;
  flex-grow: 1;
}

.horizontal {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: grey;
  width: 100%;
  height: 100%;
}
.horizontal > .item {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  margin: auto;
  border-left: 1px solid #181a1f;
  border-bottom: 1px solid #181a1f;
  flex-grow: 1;
}

PS:我将运行成品的平台是一个打包的 Chromium 实例。

最佳答案

我删除了一些不必要的 height: 100%margin: auto

http://codepen.io/anon/pen/waLPzR

.workspace {
  position:absolute;
  top:0;
  left:0;
  width:100%;
  height:100%;
}

.vertical {
  flex:1;
  display: flex;
  flex-direction: row;
  background:grey;
  width:100%;
  height: 100%;

  > {
    .item {
      flex:1;
      display: flex;
      flex-direction: row;
      width:100%;
      border-left: 1px solid #181a1f;
      border-bottom: 1px solid #181a1f;
      flex-grow: 1;
    }
  }
} 
.horizontal {
  flex:1;
  display: flex;
  flex-direction: column;
  background:grey;
  width:100%;

   > {
    .item {
      flex:1;
      display: flex;
      flex-direction: column;
      width:100%;
      border-left: 1px solid #181a1f;
      border-bottom: 1px solid #181a1f;
      flex-grow: 1;
    }
  }
}

关于html - 嵌套的 Flexbox 破坏父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32127466/

相关文章:

html - Chrome 上的特殊字符渲染

css - Sass循环函数导入文件

android - 设置状态栏时 View 隐藏

html - 调整浏览器窗口大小时如何防止部分重叠?

asp.net - 如何在保持文本区域的 "clickability"的同时通过超链接添加文本?

html - 如何删除内联/内联 block 元素之间的空间?

html - 当 flex div 的 1 项在另一个 flex div 中时,如何一直正确地 flex 它?

javascript - 更改鼠标光标下部分图像的颜色

html - 文本换行到第二行,在无序列表中向上推 img

javascript - 使用两个 Div 链接水平滚动