liveBook · Manning

Flex-grow

As explained in Web Design Playground, Second Edition

The CSS flex-grow property specifies how much a flex item may expand relative to the other flex items when extra space is available.

flex-grow tells a flex item what share of a flex container’s leftover space it may take. Think of the empty space as a snack: items with positive grow values get slices, while the default value, 0, means “no thanks.” It matters only when the container has room left after its items have been sized. section 12.4

item {
    flex-grow: value;
}

Apply flex-grow to the flex item you want to expand. Replace value with 0 or a positive number. section 12.4

A single item with any positive value receives all available empty space. When several items can grow, equal values split it evenly. Different values split the space in proportion to their values—not necessarily the items’ final total sizes. For values 1, 2, and 1, the available space is divided 25%, 50%, and 25%. Add the grow values, then divide each item’s value by that total. section 12.4

PLAY: Use flex-grow when a layout needs an item to occupy leftover room.

  • Let a description area expand beside a thumbnail by setting its flex-grow to 1; keep the image from shrinking with flex-shrink: 0. section 12.5
  • Make a main area fill unused vertical space so a footer stays at the bottom of a window. section 12.6
  • Divide a two-column main area unevenly: flex-grow: 3 on article and flex-grow: 1 on aside assign 75% and 25% of available horizontal space. section 12.6
  • Fill the space above a photo-gallery footer with main { flex-grow: 1; }, then use align-content: flex-start to avoid odd thumbnail spacing. section 15.7

A main area and sidebar

First make main a flex container. Then give article three shares of leftover width and aside one share. If the container has extra horizontal space, article gets three quarters and aside gets one quarter. main { display: flex; flex-grow: 1; } article { flex-grow: 3; } aside { flex-grow: 1; } section 12.6

REMEMBER: flex-grow answers one question: should this item grow when extra room exists? Nearby flex properties solve different problems: flex-shrink handles too little room, flex-basis suggests an initial size, and flex combines grow, shrink, and basis in one declaration. section 12.4 section 12.7

How Web Design Playground, Second Edition covers it

How deep it goes
Explains default and positive values, equal and proportional sharing, and a simple calculation, without advanced edge cases.
Examples
Uses a thumbnail list, a Holy Grail article-and-sidebar layout, and a photo gallery footer.
What it assumes
Builds from identifying flex items in a flex container and introduces growth through direct layout examples.
What you can do after
Apply it to distribute leftover space among flex items and make page content fill available room.
How it connects
Connects flex-grow to Flexbox, flex-shrink, flex-basis, flex, align-content, and flex containers.

Also explained in

Compare how 4 books explain flex-grow →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.