This page covers Flexbox (Flexible Box Layout): display: flex, direction, wrap,
justify-content, align-items, gap, and flex properties on children. Each example shows output and code.
display: flexOn a container, display: flex makes its direct children flex items. By
default they line up in a row, shrink to fit, and stretch vertically.
.container {
display: flex;
}
flex-directionControls the main axis direction. Values: row (default), row-reverse,
column, column-reverse.
row
column
.container { display: flex; flex-direction: row; } /* default */
.container { display: flex; flex-direction: column; }
row = main axis horizontal, left to right. row-reverse =
right to left. column = main axis vertical, top to bottom. column-reverse = bottom to
top.
justify-content — main-axis alignmentAligns flex items along the main axis. Values: flex-start,
flex-end, center, space-between, space-around,
space-evenly.
flex-start
center
space-between
.container { justify-content: flex-start; } /* default: start of main axis */
.container { justify-content: center; }
.container { justify-content: space-between; } /* space between items, none at edges */
flex-start = pack at start. flex-end = pack at end.
center = pack in the middle. space-between = equal space between items, no extra at
edges. space-around = space around each item (half at edges). space-evenly = equal
space everywhere.
align-items — cross-axis alignmentAligns flex items along the cross axis (perpendicular to main). Values:
flex-start, flex-end, center, stretch (default),
baseline.
align-items: center
align-items: flex-end
.container { align-items: flex-start; } /* top of cross axis */
.container { align-items: center; }
.container { align-items: stretch; } /* default: fill cross axis */
flex-start = top;
flex-end = bottom; center = middle; stretch = fill height.
baseline = align by text baseline.
flex-wrapWhether flex items wrap to the next line when there’s not enough space. Values: nowrap
(default), wrap, wrap-reverse.
.container {
display: flex;
flex-wrap: wrap;
}
nowrap = all items stay on one line (may shrink). wrap =
items wrap to next line when needed. wrap-reverse = wrap in reverse order. Use wrap
for responsive layouts.
gapAdds space between flex items (and between rows when wrapped). Values: gap: 10px,
gap: 10px 20px (row gap, column gap), or row-gap / column-gap separately.
.container {
display: flex;
gap: 16px;
}
gap adds space between items without using margins. Shorthand:
gap: row-gap column-gap. One value applies to both. Supported in all modern browsers.
flex-grow, flex-shrink, flex-basisOn flex items, these control how items grow or shrink when there’s extra or less
space. Shorthand: flex: grow shrink basis.
/* equal width */
.item { flex: 1 1 0; } /* grow: 1, shrink: 1, basis: 0 */
/* middle item twice as wide */
.item-2 { flex: 2 1 0; }
flex-grow: 1 = take equal share of extra space.
flex-shrink: 1 = shrink when needed. flex-basis: 0 = start from zero, let grow/shrink
control size. flex: 1 is shorthand for 1 1 0.
align-self — override for one itemOn a flex item, align-self overrides align-items for that item only.
Values: flex-start, flex-end, center, stretch,
baseline, auto.
.item { align-self: center; }
.item { align-self: flex-end; }
align-self when one item needs different cross-axis alignment
than the others. auto uses the container’s align-items.
orderControls the order of flex items. Default is 0. Lower values appear first; higher
values appear later.
.item-a { order: 3; }
.item-b { order: 1; }
.item-c { order: 2; }
align-content — multi-line cross-axisWhen flex items wrap into multiple lines, align-content aligns those lines along the
cross axis. Only applies when flex-wrap: wrap and there are multiple lines.
.container {
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
justify-content: flex-start,
center, space-between, etc. Controls spacing between rows when there are multiple
rows.
display: flex, flex-direction, flex-wrap,
justify-content, align-items, align-content, gap.flex (grow shrink basis), align-self, order.
justify-content = main axis; align-items = cross axis (single line);
align-content = cross axis (multi-line).