Set the direction of flex items and control wrapping.
flex-direction chooses the main axis. flex-wrap decides whether items stay on one flex line or can move to additional lines. Together, they determine the flow of a flex layout.
.steps {
display: flex;
flex-direction: column;
gap: 1rem;
}In a horizontal writing mode, the steps form a vertical stack. The first step stays before the second in the document and appears above it.
| Direction | Main axis | Starting side in horizontal, left-to-right text |
|---|---|---|
row | Inline axis. | Left. |
row-reverse | Reversed inline axis. | Right. |
column | Block axis. | Top. |
column-reverse | Reversed block axis. | Bottom. |
A row follows the document's inline direction. For right-to-left text, row starts on the right; it is not permanently tied to the left edge.
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.cards > article {
flex: 0 1 10rem;
}Each article has a starting size, its basis, of 10rem. Flexbox decides which articles fit together before distributing extra space or shrinking the items on each line. Include the gaps when you predict the break. The next sizing lesson explains the three flex values.
With a 16-pixel root font size, no item margins or padding, and enough room for each card's content:
Items wrap when the container runs out of space, whatever device is in use. For a column to wrap into more columns, give it a height limit.
nowrap: The initial value. Items remain on one flex line, even if they shrink or overflow.wrap: Adds lines toward the cross-end side.wrap-reverse: Adds lines with cross-start and cross-end reversed.wrap-reverse changes the stacking direction of flex lines. It does not reverse the sequence of items within each line.
Direction and wrapping can also be written together:
.cards {
flex-flow: row wrap;
}Add this declaration to a flex container; the shorthand sets both properties.
Reversing the direction changes what you see, but keeps the HTML order and usual Tab order. Write the HTML in a sensible reading order first.
With the card sizes above, what happens at 320 pixels if you change wrap to nowrap? The cards can shrink; which part of the rule permits that?
They stay on one line. The middle 1 in flex: 0 1 10rem permits shrinking, although minimum sizes can still cause overflow.
A column needs multiple columns of steps. Which constraint must it have before wrapping can occur?
Give the column a limited main size (height in horizontal writing), so there is a point where another line is needed.
flex-flow combines direction and wrapping.Try it yourself
HTML + CSS