【发布时间】:2016-05-05 02:40:57
【问题描述】:
我无法让 Chrome 关注 flex-direction: column 布局中 flex: 1 1 25% 的 flex-basis 部分。它在 row 布局中运行良好。
下面的 sn-p 演示了这个问题:黄色、蓝色和粉色条是 flex-basis 50px、25% 和 75%,显示在列和行的 flex 方向上。
如果您在 Firefox(或 IE11 或 Edge)中运行它,则列和行都按预期划分区域:
但是如果您在 Chrome (47) 或 Safari (9.0.3) 中运行它,左侧的列布局似乎完全忽略了 flex-basis —— 条形的高度似乎与 flex 无关-基础:
左右之间的唯一区别是flex-direction。
.container {
width: 400px;
height: 200px;
display: flex;
background: #666;
position: relative;
}
.layout {
flex: 1 1 100%; /* within .container */
margin: 10px;
display: flex;
}
.row {
flex-direction: row;
}
.column {
flex-direction: column;
}
.exact {
flex: 1 1 50px;
background: #ffc;
}
.small {
flex: 1 1 25%;
background: #cff;
}
.large {
flex: 1 1 75%;
background: #fcf;
}
<div class="container">
<div class="layout column">
<div class="exact">50px</div>
<div class="small">25%</div>
<div class="large">75%</div>
</div>
<div class="layout row">
<div class="exact">50px</div>
<div class="small">25%</div>
<div class="large">75%</div>
</div>
</div>
我尝试将height: 100% 添加到.column,这使得 Chrome 会关注 flex-basis,但会导致另一个问题——flex 变得比它的容器大:
.column {
flex-direction: column;
height: 100%;
}
我猜这是long-standing webkit bug。有没有办法解决它? (我正在尝试创建一些通用布局组件,因此硬编码特定数量的子元素或特定像素高度是不可行的。)
[编辑] 这是一个显示一般问题的附加示例(并避免上例中的边距和总大于 100% 的问题):
.container {
width: 300px;
height: 300px;
display: flex;
}
.layout {
flex: 1 1 100%; /* within its flex parent */
display: flex;
background: #ffc;
}
.row {
flex-direction: row;
}
.column {
flex-direction: column;
height: 100%; /* attempted workaround for webkit */
}
.small {
flex: 1 1 30%;
background: #cff;
}
.large {
flex: 1 1 70%;
background: #fcf;
}
div {
/* border/padding just makes divs easier to see --
you can remove all of this without changing the problem */
box-sizing: border-box;
border: 1px solid #999;
padding: 10px;
}
<div class="container">
<div class="layout row">
<div class="small">row: 30%</div>
<div class="large layout column">
<div class="small">row: 70%; col: 30%</div>
<div class="large">row: 70%; col: 70%</div>
</div>
</div>
</div>
【问题讨论】:
-
还尝试了一个完全避免 % heights/flex-basis 的版本,使用 only 纯 flex (flex-grow)。同样的问题:Firefox 根据列容器中的要求将主轴弯曲 70/30,Chrome 做自己的事情。 fiddle here
-
通过使用非 flex 绝对包装器(呃!)包装每个 flex 子级的可能解决方法:github.com/philipwalton/flexbugs/issues/…。问题可能是我的 .container 和 .layout.row 的孩子通过横轴拉伸(
align-items: stretchflex 默认)获得他们的高度,而 webkit 不会将其视为确定的高度。
标签: css google-chrome webkit flexbox