Activation
display: flex; /* turns direct children into flex items */
display: inline-flex; /* same, but container is inline-level */
The Axis Rule
Always remember
justify-content → main axis (direction items flow)
align-items → cross axis (perpendicular to flow)
When flex-direction changes, the axes swap. The rule stays the same.
Container Properties
flex-direction
row— left → right (default)row-reverse— right → leftcolumn— top → bottomcolumn-reverse— bottom → top
justify-content
flex-start— pack at start (default)flex-end— pack at endcenter— center as groupspace-between— first/last at edgesspace-around— equal around eachspace-evenly— equal everywhere
align-items
stretch— fill cross axis (default)flex-start— align to startflex-end— align to endcenter— center on cross axisbaseline— align text baselines
flex-wrap
nowrap— single line (default)wrap— wrap to new lineswrap-reverse— wrap upward
gap
gap: 16px— equal row + column gaprow-gap: 16px— between rows onlycolumn-gap: 16px— between columns onlygap: 16px 24px— row gap, column gap
align-content
Only works with flex-wrap: wrap
flex-start— pack lines to startflex-end— pack lines to endcenter— center linesspace-between— distribute linesstretch— lines fill space (default)
Item Properties
flex (shorthand)
flex: 1— grow equally, basis 0%flex: 0 0 auto— fixed sizeflex: 1 1 200px— grow/shrink from 200pxflex: none— completely inflexible
Individual Properties
flex-grow: 0— share of extra space (default: 0)flex-shrink: 1— shrink factor (default: 1)flex-basis: auto— ideal starting sizealign-self: center— override container's align-itemsorder: -1— visual order (default: 0)
Copy-Paste Patterns
Perfect centering
display: flex;
justify-content: center;
align-items: center;
Navigation bar (logo left, links right)
display: flex;
justify-content: space-between;
align-items: center;
Equal-width columns
.container { display: flex; gap: 16px; }
.column { flex: 1; }
Responsive card grid (no media queries)
.grid { display: flex; flex-wrap: wrap; gap: 16px; }
.card { flex: 1 1 280px; }
Sticky footer (content fills page)
body { display: flex; flex-direction: column; min-height: 100vh; }
main { flex: 1; }
/* header and footer stay content-sized */