Container & items
01Put display: flex on the parent. Its direct children automatically become flex items — no property needed on them.
.row { display: flex; }
Main axis & direction
02flex-direction sets the main axis. Everything else is described relative to it.
(row, default)
(perpendicular)
.col { display: flex; flex-direction: column; }
justify-content — main axis
03Positions items along the main axis — the direction set by flex-direction.
flex-startcenterspace-betweenspace-aroundalign-items — cross axis
04Positions items along the cross axis — perpendicular to the main axis.
flex-startcenterflex-endstretch (default)gap
05Adds space between items only — not around the outside. Use it instead of margin on every item.
.row { display: flex; gap: 12px; }
flex-wrap
06By default items squeeze onto one line. wrap lets them drop to a new line instead.
wrapalign-content — wrapped lines
07Once flex-wrap creates more than one line, align-content spaces the lines apart. align-items still aligns items inside each line — one doesn't replace the other.
flex-wrap: wrap; + align-content: space-between;order & align-self — one item, not all
08Both go on the item, not the container — and override just that one child without touching the rest.
.item-3 { order: -1; } .item-2 { align-self: flex-end; }
flex-grow & flex-shrink — item sizing
09Distributes extra (flex-grow) or missing (flex-shrink) space unevenly. Goes on the item — on the container, it does nothing.
.item-2 { flex-grow: 1; } /* items 1 & 3 keep flex-grow: 0 (default) */
flex-basis — starting size before grow/shrink
10Sets an item's starting main-axis size before flex-grow/flex-shrink run. Goes on the item, and wins over width on the main axis.
.item-2 { flex-basis: 6rem; } /* shorthand: flex: grow shrink basis; */ .item-2 { flex: 1 1 6rem; }
Habits that keep layouts working
11- Ask two questions first: which element is the container, and what are its direct children?
- Direction defines the main axis — justification and alignment follow from that, so switching to
columnswaps their effect. - Test the layout at a narrow and a wide preview width before calling it done.
display: flex on an item instead of its parent does nothing for the layout you want.gap breaks as soon as content changes.