Three properties decide how much space each item in a flex container gets: how much it may grow, how much it may shrink, and what size it starts from. They are almost always written as a single shorthand, and the shorthand has a compact form that is worth understanding rather than memorising.
The previous lesson in this module covered "How Do justify-content and align-items Center Anything?", and the next lesson in this module is about "How Do You Build a Responsive Navbar With Flexbox?".
What do the three properties do?
.item {
flex-grow: 0; /* how much of the free space this item takes */
flex-shrink: 1; /* how much this item gives up when space is short */
flex-basis: auto; /* the size this item starts from */
}
flex-grow distributes the leftover space. If the items are narrower than the container, the extra space is shared out in proportion to the grow factors. flex-shrink distributes the missing space, again in proportion to the shrink factors. And flex-basis is the size the item is measured from before any growing or shrinking happens.
/* two items share the space equally */
.item {
flex: 1 1 0%;
}
That is the shorthand flex: 1, and it is worth understanding, because the number you write is not the whole story. flex: 1 means grow by one, shrink by one, and start from a basis of zero. The basis of zero is what makes the split exactly equal: with flex-basis: auto, the items would start from their content sizes and the extra space would be divided on top of that, giving a slightly uneven result.
What does the flex shorthand actually set?
It takes one, two or three values, and the pattern of what goes where depends on whether the first value is a number or a length.
flex: 1; /* grow 1, shrink 1, basis 0% */
flex: 1 1 200px; /* grow 1, shrink 1, basis 200px */
flex: 0 0 150px; /* fixed: no growth, no shrinking */
flex: 1 1 auto; /* grow and shrink, from the content size */
flex: none; /* the same as 0 0 auto */
flex: auto; /* the same as 1 1 auto */
When you write a single number, it is the grow factor and the basis becomes zero. When you write a single length, it is the basis and the grow and shrink factors become zero, which means the item does not resize at all.
Which patterns come up in real layouts?
Three cover most needs. Equal columns use flex: 1. A fixed-width sidebar with a growing content area uses a basis for the sidebar and a grow of one for the content.
.layout {
display: flex;
gap: 2rem;
}
.sidebar {
flex: 0 0 16rem; /* fixed width, never grows or shrinks */
min-width: 0; /* but allowed to be overridden if needed */
}
.content {
flex: 1 1 auto; /* takes the remaining space */
min-width: 0;
}
The third pattern is proportional widths: a two-thirds and one-third split is flex: 2 and flex: 1, and it is more robust than setting percentages, because the gap does not have to be subtracted from the widths by hand.
.split {
display: flex;
gap: 1rem;
}
.split > *:first-child { flex: 2; }
.split > *:last-child { flex: 1; }
Why is min-width: 0 needed?
Because a flex item has an automatic minimum size equal to its content size, and it refuses to shrink below that. So a long unbroken word in a flexible column will not shrink — it will push the layout wider.
.row {
display: flex;
}
.row > * {
flex: 1;
min-width: 0; /* let the item shrink below its content width */
}
With min-width: 0, the item is allowed to shrink, and the content inside it then needs to deal with the overflow — usually with overflow: hidden and truncation. Without it, the common symptom is a single item in a row that refuses to shrink and pushes everything off the screen.
How do flex-basis and width interact?
flex-basis wins over width when the item is sized by flexbox. The width is only used if the flex-basis is auto, and the content size is used if the basis is auto and the width is auto. That is why setting a width on a flex item often appears to do nothing when the shorthand has already set a basis.
.item {
width: 200px; /* ignored for the main axis */
flex: 1 1 auto; /* basis is auto, so the width is the starting point */
}
Setting the width to auto and the basis to a length is the clearest way to be explicit about which one you mean.
How does shrinking get divided up?
The shrink factors are multiplied by the items' base sizes, and the total is what gets divided among the items that are shrinking. An item with a large base size therefore gives up more than one with a small base size, even with the same shrink factor.
.row {
display: flex;
}
.wide { flex: 0 1 40%; }
.narrow { flex: 0 1 10%; }
In practice this means two items with the same shrink factor do not shrink by the same number of pixels — the larger one loses more. If you need a specific ratio, set the shrink factors to match the ratio you want rather than relying on the sizes.
There is one more thing to know: an item with flex-shrink: 0 never shrinks, whatever else happens. That is useful for a fixed-width element in a row, and it is also a common cause of overflow when the rest of the row is squeezed.
Can I grow from a specific starting size?
Yes, and this is the pattern for a content area that should start at a readable width and take the rest of the space.
.content {
flex: 1 1 20rem; /* start at 20rem, grow to fill, shrink if needed */
min-width: 0;
}
The basis of 20rem acts as a sensible minimum in practice: the item is at least 20rem wide before the container gets too small, and the min-width: 0 allows it to go below that when it has to. This is a more robust alternative to hard-coding a width, and it degrades gracefully.
Quick reference
- Grow takes the leftover space, shrink absorbs the shortage, basis is the starting size.
flex: 1means grow 1, shrink 1, basis zero — an exact even split.- A single length in the shorthand sets the basis and turns growth and shrinking off.
flex: 0 0 16remis a fixed sidebar;flex: 1 1auto is a growing content area.- Proportional splits are more robust than percentages because gaps need no arithmetic.
min-width: 0is what lets a flexible item actually shrink.
Try it yourself: build a row of three equal columns, then set the middle one to flex: 2 and watch the widths change. Next, put a very long word in one of the columns and add min-width: 0 to see the difference. Finally, remove the basis from one item and watch the equal split become slightly uneven.