Center a div with flexbox
Horizontal and vertical centring is the usual reason people reach for flexbox on a single row. Set display: flex, pick justify-content: center for the main axis and align-items: center for the cross axis when direction is row. Swap which property feels “horizontal” when you switch to column—main axis follows direction.
justify-content versus align-items
| Property | Axis it moves on (row direction) | Values available here |
|---|---|---|
| justify-content | Horizontal (main) | flex-start, center, flex-end, space-between, space-around, space-evenly |
| align-items | Vertical (cross) | stretch, flex-start, center, flex-end |
| gap | Between items | 0–40 px |
The preview uses four labelled boxes so you can see wrapping when flex-wrap: wrap is enabled. Copied CSS targets a class named .row—rename to match your markup.
Does this set flex on children?
No. Only the container properties appear in the output. Child flex values such as flex: 1 or align-self are outside the playground.