overflow:嵌套的flexbox中的auto

时间:2015-06-18 14:51:34

标签: css html5 flexbox

我正在尝试使用flexbox布局并遇到问题,当我嵌套多个flexbox时,overflow不会被应用。

只要我使用一级Flex框(参见示例1),一切正常:只要内容超出给定空间,滚动条就会应用到红框(#top)。 / p>

但是,如果我引入另一层flexbox(参见示例二),则蓝色框(#right)上没有滚动条。相反,滚动条显示在body元素上,完全忽略蓝色框上的overflow: auto设置。

所以我的问题是:如何在使用嵌套的flexbox时让overflow工作?

备注:我使用Chrome45,Firefox 40和IE11进行了测试。所有行为都是一致的。

以下是两种情况的代码和小提琴链接。

(1)只有一个弹性箱,没有嵌套

Fiddle

<div id="container">
    <div id="top">
      ...
    </div>
    <div id="bottom">
        blub
    </div>
</div>

html, body { 
    width: 100%; 
    height: 100%; 
    margin: 0; 
    padding: 0;
}

#container{
    background-color: yellow;
    flex-direction: column;
    display: flex;
    height: 100%;
}

#top {
    background-color: red;
    flex-grow: 1;
    display: flex;
    flex-direction: row;
        overflow: auto;
    white-space:pre;
}


#bottom {
    background-color: green;
    height: 4em;
}

<小时/> (2)嵌套的flexboxes

Fiddle

<div id="container">
    <div id="top">
        <div id="left">
        </div>
        <div id="right">
          ...
        </div>
    </div>
    <div id="bottom">
        blub
    </div>
</div>

html, body { 
    width: 100%; 
    height: 100%; 
    margin: 0; 
    padding: 0;
}

#container{
    background-color: yellow;
    flex-direction: column;
    display: flex;
    height: 100%;
}

#top {
    background-color: red;
    flex-grow: 1;
    display: flex;
    flex-direction: row;
}

#right {
    white-space: pre;
    overflow: auto;
    background-color: blue;
    width: 5em;
}

#left {
    flex-grow: 1;
    background-color: orange;
}

#bottom {
    background-color: green;
    height: 4em;
}

2 个答案:

答案 0 :(得分:4)

最新的浏览器将新的auto实现为min-height的初始值。

强制#top至少与其内容一样高。

所以你需要撤消它:

#top {
  min-height: 0;
}

然后#top可能比其内容短。 #right会被拉伸至#top一样高,#top的内容可能会溢出。

html, body { 
  width: 100%; 
  height: 100%; 
  margin: 0; 
  padding: 0;
}
#container{
  background-color: yellow;
  flex-direction: column;
  display: flex;
  height: 100%;
}
#top {
  background-color: red;
  flex-grow: 1;
  display: flex;
  flex-direction: row;
  min-height: 0;
}
#right {
  white-space: pre;
  overflow: auto;
  background-color: blue;
  width: 5em;
}
#left {
  flex-grow: 1;
  background-color: orange;
}
#bottom {
  background-color: green;
  height: 4em;
}
<div id="container">
  <div id="top">
    <div id="left">
    </div>
    <div id="right">
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      11
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
    </div>
  </div>
  <div id="bottom">
    blub
  </div>
</div>

答案 1 :(得分:0)

备选答案:只需指定flex:1而不是{top>上的flex-grow:0 或指定flex-basis:0flex:1隐式设置flex: 1 1 0,即第三个0表示flex-basis:0

这很重要,因为flex-basis的默认初始值不是 0而是auto,这反过来实际上意味着content - 大小,因此您需要诉诸min-height。和OP一样,我发现min-height有点不合时宜,有点令人困惑,flex符号也不那么明显。

更新了代码段:

http://codepen.io/yaaang/pen/gayqPE

From the spec

  

[{1}}中的第三个组件设置了flex-basis longhand并指定了flex   basis:自由空间之前的flex项的初始主要大小   根据flex因子分布。它采用相同的值   width属性(auto除外区别对待)和a   其他内容关键字。当从flex缩写中省略时,它   指定值为0%。

     

如果指定的flex-basis为auto,则使用   flex basis是弹性项目主要大小的计算值   属性。如果该值本身是auto,那么使用的flex基础是   根据其内容自动确定(即根据其大小确定)   含量)。

相关问题