Sub topic 11
The flex shorthand and its common values
This chapter assumes you can already create a flex container, know the main axis from the cross axis, and understand flex-grow, flex-shrink and flex-basis individually. It covers how those three properties combine into the flex shorthand, which values you will use most, and the traps in how the browser reads short forms.
What the shorthand sets
The flex property sets flex-grow, flex-shrink and flex-basis in one declaration, always in that order.
.item {
flex: 2 1 150px;
/* flex-grow: 2; flex-shrink: 1; flex-basis: 150px; */
}The initial values, used when you write no flex declaration at all, are 0 1 auto. An item does not grow, it can shrink, and its starting size comes from its width (or height in a column) or, if that is auto, from its content.
The shorthand also resets any of the three values you leave out. That difference from the longhand properties matters, and a later section returns to it.
The keyword values
Four keywords cover most real use.
| Declaration | Expands to | Behavior |
|---|---|---|
| `flex: initial` | `0 1 auto` | Content-sized or `width`-sized. Shrinks if needed, never grows. |
| `flex: auto` | `1 1 auto` | Starts at its normal size, then grows and shrinks. |
| `flex: none` | `0 0 auto` | Fully inflexible. Keeps its normal size. |
| `flex: 1` | `1 1 0%` | Ignores its normal size and shares the line equally. |
flex: none suits items such as icons or avatars, which must never be squeezed. flex: 1 is the usual way to say "take the leftover space".
How a short form is read
Browsers accept one, two or three values. They decide what each value means from its type. A unitless number is a flex factor, and a length or percentage is a basis.
- One unitless number, such as
flex: 3, means3 1 0%. The basis becomes0%, notauto. - One length or percentage, such as
flex: 200px, means1 1 200px. - One keyword, such as
autoornone, uses the table above. - Two unitless numbers, such as
flex: 2 0, set grow and shrink, with the basis0%. - A number and a length, such as
flex: 2 150px, set grow and basis, with shrink1. - Three values set everything explicitly.
Watch the unitless zero. In flex: 1 1 0, the final 0 is accepted as a basis because two flex factors precede it. In flex: 0, the zero is a grow factor, so the result is 0 1 0%, which collapses an item to its minimum size. If you mean a zero basis, write 0% or 0px.
Why `flex: 1` and `flex: auto` differ
The basis is the whole difference. flex: 1 uses a basis of 0%, so all of the container's width counts as free space and is divided by grow factor. flex: auto keeps each item's own size as its starting point and divides only what remains.
Take a container 800px wide with three items whose width values are 100px, 200px and 300px.
<div class="row">
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
</div>.row {
display: flex;
width: 800px;
}
.a { width: 100px; }
.b { width: 200px; }
.c { width: 300px; }
/* Variant 1: every item gets the same rule */
.row > div { flex: 1; }
/* Variant 2: swap the rule above for this one */
/* .row > div { flex: auto; } */With flex: 1, each width is ignored. The free space is 800px, split three ways, so every item is about 266.67px wide.
With flex: auto, the bases are 100, 200 and 300, totaling 600px. The 200px left over is split equally, about 66.67px each. The items end at roughly 166.67px, 266.67px and 366.67px, so the original size differences remain.
Use flex: 1 when you want equal columns regardless of content. Use flex: auto when you want content-based sizes that fill the row.
Weighted sharing
Grow factors are ratios. With a zero basis, they give predictable proportions.
.layout {
display: flex;
gap: 1rem;
}
.sidebar { flex: 1; }
.content { flex: 3; }The free space is divided 1:3, so the content area is three times as wide as the sidebar. With gap, the free space is first reduced by the gap total, so the ratio applies to what remains. Padding and borders are not part of a 0% basis under box-sizing: content-box, so they are added on top and can skew the visible ratio. Setting box-sizing: border-box on the items avoids this.
A fixed item beside a flexible one
A common pattern gives one item a fixed size and lets another fill the rest.
.field {
display: flex;
gap: 0.5rem;
}
.field input {
flex: 1;
}
.field button {
flex: none;
}The button keeps its natural width and never shrinks, and the input takes all remaining space. Writing flex: 0 0 auto would be equivalent, but none is shorter and says the same thing.
Gotchas
The shorthand resets omitted values
If a rule such as .item { flex-shrink: 0; } is followed later in the cascade by .item { flex: 1; }, the shorthand wins and flex-shrink returns to 1. Place longhand overrides after the shorthand, or include all three values.
`flex: 1` and a column container
In a flex-direction: column container with no definite height, a 0% basis cannot be resolved against an unknown height. Browsers then treat the basis as content, and items may not share space as you expect. Give the container a height or min-height, as in the sticky-footer pattern.
`flex: 1` does not stop overflow
A 0% basis does not lower an item's automatic minimum size. A flexible item holding a long unbroken string or a wide image can still refuse to shrink. The fix is min-width: 0, covered in the chapter on minimum sizing.
Percent bases and gaps
An item with flex: 0 0 33.33% and a gap set on its container overflows, because the three bases fill the whole width before the gaps are added. Let items shrink, or use calc(33.33% - 1rem) style bases.
Choosing a value
- Need an item that never changes size? Use
flex: none. - Need equal columns? Use
flex: 1on all of them. - Need content-based sizes that fill the row? Use
flex: auto. - Need a proportional split? Use different unitless numbers, such as
flex: 1andflex: 3. - Need cards that wrap with a minimum width? Use
flex: 1 1 200pxtogether withflex-wrap: wrap.
Exercise
A 600px-wide flex container holds two items with no gaps, padding or borders. Item one has width: 100px; flex: auto;. Item two has width: 300px; flex: auto;. What width does each end up with? Then, what widths result if both use flex: 1?
Show answer
With flex: auto, the bases are 100px and 300px, totaling 400px. The 200px of free space is split equally, so item one is 200px and item two is 400px. With flex: 1, the bases are 0%, so all 600px is shared equally and both items are 300px wide.
Exercise
What does flex: 2 expand to, and why does flex: 2 often give different results from flex: 2 1 auto?
Show answer
flex: 2 expands to 2 1 0%. The basis is zero, so item sizes are ignored and the whole line is shared by grow factor. flex: 2 1 auto starts from each item's own size and shares only the leftover space, so item sizes still influence the result.