Guide

CSS flexbox: axes, grow, shrink and gap explained

Reviewed 23 November 2026 · 7 min read · Written and maintained by Toolnexo

Flexbox is the part of CSS made for laying items out along one line, either a row or a column. Most of the confusion about it comes from a few ideas that are rarely explained together: which axis a property works on, what happens to spare space, and what the flex shorthand really sets. This guide takes them one at a time with measured examples. The properties and values come from the CSS flexbox generator on this site, which runs in your browser, so nothing you build is uploaded.

One direction at a time

Setting display: flex on an element makes it a flex container and its children flex items. The flex-direction property decides which way the items run: row (the default), row-reverse, column or column-reverse. That direction is the main axis. The other direction, across it, is the cross axis. In a row the main axis runs left to right and the cross axis runs top to bottom. In a column they swap.

Almost every flexbox property is tied to one of the two axes, so the first question to ask when something does not move the way you expect is which axis you are on.

Spacing along the main axis: justify-content

  • flex-start, the default, packs the items at the start.
  • flex-end packs them at the end, and center puts them in the middle.
  • space-between puts the first item at the start, the last at the end and shares the space equally between items.
  • space-around gives every item the same space on both sides, so the gap at each edge is half the gap between two items.
  • space-evenly makes the gaps at the edges and between items all equal.

Aligning across the axis: align-items

align-items places the items on the cross axis. The default, stretch, makes every item as tall as the container in a row, unless the item has its own height. The other values are flex-start, flex-end, center and baseline, which lines up the first line of text in each item. To centre one item both ways, use display: flex, justify-content: center and align-items: center on the parent, and make sure the parent has a height to centre in.

Wrapping, and when align-content matters

By default flex-wrap is nowrap, so all the items stay on one line and shrink if they have to. Setting wrap lets items drop onto new lines when they run out of room, and wrap-reverse stacks the new lines the other way.

align-content spaces the lines themselves, so it only has an effect when there is more than one line. On a single line it does nothing, which is why the generator leaves that line out of the CSS while wrapping is off.

How grow, shrink and basis share the space

Each item has a starting size, the flex-basis, which is auto (use the item width) unless you set a length. The browser adds up the starting sizes. If there is spare room it hands it out in proportion to flex-grow. If there is not enough room it takes the shortfall away in proportion to flex-shrink, weighted by each item size.

Measured in a browser with a 600-pixel-wide container and three items that start at 100 pixels: with a grow of 1 each, the 300 spare pixels split three ways and every item becomes 200 pixels wide. With grow values of 1, 2 and 1 the spare space splits 75, 150 and 75, giving widths of 175, 250 and 175. With a shrink of 1 and three items that each start at 150 pixels in a 300-pixel container, the 150-pixel shortfall is removed equally and every item ends up 100 pixels wide.

What the flex shorthand sets

  • flex: 0 1 auto is the starting value: do not grow, shrink if needed, start from the item size.
  • flex: 1 is short for 1 1 0%. Every item starts from zero, so they share the whole width equally. Three of them in 600 pixels measure 200 each.
  • flex: auto is 1 1 auto: items start from their own size and share only the spare room.
  • flex: none is 0 0 auto: the item keeps its size and neither grows nor shrinks.

Gap instead of margins

The gap property puts space between items without adding any before the first or after the last. Take the 600-pixel container with three items starting at 100 pixels, each allowed to grow, and add a 12-pixel gap. The two gaps use 24 pixels, so 276 pixels are spare, each item gains 92 and ends at 192 pixels. A gap also works across wrapped lines, which margins make awkward.

Common mistakes

  • Using justify-content to move items up and down in a row. It works on the main axis, so use align-items for that.
  • Centring vertically in a parent with no height. There is no free space to centre in until you give the parent a height or a minimum height.
  • Expecting align-content to change a single line of items.
  • Letting long words overflow. Flex items will not shrink below their content by default, so give the item min-width: 0 if a long word or URL pushes the layout wider.

Using the generator

  • Choose the direction, wrap, justify-content, align-items and gap and watch the preview rearrange as you go.
  • Set grow, shrink and basis to see how items share the space.
  • Copy the CSS or the HTML. Properties equal to the browser default are left out unless you tick Include default values.
Tools in this guide

Put it into practice

How this guide is maintained

This page is reviewed when the related tools change. It describes what the published tools actually do: the tools run in your browser tab, so your files stay on your device. If you find something inaccurate, email [email protected] and it will be corrected.

Keep reading

Other guides

All guides →