As stated on MDN, the shorthand flex: 1
is supposed to establish flex-basis: 0
. Surprisingly, it sets flex-basis: 0%
instead, leading to unexpected behavior.
In the given example, I anticipated that div.middle
would shrink and allow scrolling due to having overflow-y: auto
and flex: 1
, which should imply flex-basis: 0
. However, this is not the case - the entire body scrolls because it refuses to shrink. Uncommenting the explicit flex-basis: 0
rectifies the issue.
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>
I have tested this scenario in both Chrome 84.0 and Firefox 68.11.0esr, and they exhibit the same unexpected behavior.
- What causes the difference between
flex-basis: 0%
andflex-basis: 0
? - Why does
flex: 1
establishflex-basis: 0%
rather thanflex-basis: 0
?