Two properties reshape a flex container completely: the direction the items flow in, and whether they wrap when they run out of room. Both are simple to set, and both have consequences that are easy to get wrong when the container is not horizontal.
The previous lesson in this module covered "What Does display:flex Change on a Container?", and the next lesson in this module is about "How Do justify-content and align-items Center Anything?".
What does flex-direction control?
It sets the main axis, and therefore the direction the items are placed in.
.row { display: flex; flex-direction: row; } /* the default */
.row-reverse{ display: flex; flex-direction: row-reverse; }
.column { display: flex; flex-direction: column; }
.column-rev { display: flex; flex-direction: column-reverse; }
The two reverse values are the ones people reach for to move something to the other end of a row, and they are a trap. row-reverse does not move the last item to the front — it reverses the visual order of the whole row, and the starting edge moves to the right. The first item in the markup ends up on the right.
<div class="row-reverse">
<span>First</span>
<span>Second</span>
<span>Third</span>
</div>
That has two consequences. The visual order no longer matches the DOM order, which matters for keyboard navigation and for anyone reading with a screen reader. And the horizontal scroll direction flips, so a row that scrolls sideways scrolls the other way.
The clean way to push one item to the far end of a row is to let the others share the space.
.toolbar {
display: flex;
gap: 0.5rem;
}
.spacer {
flex: 1; /* takes all the remaining space */
}
The spacer grows to fill the gap, so the last item ends up on the right without any reversal. The order in the markup stays the order on screen, which is the outcome you want almost every time.
When do items wrap?
By default they do not: they shrink, and if they cannot shrink enough they overflow. The flex-wrap property changes that.
.cards {
display: flex;
flex-wrap: wrap; /* move to a new line when there is no room */
gap: 1rem;
}
.no-wrap {
display: flex;
flex-wrap: nowrap; /* the default */
}
Wrapping is decided by the items' base size, before they grow or shrink. If the items have a basis of auto, that means their content width, so a row of images wraps when their combined content width no longer fits. If the items have a percentage basis, they wrap when the percentages no longer fit — which is how a row of cards that wrap into a grid works.
/* three cards that wrap onto new lines, each a third of the row */
.card {
flex: 0 0 30%;
margin: 0 1% 1rem;
}
Is there a shorthand for both?
Yes, and it is worth knowing even if you do not use it, because you will meet it in other people's stylesheets.
/* flex-flow: direction then wrap */
.container {
display: flex;
flex-flow: row wrap;
}
It reads in the same order as the two longhand properties, so it is easy to decode once you know that.
Why does reverse change what justify-content means?
Because the alignment properties follow the main axis, and the main axis has a direction. With row-reverse, the main axis still runs horizontally, but it now starts on the right, so justify-content: flex-start pushes the items to the right and flex-end pushes them to the left.
/* in a reversed row, "start" is the right-hand side */
.reversed {
flex-direction: row-reverse;
justify-content: flex-start; /* items packed to the right */
}
The same applies to column-reverse, where the main axis runs upwards, so justify-content: flex-start packs the items to the bottom. This is why reverse is best avoided unless you are certain you want the visual order to differ from the DOM order — and it is the reason a "reversed" layout is a common source of confusing bugs in maintenance.
How do you control where the wrap happens?
The wrap point is decided by the base sizes of the items, so the two things that control it are the flex-basis and any margins on the items. An item that is a third of the container in basis will wrap into thirds, and an item with a fixed basis will wrap when its fixed width no longer fits.
/* three cards, wrapping into rows of three */
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 0 1 18rem; /* basis 18rem, may shrink, never grows */
}
With a basis of 18rem, the cards sit three or four to a row depending on the container width, and the last row is left ragged rather than stretched. That ragged last row is the thing to plan for: a grid of cards with a flexible basis will rarely end on an exact row, and if it must, grid is the tool that can guarantee it.
Another approach is to let the items grow and shrink but fix the basis as a percentage, so the wrap happens on exact fractions of the container.
.card {
flex: 0 0 30%; /* 30% of the container, three to a row */
margin-right: 5%;
}
This is the older technique and it has a flaw: the margins have to be subtracted by hand to make the percentages add up, and it breaks if the gap changes. Using gap with a fractional basis and letting the items grow is more robust.
Is column-reverse ever useful?
It is used in a few specific patterns — a chat interface where new messages appear at the bottom, or a stack where items should be counted from the bottom up. In those cases the reversed direction is genuinely useful, and the alignment properties are then measured from the bottom.
.chat {
display: flex;
flex-direction: column-reverse;
justify-content: flex-end;
overflow-y: auto;
}
The caveat is the same as for a reversed row: the visual order no longer matches the DOM order, so anything that depends on the order — a screen reader reading the messages in sequence, or a form whose fields are tabbed through — will read them in the opposite order to the one the reader sees. It is a legitimate technique, but it should be a decision rather than an accident.
Quick reference
flex-directionsets the main axis: row, row-reverse, column, column-reverse.- Reverse changes the visual order, which breaks the match with the DOM order.
- To push one item to the far end, use a growing spacer rather than reversing.
flex-wrap:wrap moves items to a new line based on their base size.flex-flowis the shorthand for direction and wrap, in that order.- With a reversed axis,
flex-startmoves to the opposite end.
Try it yourself: build a row of three items, reverse it, and use the developer tools overlay to see where the main axis now starts. Then remove the reversal and replace it with a spacer that grows, and confirm the visual result is identical while the DOM order stays correct.