Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a row with a label, a text area, and a button. I want the text area to fill the gap between the label and button. What should I change?

Geez

Make the text area the only item with a positive grow factor. For example:

.row {
  display: flex;
  gap: 0.75rem;
}

.label,
.button {
  flex-grow: 0;
}

.text-area {
  flex-grow: 1;
}

When the row has positive free space, the text area gets it because it is the only item set to grow. The label and button do not grow. [S1] This does not by itself promise that they can never shrink: flex-grow controls growth, not all sizing behavior. Check the layout at narrow widths too.

Follow-up question

How does the browser decide what counts as leftover space? Does this only work horizontally?

Geez

Leftover space means positive free space along the flex container’s main axis. In a row, the main axis is horizontal, so the growing item gets extra width. With flex-direction: column, the main axis is vertical, so growth applies to height instead. [S1]

For example, imagine a row with 600 pixels of available inner width. If the label and button together use 160 pixels, and gaps and other sizing leave positive space, the text area can occupy that remaining room. Those numbers are only an illustration: actual space depends on the items, container, gaps, and sizing constraints. [S1]

Follow-up question

The label and button should keep their natural sizes, but sometimes I want a sidebar to be exactly 12rem wide. How should I handle those cases?

Geez

For neighbors that should not grow beyond their natural starting size, use flex-grow: 0; leave their basis at its usual default or specify a basis. If the sidebar should start at a specified size, set that basis explicitly. A compact pattern is:

.main {
  flex: 1 1 0;
}

.sidebar {
  flex: 0 0 12rem;
}

Here, the main item can grow into available space, while the sidebar has a 12rem basis and does not grow. The flex shorthand sets grow, shrink, and basis together, so this example also sets shrink behavior explicitly. [S1] A basis is a starting size, not a guarantee that every layout constraint will be ignored.

Follow-up question

What if the expanding item still looks too small, or the row is crowded? Can you give me a quick check and a reusable pattern?

Geez

First confirm that the container is actually a flex container and that the intended item has a positive grow factor. Then check whether positive space exists on the main axis: if the items already use all available room, there is nothing extra to distribute. Growth can also be limited by sizing constraints. [S1] If multiple items have positive grow factors, free space is shared in proportion to those factors rather than going entirely to one item. [S1]

Reusable row pattern:

.row {
  display: flex;
  gap: 0.75rem;
}

.fixed-side {
  flex: 0 0 auto;
}

.fill-space {
  flex: 1 1 0;
}

Use fixed-side for items that should not grow and fill-space for the one meant to expand. If a neighboring item needs a specific starting size, replace auto with a basis such as 12rem. Test the row at the actual widths you support; this pattern identifies which item grows, but it does not establish that every combination of content will fit.

A practical checklist

  • Confirm the container has display: flex and the intended direction.

  • Give the item that should expand a positive grow factor, such as flex-grow: 1.

  • Set neighboring items to flex-grow: 0 if 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.

  1. [S1] MDN: How flex-grow allocates positive free space
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work