Skip to content

/cheatsheets/css/css-flexbox-cheat-sheet

CSS flexbox cheat sheet: container and item properties

The commands on this page have not been executed by our verification runner yet.updated 2026-10-20
tool
CSS
versions
CSS Flexible Box Layout Level 1
os
any

Flexbox lays out items in one direction, a row or a column. Properties on the container decide direction, wrapping and alignment; properties on the items decide how each one grows, shrinks and aligns itself.

Container

PropertyValuesEffect
displayflex | inline-flexturns on flexbox for the children
flex-directionrow | row-reverse | column | column-reversemain axis direction
flex-wrapnowrap | wrap | wrap-reverseallow items onto several lines
justify-contentflex-start | center | flex-end | space-between | space-around | space-evenlydistribution along the main axis
align-itemsstretch | flex-start | center | flex-end | baselinealignment across the main axis
align-contentsame as justify-content, plus stretchspacing of the lines when wrapping
gap<length> [<length>]space between items (row gap, column gap)

Items

PropertyValuesEffect
flex<grow> <shrink> <basis>, e.g. 1 1 0shorthand for grow, shrink and basis
flex-grow<number>share of the free space this item takes
flex-shrink<number>how much the item shrinks when space is short (0 = never)
flex-basisauto | <length>starting size before growing or shrinking
align-selfauto | flex-start | center | flex-end | stretchoverrides align-items for one item
order<integer>visual order (does not change tab order)

Recipes

css
/* centre anything */
.center { display: flex; justify-content: center; align-items: center; }

/* toolbar: logo left, actions right */
.toolbar { display: flex; align-items: center; gap: 1rem; }
.toolbar .actions { margin-left: auto; }

/* equal columns that wrap below 16rem */
.cards { display: flex; flex-wrap: wrap; gap: 1rem; }
.cards > * { flex: 1 1 16rem; }

/* sticky footer */
body { min-height: 100dvh; display: flex; flex-direction: column; }
main { flex: 1; }

flex: 1 is flex: 1 1 0%: items share the space equally regardless of their content. flex: auto (1 1 auto) shares the free space after each item's content size.

Sources