Sub topic 9
Sharing extra space with flex-grow
This chapter assumes you know how to create a flex container, that you can name the main axis and cross axis for a given flex-direction, and that you understand flex-basis as an item's starting size. It also assumes you are comfortable with the box model and with px, rem and percentage units.
What flex-grow does
A flex container often has more room on the main axis than its items need. The items' starting sizes (their flex-basis values, or their content sizes when flex-basis is auto) add up to less than the container's inner size. The leftover room is called free space.
By default, free space stays empty. Items keep their starting size and justify-content positions them. The flex-grow property changes this. It takes a unitless, non-negative number, and that number says how big a share of the free space the item claims.
- The default is
0, which means "do not grow". - A positive value means the item takes part in sharing free space.
- Values do not have to be whole numbers.
0.5is valid. - Negative values are invalid and the declaration is ignored.
How the free space is divided
The browser works in a fixed order for each flex line:
- It determines each item's starting size from
flex-basis. - It adds the starting sizes, plus any gaps and margins, and subtracts the total from the container's main size. The result is the free space.
- It adds up the
flex-growvalues of all items on the line. - Each item receives
free space × (its flex-grow ÷ sum of flex-grow values)in addition to its starting size.
This is a ratio, not a percentage. Only the proportions between values matter.
A worked example
Suppose a container is 600px wide, with three items that each have flex-basis: 100px, and no gaps.
- Starting sizes total 300px, so the free space is 300px.
- The items have
flex-growvalues of 1, 1 and 2, which sum to 4. - The shares are 75px, 75px and 150px.
- The final widths are 175px, 175px and 250px.
.container {
display: flex;
width: 600px;
}
.item {
flex-basis: 100px;
flex-grow: 1;
}
.item.wide {
flex-grow: 2;
}Notice that the .wide item is not twice as wide as the others. It receives twice the free space. Its final width is 250px, against 175px for each of the others. This is the most common misreading of flex-grow, and it follows directly from the fact that growth is added to the basis.
Growing from zero to get proportional widths
If you want final widths to match your ratios exactly, remove the starting size. Setting flex-basis: 0 makes all of the container's inner width free space, so the shares become the whole widths.
.layout {
display: flex;
gap: 1rem;
}
.sidebar {
flex: 1 1 0;
}
.content {
flex: 3 1 0;
}With gap: 1rem, the gap is subtracted before the split. The content column ends up three times as wide as the sidebar, and the two columns together fill the row minus the gap. This is why flex: 1 (which expands to 1 1 0%) is so popular for equal-width columns.
Growing from content size
If you leave flex-basis as auto and the items have width: auto, each item's basis is its content width. Growth is then added to different starting points, so equal flex-grow values do not give equal widths.
<div class="tags">
<span>CSS</span>
<span>Flexbox layout</span>
<span>A</span>
</div>.tags {
display: flex;
}
.tags span {
flex-grow: 1;
background: #dde;
}All three spans receive the same extra amount, so the long label stays the widest and A stays the narrowest. If you want identical widths, set flex-basis: 0 as well, for example with flex: 1.
One item takes everything
A single item with flex-grow: 1 and its siblings at 0 absorbs all the free space. This is the basis of the sticky-footer pattern, where main fills the leftover height of a column container.
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
main {
flex: 1;
}In a column container, the main axis is vertical. Free space is therefore height, and min-height: 100vh is what creates it on short pages. Without a container height larger than the content, there is no free space for main to claim.
Sums below 1
There is an edge case that surprises many developers. When the sum of all flex-grow values on a line is less than 1, the items share only that fraction of the free space, not all of it.
.partial {
display: flex;
width: 600px;
}
.partial .a {
flex-basis: 100px;
flex-grow: 0.25;
}
.partial .b {
flex-basis: 100px;
flex-grow: 0.25;
}The free space is 400px. The grow values sum to 0.5, so only 50% of it, or 200px, is distributed. Each item gains 100px and ends at 200px, leaving 200px unused. Once the sum reaches 1 or more, all of the free space is used.
Interactions with other properties
max-widthandmax-height. An item stops growing when it reaches its maximum size. The browser freezes that item and redistributes the remaining free space among the others, using the same ratios.min-widthandmin-height. Minimums can matter on the growth side too, because an item may start larger than itsflex-basissuggests. This changes how much free space exists.flex-wrap. Withwrap, free space is computed for each line separately. The last line often has fewer items, so each of its items grows more. Cards in aflex: 1 1 200pxlayout can therefore look wider on an incomplete last row.justify-content. If every item has a positiveflex-grow, no free space remains, sojustify-contenthas no visible effect. It only distributes space that growth did not consume.- Auto margins. Auto margins on the main axis take free space before
flex-growdoes, in the sense that they make growth unnecessary only when no item grows. If an item has both,flex-growwins and the auto margins resolve to zero. flex-direction. Growth always follows the main axis. It never changes the cross-axis size. To stretch items on the cross axis, usealign-items: stretch(the default) oralign-self.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Expecting `flex-grow: 2` to double the width | The item gets double the share of free space, not double the width | Use `flex-basis: 0`, for example `flex: 2 1 0` |
| Using `flex-grow: 1` with long content and unequal widths result | Different content sizes produce different starting sizes | Set `flex-basis: 0` or a fixed basis |
| Using a unit on the value, such as `flex-grow: 1px` | The declaration is invalid and ignored | Write a plain number |
| Setting `flex-grow` on a child of a non-flex element | No effect, because the parent is not a flex container | Add `display: flex` to the parent |
| A growing column does not fill the page | No free space exists because the container is only as tall as its content | Give the container a height or `min-height` |
Putting it together
The next stylesheet builds a toolbar with a title that fills the middle and two fixed buttons at the edges. Only the title grows.
.toolbar {
display: flex;
align-items: center;
gap: 0.5rem;
}
.toolbar .title {
flex-grow: 1;
}
.toolbar button {
flex-grow: 0;
}The buttons keep their content width. The title absorbs everything else, which pushes the right-hand button to the far edge without any auto margin.
Exercise
A flex container is 800px wide with gap: 0. It has three items with flex-basis: 100px and flex-grow values of 1, 2 and 2. What are the three final widths?
Show answer
The starting sizes total 300px, so the free space is 500px. The grow values sum to 5, so one unit is 100px. The items receive 100px, 200px and 200px, and the final widths are 200px, 300px and 300px. The widths add up to 800px.
Exercise
You want a sidebar and a main column at a 1:3 width ratio, with a 1rem gap between them, filling the row exactly. Which flex values do you give each column, and why is flex-basis: 0 needed?
Show answer
Use flex: 1 1 0 on the sidebar and flex: 3 1 0 on the main column. With a basis of 0, all of the width left after the gap is free space, so it is split in the ratio 1:3. With auto as the basis, the content widths would be added first and the final widths would not match the ratio.