Full answer
flex is a shorthand for three properties: flex-grow, flex-shrink and flex-basis. The basis is the starting size before growing or shrinking.
flex: 1means1 1 0%. The starting size is zero, so all the free space is shared by the growth factors. Items withflex: 1become equal in width, whatever their content.flex: automeans1 1 auto. The starting size is the content or width size. Free space is added on top of that, so items with more content stay wider.flex: nonemeans0 0 auto. The item neither grows nor shrinks. It keeps its width or content size. This is good for a fixed icon or a sidebar.flex: 0 1 200pxmeans do not grow, can shrink, start at 200px. It is a width that gives way when space is short.flex: 2makes an item take twice the share of an item withflex: 1.flex-basisoverrideswidthin a row, andheightin a column.flex-basis: 0andwidth: 0are not the same in all cases because of the minimum size rules.
A single number is read as flex-grow, and the basis becomes 0, which is why flex: 1 gives equal columns. A length alone is read as the basis.
Items also have min-width: auto, so a long word can prevent shrinking. Set min-width: 0 on the item when text must wrap or truncate.
In the first row, both items are 150px wide. In the second row, the long item is wider than the short one, because each starts at its content size and then both share the extra space. In the third row the first item is exactly 100px and the second takes the remaining 200px.
Check the result when the text is longer, because wrapping changes the sizes.
<style>
.row { display: flex; width: 300px; margin-bottom: 8px; background: #eee; }
.row > div { background: #cde; border: 1px solid #99a; box-sizing: border-box; }
.one > div { flex: 1; }
.auto > div { flex: auto; }
.none > div:first-child { flex: none; width: 100px; }
.none > div:last-child { flex: 1; }
</style>
<div class="row one" id="r1"><div id="a1">Short</div><div id="a2">A much longer piece of text</div></div>
<div class="row auto" id="r2"><div id="b1">Short</div><div id="b2">A much longer piece of text</div></div>
<div class="row none" id="r3"><div id="c1">Fixed</div><div id="c2">Takes the rest</div></div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- flex is grow, shrink and basis.
- flex: 1 is 1 1 0% and gives equal widths.
- flex: auto starts from the content size.
- flex: none is rigid.
- min-width: 0 lets text shrink.
Common mistakes
- Using flex: 1 and expecting content-based widths.
- Setting width on a flex item and being surprised when it shrinks.
- Forgetting min-width: 0 when text must wrap in a flex item.
Follow-ups
- What does a single number in flex mean?
- Why can an item with a width still shrink?
Tips
- Give the expanded form of 1, auto and none.
- Explain the difference between 1 and auto using the starting size.