All articles

CSS flexbox cheat sheet

· 3 min read

Flexbox arranges items along one axis, either a row or a column, and distributes space between them. Nearly every toolbar, navigation bar, card footer and centered element on the web uses it. Most confusion comes from two things: which axis each property acts on, and how items grow and shrink.

The two axes

  • Main axis: the direction items flow, set by flex-direction. Horizontal for row, vertical for column.
  • Cross axis: perpendicular to it.

justify-content always works on the main axis, and align-items on the cross axis. When you switch to flex-direction: column, they swap visual directions.

Container properties

.container {
  display: flex;               /* or inline-flex */
  flex-direction: row;         /* row | row-reverse | column | column-reverse */
  flex-wrap: nowrap;           /* nowrap | wrap | wrap-reverse */
  justify-content: flex-start; /* main axis */
  align-items: stretch;        /* cross axis, per line */
  align-content: normal;       /* cross axis, between lines (only with wrap) */
  gap: 16px;                   /* row-gap column-gap */
}

justify-content

Value Effect
flex-start Items packed at the start
center Items packed in the middle
flex-end Items packed at the end
space-between First and last at the edges, equal space between
space-around Equal space around each item (half-size at edges)
space-evenly Equal space everywhere, including edges

align-items

Value Effect
stretch Items fill the cross axis (default, if they have no fixed size)
flex-start / flex-end Aligned to the start or end of the cross axis
center Centered on the cross axis
baseline First text baselines line up, useful for mixed font sizes

align-content

Only matters in a container with flex-wrap: wrap (or wrap-reverse). It distributes the lines along the cross axis, with the same values as justify-content plus stretch. With flex-wrap: nowrap it does nothing, which is a common source of confusion.

Item properties

.item {
  flex: 0 1 auto;     /* grow shrink basis */
  align-self: auto;   /* override align-items for this item */
  order: 0;
}

flex-grow, flex-shrink and flex-basis

  • flex-basis: the starting size along the main axis before free space is distributed. auto means "use width (or height), or the content size".
  • flex-grow: how much of the remaining space the item takes, relative to other items. 0 means don't grow.
  • flex-shrink: how much the item shrinks, relative to others, when there isn't enough space. 0 means never shrink below the basis.

The flex shorthand

Shorthand Expands to Meaning
flex: initial 0 1 auto Default: content size, can shrink, won't grow
flex: auto 1 1 auto Content size, then share free space
flex: 1 1 1 0% Ignore content size, share all space equally
flex: none 0 0 auto Fixed at content size
flex: 2 2 1 0% Twice the share of a flex: 1 sibling

flex: 1 and flex: auto look similar but differ: flex: 1 gives equal widths regardless of content, flex: auto gives longer content more room.

Recipes

Center anything

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}
.nav { display: flex; align-items: center; gap: 16px; }
.nav .push { margin-left: auto; }

An auto margin absorbs all free space on that side, which is the simplest way to split a row into left and right groups.

body { display: flex; flex-direction: column; min-height: 100vh; }
main { flex: 1; }
.layout { display: flex; gap: 24px; }
.sidebar { flex: 0 0 240px; }   /* fixed 240px, never grows or shrinks */
.content { flex: 1; min-width: 0; }

Wrapping chips or tags

.tags { display: flex; flex-wrap: wrap; gap: 8px; }

Equal-width buttons

.actions { display: flex; gap: 8px; }
.actions > * { flex: 1; }

Common problems

Content overflows instead of shrinking

Flex items have min-width: auto by default, so they won't shrink below their content's minimum size. Long words, URLs, <pre> blocks and wide tables push the layout wider than the screen. Fix it on the item:

.content { min-width: 0; }   /* or overflow: hidden */

For column layouts, the same applies to min-height.

Images get squashed

Images in a row shrink with the default flex-shrink: 1. Use flex-shrink: 0 on the image, or set flex: none.

Percentage heights don't work in columns

A child with height: 100% needs a parent with a definite height. In a column flex container, use flex: 1 on the child instead.

Using flexbox for a two-dimensional grid

Wrapped flex rows don't align into columns: each line distributes its own space. For real rows and columns, use grid; see the CSS grid tutorial.

Spacing values like gap: 1rem scale with the user's font settings; px vs rem vs em explains when to use which.

Frequently asked questions

Flexbox or grid?

Flexbox lays out items in one direction and lets their content decide sizes; grid places items in rows and columns at the same time. Use flexbox for toolbars, navbars and lists of chips, grid for page layouts and card grids.

Why doesn't align-items center my text vertically?

It centers the flex items, not the text inside them. If the item is stretched to full height, add display: flex; align-items: center to the item too.

Is the gap property supported for flexbox?

Yes, in all current browsers. It replaces margin hacks for spacing between items.

What does order do?

It changes the visual order of items without changing the HTML. Screen readers and keyboard focus still follow the HTML order, so avoid using it for meaningful reordering.

Further reading