This example demonstrates the difference between a flex-basis
of 0
versus a flex-basis
of 0%
when flex-direction
is set to column
and the flex containers and flex items don't have a set height; while 0
is an absolute length, percentage flex-basis values resolve to content
values.
HTML
We include two same-structure flex containers. These which will be styled similarly, except for their flex-basis
values. The containers each have two children: a heading <div>
and a <section>
. The <section>
element has a content <div>
child, which will not be set as a flex item but will be given a height.
<div class="container basis-0">
<div>heading</div>
<section>
flex-basis: 0;
<div class="content"></div>
</section>
</div>
<div class="container basis-0-percent">
<div>heading</div>
<section>
flex-basis: 0%;
<div class="content"></div>
</section>
</div>
CSS
We style the containers as inline flex containers that will appear side by side to better enable comparing them. We set the flex-direction
to the column. The first container's flex items have a flex-basis
value of 0
and the second container's flex item have a flex-basis
value of 0%
. Neither the flex containers nor their flex items have a height explicitly set, but the heights of section
elements can not exceed 200px
and their children have height 300px
.
.container {
width: 40vw;
padding: 1rem;
border: 1px dashed blue;
display: inline-flex;
flex-direction: column;
}
section {
border: 1px solid red;
overflow: auto;
min-height: 200px;
}
.content {
background: wheat;
height: 300px;
}
.container.basis-0 > * {
flex-basis: 0;
}
.container.basis-0-percent > * {
flex-basis: 0%;
}
Results
In the first container, with flex-basis: 0
, the <section>
element has an initial main size of zero, and it grows to the 200px
height limit. In the second container, with flex-basis: 0%
, the <section>
element has an initial main size of 300px
because, as the flex container doesn't have a set height, the percentage flex-basis values resolve to the content
value.