CSS

CSS Flexbox Cheatsheet

Quick reference for CSS Flexbox layout including container properties, item properties, alignment, and common layout patterns.

Container Properties

.container {
  display: flex;          /* or inline-flex */
}

flex-direction

ValueDescription
rowLeft to right (default)
row-reverseRight to left
columnTop to bottom
column-reverseBottom to top

flex-wrap

ValueDescription
nowrapSingle line (default)
wrapWrap to new lines
wrap-reverseWrap upward/leftward

flex-flow (shorthand)

flex-flow: row wrap;  /* flex-direction + flex-wrap */

justify-content (main axis)

ValueDescription
flex-startPack to start (default)
flex-endPack to end
centerCenter items
space-betweenEven space between items
space-aroundEven space around items
space-evenlyEqual space between and around

align-items (cross axis)

ValueDescription
stretchStretch to fill (default)
flex-startAlign to top/start
flex-endAlign to bottom/end
centerCenter vertically
baselineAlign text baselines

align-content (multi-line cross axis)

ValueDescription
stretchStretch lines to fill (default)
flex-startPack lines to start
flex-endPack lines to end
centerCenter lines
space-betweenEven space between lines
space-aroundEven space around lines
space-evenlyEqual space between and around lines

gap

gap: 16px;            /* row-gap and column-gap */
row-gap: 16px;        /* gap between rows */
column-gap: 8px;      /* gap between columns */
gap: 16px 8px;        /* row-gap column-gap */

Item Properties

order

.item { order: 2; }  /* Default: 0. Lower values appear first */

flex-grow

.item { flex-grow: 1; }  /* Default: 0. How much to grow */

flex-shrink

.item { flex-shrink: 0; }  /* Default: 1. How much to shrink */

flex-basis

.item { flex-basis: 200px; }  /* Default: auto. Initial size */

flex (shorthand)

ValueEquivalent
flex: 1flex: 1 1 0%
flex: autoflex: 1 1 auto
flex: noneflex: 0 0 auto
flex: 0 1 200pxgrow=0, shrink=1, basis=200px

align-self

.item { align-self: center; }
/* Values: auto | flex-start | flex-end | center | baseline | stretch */

Common Patterns

Center an element

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}
body { display: flex; flex-direction: column; min-height: 100vh; }
main { flex: 1; }
nav { display: flex; justify-content: space-between; align-items: center; }

Equal-width columns

.container { display: flex; gap: 16px; }
.column { flex: 1; }

Last item pushed to end

.container { display: flex; }
.item:last-child { margin-left: auto; }

Responsive wrap with minimum size

.container { display: flex; flex-wrap: wrap; gap: 16px; }
.item { flex: 1 1 300px; }

Holy grail layout

.page { display: flex; flex-direction: column; min-height: 100vh; }
.content { display: flex; flex: 1; }
.sidebar { flex: 0 0 250px; }
.main { flex: 1; }

Vertically centered icon + text

.btn { display: flex; align-items: center; gap: 8px; }