How can I make one flex item take leftover space?
Quick answer
Set flex-grow: 1 on the flex item that should take leftover space and flex-grow: 0 on neighboring items. If a neighbor needs a particular starting size, give it a width or flex basis. The growing item receives positive free space along the container’s main axis; it cannot take leftover space if none exists, and sizing constraints can limit its growth. [S1]
The question, the details, the next step.
A practical checklist
Confirm the container has
display: flexand the intended direction.Give the item that should expand a positive grow factor, such as
flex-grow: 1.Set neighboring items to
flex-grow: 0if they should not receive extra space.Specify a width or flex basis for neighbors that need a particular starting size.
Check for positive space on the main axis and test narrow layouts and long content.
A few more useful answers
Why does the item not expand when its grow factor is 1?
Growth uses positive free space on the flex container’s main axis. If there is no positive space, or a sizing constraint limits growth, the item may not visibly expand. [S1]
If two flex items have `flex-grow: 1`, which one takes the extra space?
Both can grow: positive free space is distributed in proportion to their grow factors. To direct it to just one item, make that item the only one with a positive grow factor. [S1]
Does `flex-grow: 0` stop a neighboring item from shrinking?
No. It specifies that the item does not grow; it does not by itself control shrink behavior. Use the flex shorthand when you also need to set shrink and basis, and check the result in the intended layout. [S1]
Sources
References used for this guide.
- [S1] MDN: How flex-grow allocates positive free space developer.mozilla.org · Checked
