CSS Flexbox Cheat Sheet

Flexbox lays items out in one direction, in a row or a column. Turn it on with display: flex on the parent.

Container properties

PropertyCommon valuesWhat it does
flex-directionrow, column, row-reverseSets the main axis
flex-wrapnowrap, wrapLets items move to a new line
justify-contentflex-start, center, space-between, space-around, space-evenlySpaces items along the main axis
align-itemsstretch, center, flex-start, flex-end, baselineAligns items on the cross axis
align-contentsame as justify-contentSpaces wrapped lines
gap1remSpace between items

Item properties

PropertyWhat it does
flex-growHow much extra space the item takes, as a share
flex-shrinkHow easily it shrinks when space is short
flex-basisIts starting size before growing or shrinking
flex: 1Shorthand to share space equally
align-selfOverrides align-items for one item
orderChanges the visual order

Patterns

Centre anything

.parent { display: flex; justify-content: center; align-items: center; }

Navbar with the logo left and links right

.nav { display: flex; align-items: center; gap: 1rem; }
.nav .logo { margin-right: auto; }

Equal columns that wrap

.cards { display: flex; flex-wrap: wrap; gap: 1rem; }
.cards > * { flex: 1 1 16rem; }

Tips

  • justify-content follows the main axis, so in a column it moves items vertically.
  • An item that refuses to shrink may need min-width: 0.
  • Use Grid when you need rows and columns at the same time.
↑ ↓ navigateEnter openEsc close