🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Flex-grow

AI & DATA SCIENCE // flex-grow

The flex-grow property determines how much of a flex container's leftover, unused space an item should absorb, relative to its sibling items' own flex-grow values.

Syntax

flex-grow: number;

Deep Dive Course

flex-grow defaults to 0, meaning an item won't grow at all beyond its own natural or specified size even if the container has leftover space — a nonzero value tells the item to expand and absorb a share of that leftover space, and when several items all have a nonzero flex-grow, the leftover space is distributed proportionally according to their relative flex-grow values, so an item with flex-grow: 2 grows twice as much as a sibling with flex-grow: 1, and an item with flex-grow: 1 amid an otherwise flex-grow: 0 layout absorbs the entirety of the leftover space by itself.

1Understanding Flex-grow

flex-grow defaults to 0, meaning an item won't grow at all beyond its own natural or specified size even if the container has leftover space — a nonzero value tells the item to expand and absorb a share of that leftover space, and when several items all have a nonzero flex-grow, the leftover space is distributed proportionally according to their relative flex-grow values, so an item with flex-grow: 2 grows twice as much as a sibling with flex-grow: 1, and an item with flex-grow: 1 amid an otherwise flex-grow: 0 layout absorbs the entirety of the leftover space by itself.

💡

Setting flex-grow: 1 on a single flex item, with every other sibling left at the default flex-grow: 0, causes that one item alone to absorb the entirety of the container's leftover space, a common, simple technique for pushing one specific item, like a search bar, to fill the remaining room in a row.

editor.html
.search-bar {
  flex-grow: 1;
}

/* sibling buttons left at default flex-grow: 0 */
localhost:3000

2Practical Example

Here is a real-world application of Flex-grow showing how it is used in production CSS code.

editor.html
.item-a { flex-grow: 1; }
.item-b { flex-grow: 2; }
localhost:3000

3Best Practices

Follow these guidelines when working with Flex-grow:

1. Set flex-grow: 1 on a single item that should expand to fill all remaining leftover space, leaving its siblings at the default flex-grow: 0

2. Use proportional flex-grow values, like 1 and 2, when several items should share leftover space unevenly, in specific relative proportions to each other

3. Remember flex-grow only distributes leftover space — it has no effect at all if the container's items already fill or exceed the container's total size, with no leftover space left to distribute

⚠️

Tip: Setting flex-grow: 1 on a single flex item, with every other sibling left at the default flex-grow: 0, causes that one item alone to absorb the entirety of the container's leftover space, a common, simple technique for pushing one specific item, like a search bar, to fill the remaining room in a row.

editor.html
.search-bar {
  flex-grow: 1;
}

/* sibling buttons left at default flex-grow: 0 */
localhost:3000

Examples

Example 01Basic Usage
.search-bar {
  flex-grow: 1;
}

/* sibling buttons left at default flex-grow: 0 */
Example 02Advanced Example
.item-a { flex-grow: 1; }
.item-b { flex-grow: 2; }

Best Practices

  • Set flex-grow: 1 on a single item that should expand to fill all remaining leftover space, leaving its siblings at the default flex-grow: 0
  • Use proportional flex-grow values, like 1 and 2, when several items should share leftover space unevenly, in specific relative proportions to each other
  • Remember flex-grow only distributes leftover space — it has no effect at all if the container's items already fill or exceed the container's total size, with no leftover space left to distribute

Interview Question

If a flex container has 30px of leftover space, and two items have flex-grow values of 1 and 2 respectively, why does the second item absorb exactly 20px rather than some other split?

Hint: Think about flex-grow values as representing each item's proportional share, or ratio, of the total leftover space, rather than an absolute amount of space each one is guaranteed.

flex-grow values don't represent an absolute, fixed amount of space each item is entitled to, they represent each item's relative proportion, or ratio, of however much leftover space actually exists, calculated by adding up every growing item's flex-grow value to get a total, then giving each item a fraction of the leftover space equal to its own value divided by that total. With flex-grow values of 1 and 2, the total across both items is 3, so the first item gets 1/3 of the leftover space and the second item gets 2/3, which for exactly 30px of leftover space works out to 30 times 1/3, or 10px, for the first item and 30 times 2/3, or 20px, for the second, precisely matching the 1:2 ratio the two flex-grow values represent. This proportional, ratio-based distribution is exactly why the specific absolute values of 1 and 2 matter only relative to each other, using 2 and 4 instead would produce the exact identical 1:2 split and the exact same final pixel amounts, since it's the ratio between sibling values, not their individual absolute magnitude, that actually determines each item's actual share.

Exercises

MediumPractice using Flex-grow in a real scenario.
View Solution
.search-bar {
  flex-grow: 1;
}

/* sibling buttons left at default flex-grow: 0 */

Frequently Asked Questions

If a flex container has 30px of leftover space, and two items have flex-grow values of 1 and 2 respectively, why does the second item absorb exactly 20px rather than some other split?

flex-grow values don't represent an absolute, fixed amount of space each item is entitled to, they represent each item's relative proportion, or ratio, of however much leftover space actually exists, calculated by adding up every growing item's flex-grow value to get a total, then giving each item a fraction of the leftover space equal to its own value divided by that total. With flex-grow values of 1 and 2, the total across both items is 3, so the first item gets 1/3 of the leftover space and the second item gets 2/3, which for exactly 30px of leftover space works out to 30 times 1/3, or 10px, for the first item and 30 times 2/3, or 20px, for the second, precisely matching the 1:2 ratio the two flex-grow values represent. This proportional, ratio-based distribution is exactly why the specific absolute values of 1 and 2 matter only relative to each other, using 2 and 4 instead would produce the exact identical 1:2 split and the exact same final pixel amounts, since it's the ratio between sibling values, not their individual absolute magnitude, that actually determines each item's actual share.

Related Functions

Flex-shrinkFlex-basisDisplay-flex