flex
Example
.item {
flex-grow: 1;
}.item {
flex: 1;
min-width: 0;
}flex: 1 also sets flex-basis: 0, so items share space equally, and min-width: 0 lets them shrink below their content.
Common mistakes
- Setting
flex: 1on the container instead of its children. - Items with
flex: 1still overflow because of long words and nomin-width: 0.
Key points
- Equal-width items use
flex: 1together withmin-width: 0when they hold long text or code. - The shorthand
flexis used instead of settingflex-grow,flex-shrinkandflex-basisone by one. flexis set on the flex items, not on the container withdisplay: flex.
Interview questions
flex: 1 vs flex: auto: what is the difference?
flex: 1 means 1 1 0, so items get equal widths. flex: auto means 1 1 auto, so items start from their content size and share the remaining space.
What is the default value of flex?
The initial value is 0 1 auto: items do not grow, can shrink, and start from their own size.
What does flex: none do?
It sets 0 0 auto, so the item keeps its size and neither grows nor shrinks.