Overview
display: flex lays a container’s items along one main axis, set by flex-direction, and aligns them on the perpendicular cross axis. Use it for toolbars, nav links, button rows, and centering, letting content size drive the layout. When columns must line up across rows, use css-grid.
Rules
flex-directionisrow(default),column, or a-reversevariant.justify-contentaligns along the main axis andalign-itemsalong the cross axis, so switching tocolumnswaps what they do; that is why centering often breaks at a mobile breakpoint.rowfollows the writing mode’s inline direction: right to left in RTL text and top to bottom in verticalwriting-mode.- Avoid
row-reverseandcolumn-reversefor content users read or tab through: only visual order changes, while DOM, tab, and screen-reader order do not. flex-wrap: wrapmakes multi-line layouts whose lines size independently.- Flex items default to
min-width: auto, so long content will not shrink; setmin-width: 0on the item to allow it. - Use
gapfor spacing instead of margins.
.navbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
@media (max-width: 600px) { .navbar { flex-direction: column; } }Related
- css-grid: the two-dimensional layout model.
- box-model: flex items do not collapse margins.
- css-flexbox: layout patterns in depth.
- css: CSS conventions.
- tailwind: the
flex,items-*, andjustify-*utilities.