Flexbox: Items — CSS

The leftover space is the width of the parent minus the starting widths of all the items. flex-grow divides that leftover space by each item's share (0 means…

Picture a dashboard page (the admin page of an app). The sidebar (the menu column on the left) only needs to be as wide as its menu. The content area next to it has to fill the rest of the screen, however wide the screen is. Placing items is not enough for that. The items have to be able to change size.

The properties in this lesson go on an item, not on the parent, and set the size of that item: whether it may grow into empty space, and whether it may shrink. That empty space is called the leftover space: the parent's width minus the starting widths of all the items.

flex-grow shares out the leftover space

These CSS rules style one parent, .layout, and the three columns inside it:

.layout { display: flex; }
.sidebar { width: 88px; flex-grow: 0; }
.content { width: 64px; flex-grow: 1; }
.aside { width: 72px; flex-grow: 0; }

The result: the Sidebar and Aside columns stay narrow, and the Content column grows until the row is full.

Now suppose all three columns get flex-grow: 1. Do they end up the same width, or still different? Guess before you read on.

The answer: still different. Only the leftover space is shared: every column gets the same extra width, but the columns start at different widths, so they also end at different widths. The flex-grow number is a share of that leftover space. For example, two items have flex-grow: 1 and one item has flex-grow: 2. The leftover space is cut into four parts, and the item with 2 gets two of them.

flex: 1 to fill the leftover space

flex is a shorthand, one property for three properties at once:

For example, the sidebar and the content of a dashboard page:

.sidebar { flex: 0 0 250px; }
.content { flex: 1; }

flex: 0 0 250px sets those three properties in that order: no growing, no shrinking, a starting size of 250px. flex: 1 is the same as flex-grow: 1; flex-shrink: 1; flex-basis: 0. A basis of 0 means the item's starting size counts as 0, so the item's whole width comes from its share of the leftover space. That is why several items with flex: 1 get equal widths, as long as their content can shrink. One very long word still makes its item wider, unless that item gets min-width: 0. In this example, the sidebar stays 250px, and the content fills all the space next to it.

flex-grow: 1 alone is not the same as flex: 1: the item's starting size is not 0, but still its starting width (its width, or the width of its content when no width is set). In the exercise, when you are asked for flex: 1, write flex: 1.

Change the order with order

The order property changes the order in which items are shown:

.aside { order: -1; }

Items with a smaller order value are shown first. The default is 0, so order: -1 moves .aside to the front without changing the HTML.

⚠️ Widths not adding up?

TL;DR: The leftover space is the width of the parent minus the starting widths of all the items. flex-grow divides that leftover space by each item's share (0 means the item takes none). flex-shrink says whether an item may shrink, and flex-basis sets its starting size. flex: 1 (grow 1, shrink 1, basis 0) makes an item fill the leftover space, and several items with flex: 1 split it evenly. flex: 0 0 250px locks an item's size at 250px. order changes the order items are shown in without changing the HTML.

⚡ Asking for the leftover space

Write:

.layout { display: flex; }
.sidebar { width: 88px; flex-grow: 0; }
.content { width: 64px; flex-grow: 1; }
.aside { width: 72px; flex-grow: 0; }

Meaning: .layout is the parent of the three columns. The leftover space is the parent's width minus the starting widths (width) of all the columns. flex-grow: 0 (the default) means a column takes no leftover space. Only .content has flex-grow: 1, so all the leftover space goes to .content.

⚡ A share, not a width

Guess first: all three columns get flex-grow: 1. Do they end up the same width?

No, still different. Only the leftover space is shared evenly, and each column starts at a different width.

Meaning: The flex-grow number is a share of the leftover space. An item with flex-grow: 2 gets twice the share of an item with flex-grow: 1, not twice the width.

⚡ The dashboard recipe

Write:

.sidebar { flex: 0 0 250px; }
.content { flex: 1; }

Meaning: flex sets three things at once, in this order: grow (may the item grow), shrink (may it shrink) and basis (its starting size). flex: 0 0 250px means grow 0, shrink 0, basis 250px: the sidebar (the menu column at the side) is locked at 250px. flex: 1 is grow 1, shrink 1, basis 0: the content starts from a size of 0, then takes all the leftover space next to the sidebar.

In the exercise, when you are asked for flex: 1, write flex: 1, not only flex-grow: 1.

What you will learn