Skip to content

Flexbox by drawing it

BasicsConcept

You give flex items a width, and they come out a different size. Here is the algorithm that decides, run on a single row: start at the basis, then grow or shrink to fill the line.

01 Three boxes in one row

You write display: flex and three boxes jump into a row. Then you give them widths, and the browser ignores them: they come out wider, or narrower, than you asked. Flexbox is not ignoring you. It follows a short algorithm, and once you see it run on one row you can predict every width.

row.html
<div class="row">
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
</div>
row.css
.row { display: flex; width: 600px; }
.a { flex: 1 1 100px; } /* grow 1, shrink 1, basis 100px */
.b { flex: 1 1 200px; }
.c { flex: 2 2 100px; }

The three numbers in flex are grow, shrink and basis. The rest of this page works out what they produce.

02 Two axes: main and cross

A flex container lays its children out along one line, the main axis. flex-direction picks it: row (the default) runs left to right in English, column runs top to bottom. The cross axis is the other one, at right angles.

Which property works on which axis
PropertyAxisWhat it does
flex-directionMainChooses the main axis: row or column
flex-grow, flex-shrink, flex-basisMainSize each item along the main axis
justify-contentMainPlaces items when space is left over after growing
align-itemsCrossPlaces or stretches each item across the line
flex-wrapBothLets items start a new line instead of shrinking

Sizing happens on the main axis only. Across, the default align-items: stretch makes every item as tall as the tallest one, which is why flex columns come out the same height.

The same three items on the cross axis. Content makes A one unit tall, B five and C three.
align-items: stretch (the default)
AAABBBCCC
AAABBBCCC
AAABBBCCC
AAABBBCCC
AAABBBCCC
align-items: flex-start
AAABBBCCC
···BBBCCC
···BBBCCC
···BBB···
···BBB···
align-items: center
···BBB···
···BBBCCC
AAABBBCCC
···BBBCCC
···BBB···
  • item
  • empty

The line is as tall as its tallest item, B. stretch grows the others to match; the other values keep each item’s own height and only move it.

03 Every item starts at its flex-basis

Flexbox first gives each item a starting size along the main axis: its flex-basis. With flex-basis: auto (the default) that is the item’s width in a row, or, if it has none, the size of its content.

Then it subtracts. Free space is the container’s size minus all the bases. If it is positive, flex-grow decides who gets it. If it is negative, flex-shrink decides who gives some back. Nothing else is shared out.

Text
free space = container − (basis A + basis B + basis C)
= 600px − (100 + 200 + 100)
= 200px

04 flex-grow shares out the space left

Each item’s flex-grow is its number of shares in the free space. Here is the row from the first section, 600px wide, drawn in 50px cells.

flex-grow shares out 200px of free space
Step 1 of 7
the row, one cell per 50px (600px wide)
·
·
·
·
·
·
·
·
·
·
·
·
  • just added
  • item
  • free space
  • not laid out yet

The container is 600px wide and holds A, B and C. Nothing is placed yet.

600px free space

Grow adds to the basis; it does not set the final size. A and B both grew by 50px, and B is still bigger because it started bigger.

05 flex-shrink takes back what does not fit

When the bases add up to more than the container, the free space is negative and items shrink. The rule is slightly different: each item gives back in proportion to flex-shrink times its basis, so big items give more.

flex-shrink takes back 200px of overflow
Step 1 of 7
the row, 50px per cell (container ends at the mark)
·
·
·
·
·
·
·
edge·
·
·
·
·
  • just placed
  • just shrank
  • item, inside
  • item, overflowing
  • free space
  • outside the container

Now the container is only 400px wide, and A, B and C each have flex-basis: 200px. Cells after the mark are outside the container.

400px free space

Weighting by basis keeps small items from vanishing. If shrinking were shared equally, a 40px icon next to two 400px panels could be squeezed to nothing.

06 What flex: 1 really means

Most code sets all three values with the flex shorthand, and the common values hide different bases. That is where most surprises come from.

Common flex values, spelled out
Shorthandgrowshrinkbasis
(nothing set)01auto
flex: 1110%
flex: auto11auto
flex: none00auto
flex: 1Equal widthsEvery basis is 0, so the whole row is free space, shared equally. Three items get a third each, whatever their content.
flex: autoContent + shareEach item starts at its content size and then gets an equal share of what is left, so an item with more text stays wider.

To check a result, open DevTools: Chrome and Firefox show a flex badge next to the container in the Elements panel that draws the items, the free space and the gaps on the page. Or read the sizes from the console:

JavaScript
[...document.querySelectorAll('.row > *')]
.map(el => el.getBoundingClientRect().width);
// [150, 250, 200] for the row in the first section

07 When flexbox surprises you

.title { flex: 1; } /* holds a long URL */An item that will not shrinkFlex items have min-width: auto, so they do not shrink below their content’s smallest size. A long word or URL pushes the row wider. Add min-width: 0 (or overflow: hidden) to the item.
.a { width: 300px; flex: 1; }width that seems ignoredflex: 1 sets flex-basis: 0%, and a basis other than auto wins over width. Use flex-basis to size flex items, or flex: 0 0 300px to fix one.
.row img { } /* in a flex row */Images stretched tallThe default align-items: stretch stretches an item to the line’s height. An image whose height is not set can be stretched out of shape. Set align-items: flex-start or align-self on the image.
.cards { display: flex; } /* 12 cards */Items squeezed instead of wrappingThe default is flex-wrap: nowrap, so items shrink to fit one line. Add flex-wrap: wrap and a basis, such as flex: 1 1 200px, to let them flow onto new lines.
justify-content: center; /* items have flex: 1 */justify-content does nothingjustify-content only places leftover space. If any item can grow, it takes all the free space first and there is nothing left to place.

08 Check yourself

Question

A 500px row holds two items with flex: 1 1 100px and flex: 3 1 100px. How wide is the second one?

Question

A 300px row holds items with bases 100px and 300px, both with flex-shrink: 1. How much does the first one shrink?

Question

In flex-direction: column, which property makes items fill the container’s width?

09 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?