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-direction is row (default), column, or a -reverse variant. justify-content aligns along the main axis and align-items along the cross axis, so switching to column swaps what they do; that is why centering often breaks at a mobile breakpoint.
  • row follows the writing mode’s inline direction: right to left in RTL text and top to bottom in vertical writing-mode.
  • Avoid row-reverse and column-reverse for content users read or tab through: only visual order changes, while DOM, tab, and screen-reader order do not.
  • flex-wrap: wrap makes multi-line layouts whose lines size independently.
  • Flex items default to min-width: auto, so long content will not shrink; set min-width: 0 on the item to allow it.
  • Use gap for spacing instead of margins.
.navbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
 
@media (max-width: 600px) { .navbar { flex-direction: column; } }
  • 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-*, and justify-* utilities.