【发布时间】:2013-12-06 19:55:21
【问题描述】:
我的目标是创建一个带有 flexbox 的双列布局,其中第一列有两行,第二列有一个,如下所示:
在第三个项目上设置flex-basis: 100% 可以达到预期的效果,但前提是容器的flex-direction 是row:
将flex-direction 更改为column 会导致以下结果,除非明确设置height,这在我的项目中是不可行的:
如何在不明确设置容器的height 的情况下获取第一张图片?
Here's a Plunker illustrating the problem.
body {
display: flex;
height: 100px;
width: 100px;
}
.container {
display: flex;
flex-direction: column; /* Setting this to `row` gives the expected effect,but rotated */
flex-grow: 1;
flex-wrap: wrap;
/* height: 100%; */ /* Setting this fixes the problem, but is infeasible for my project*/
}
.item {
flex-grow: 1;
}
.one {
background-color: red;
}
.two {
background-color: blue;
}
.three {
background-color: green;
flex-basis: 100%;
}
<div class="container">
<div class="item one"> </div>
<div class="item two"> </div>
<div class="item three"> </div>
</div>
【问题讨论】:
-
为什么不嵌套读取和蓝色块?然后给你的外部网格一个水平方向和内部网格垂直。这是更多的标记,但会起作用:P
-
列方向需要一个明确的高度,以便像包装元素一样遵循 flex-basis 值。
-
@cimmanon 谢谢。出于好奇,为什么当网格是行优先时不是这种情况?我在规范中找不到任何相关内容。
-
我不知道为什么会这样(是的,我希望不需要高度)。如果你在 Flexbox 外面看一会儿,如果内容要强制滚动,所有元素都更喜欢垂直增长而不是水平增长。在某种程度上,块元素确实有明确的宽度,即使它设置为 auto(父元素的宽度)。另一方面,高度由设置为 auto 时元素的内容决定。
-
有趣的是,您的 Plunker 示例在 Firefox 32(Win 7)中“按原样”适用于我。也许这只是 Chrom(e|ium) 错误?