← Cheat Sheets

Flexbox & Grid Cheat Sheet

Flexbox lays out items along one axis; Grid places them in two dimensions. Same box model, different superpowers.

Flexbox — container properties

PropertyCommon valuesDoes
displayflexEnables flex layout
flex-directionrow · column · row-reverseMain axis direction
justify-contentflex-start · center · space-between · space-aroundDistribution along the main axis
align-itemsstretch · center · flex-start · flex-endAlignment on the cross axis
flex-wrapnowrap · wrapWhether items can line-break
gap12px · 8px 16pxSpacing between items (row column)

Flexbox — item properties

PropertyCommon valuesDoes
flex-grow0 · 1 · 2How much free space an item absorbs
flex-shrink1 · 0How willingly an item shrinks
flex-basisauto · 200px · 0Initial size before growing/shrinking
flex1 · 0 auto · 1 1 0Shorthand for grow shrink basis
align-selfauto · center · flex-endPer-item cross-axis override
order0 · 1 · -1Visual reordering without touching HTML

Grid — container properties

PropertyCommon valuesDoes
displaygridEnables grid layout
grid-template-columnsrepeat(3, 1fr) · 200px 1fr · auto-fill minmax(240px, 1fr)Defines the columns
grid-template-rowsauto · repeat(2, minmax(0, 1fr))Defines the rows
gap16px · 8px 24pxGutters between tracks
justify-items / align-itemsstart · center · stretchPlacement inside each cell
place-itemscenterShorthand for align + justify items

FAQ

Flexbox or Grid — how do I choose?
One-dimensional (a toolbar, a nav, a card row that should stretch) → Flexbox. Two-dimensional or explicit rows AND columns → Grid. Most real layouts use both.

Why do my grid images overflow?
Grid tracks default to auto, so wide content expands the track. Use minmax(0, 1fr) instead of 1fr to force tracks to shrink below content size.

justify-content vs align-items?
justify = main axis, align = cross axis — and the main axis is whichever direction flex-direction points. Flip the direction and the two swap roles.

Try it instead of memorizing it

Preview it visually in the Flexbox & Grid Playground