【发布时间】:2016-02-13 06:08:59
【问题描述】:
我偶然发现了一些发生在我的 flex 子项上的事情,如 this JSFiddle 所示。
这是代码:
<div style="position:absolute; top:10px; left:10px; background-color:yellow; height:50px; width:200px; display:flex; flex-direction:column;">
<div style="overflow:hidden;">inner div text<br>inner div text<br>inner div text<br>inner div text<br>inner div text</div>
</div>
<div style="position:absolute; top:10px; left:250px; background-color:green; height:50px; width:200px;">
<div style="overflow:hidden;">inner div text<br>inner div text<br>inner div text<br>inner div text<br>inner div text</div>
</div>
<div style="position:absolute; top:200px; left:100px; border:1px solid black; height:300px; width:300px; overflow:hidden;">
<div style="overflow:hidden;">
Why does the inner div on the yellow left have its height limited to that of its container, but not the inner div on the green right? As far as I can tell there are 4 things combined that trigger this:<br>1) the container has position:absolute<br>
2) the container has a fixed height/width<br>
3) the container is flexbox<br>
4) the flex item has overflow:hidden<br><br>But I can't find anything exactly in the flexbox spec to cause this behaviour. Why is it happening?
</div>
</div>
基本上,当设置overflow:hidden 时,具有固定高度的 flexbox 容器内的 flex 项目的高度将被限制为 flexbox 容器的高度。另一方面,固定高度的非弹性容器内的等价 div 只会扩展到其内容的大小。
在 flexbox 规范中的什么地方指定了这种行为?这只是浏览器的怪癖吗?这似乎不太可能发生在 Firefox、Chrome 和 IE 中。假设它是定义明确的 flexbox 行为,我想确切地知道它是如何以及何时触发的。另请注意,当您从 child(而不是容器)中删除 overflow:hidden 时,它的高度将不再受到限制。
【问题讨论】: