【发布时间】:2020-12-08 00:28:00
【问题描述】:
根据MDN,flex: 1 的简写应该设置为flex-basis: 0。然而,它实际上设置了flex-basis: 0%,更令人惊讶的是它有不同的行为。
在下面的示例中,我希望 div.middle 收缩和滚动,因为它被赋予了 overflow-y: auto 和 flex: 1,这也应该暗示 flex-basis: 0。但它没有——整个身体反而滚动,因为它拒绝收缩。如果您取消注释显式 flex-basis: 0,它就会正常工作。
body {
text-align: center;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: 0;
}
.outer {
min-height: 100%;
display: flex;
background: #99f;
flex-direction: column;
}
.middle {
background: #f99;
flex: 1;
/*flex-basis: 0;*/
overflow-y: auto;
}
<div class='outer'>
<div class='top'>top</div>
<div class='middle'>
A
<div style='height:800px'></div>
B
</div>
<div class='bottom'>bottom</div>
</div>
我在 Chrome 84.0 和 Firefox 68.11.0esr 中都对此进行了测试,它们都有相同的意外行为。
- 为什么
flex-basis: 0%与flex-basis: 0不同? - 为什么
flex: 1设置flex-basis: 0%而不是flex-basis: 0?
【问题讨论】:
-
伟大的收获!这种非预期投诉行为仍然存在于 firefox 90 和 chrome 91 中。