Sub topic 11
The flex shorthand and its common values
This chapter assumes you know how to create a flex container, that flex-basis sets an item's starting size, that flex-grow shares leftover space, and that flex-shrink handles overflow. Here you will combine those three into the flex shorthand and learn the values you will use most.
What the shorthand sets
The flex property sets three longhand properties in this order: flex-grow, flex-shrink, flex-basis.
.item {
flex: 2 1 150px; /* grow 2, shrink 1, basis 150px */
}The shorthand is preferred over writing the longhands separately. When you use it, omitted parts get sensible defaults instead of inheriting surprising values from elsewhere in the cascade.
How omitted values are filled in
One-value and two-value forms are interpreted by type. A unitless number is a grow factor. A length or percentage is a basis.
| You write | Grow | Shrink | Basis | Meaning |
|---|---|---|---|---|
| `flex: initial` | 0 | 1 | auto | Browser default. Item can shrink but not grow. |
| `flex: auto` | 1 | 1 | auto | Start from the item's own size, then grow and shrink. |
| `flex: none` | 0 | 0 | auto | Fully rigid. Never grows or shrinks. |
| `flex: 1` | 1 | 1 | 0 | Ignore content size, share all space equally. |
| `flex: 2` | 2 | 1 | 0 | Takes twice the share of a `flex: 1` sibling. |
| `flex: 200px` | 1 | 1 | 200px | Start at 200px, then grow and shrink. |
| `flex: 1 200px` | 1 | 1 | 200px | Same as above, written explicitly. |
Notice that flex: 1 sets the basis to 0, not auto. This is the most common source of confusion, covered next.
flex: 1 versus flex: auto
Both let items grow. The difference is the starting point.
With flex: auto, each item starts at its content size, and only the leftover space is shared. Items with more content end up wider.
With flex: 1, every item starts at zero, so all the space is shared in proportion to the grow factors. Equal factors give equal widths, whatever the content.
.equal {
display: flex;
}
.equal > * {
flex: 1; /* equal-width columns */
}
.content-sized > * {
flex: auto; /* wider content gets more room */
}Choosing a value
- Use
flex: 1for equal-width columns or for a region that should fill the remaining space, such asmainin a sticky footer. - Use
flex: autowhen items should respect their content but still fill the row. - Use
flex: nonefor fixed items such as icons, avatars, or sidebars that must keep their width. - Use
flex: 1 1 200pxfor wrapping card grids, where 200px is the preferred minimum before wrapping.
.layout {
display: flex;
gap: 1rem;
}
.sidebar {
flex: none;
width: 240px;
}
.content {
flex: 1;
}Exercise
A row has three items. You set flex: 1 on the first and flex: 2 on the other two. The row is 800px wide and there is no gap. How wide is each item?
Show answer
All three start at a basis of 0, so the 800px is split in a 1:2:2 ratio. The first item is 160px wide and the other two are 320px each.
Exercise
A logo in a flex row keeps getting squeezed when the window narrows. Which flex value stops this, and why?
Show answer
flex: none. It sets grow and shrink to 0 with a basis of auto, so the logo keeps its own width and is never shrunk.