Sub topic 3
Choosing a direction with flex-direction
This chapter assumes you can write CSS selectors, understand the box model, and have already used display: flex to make a flex container. It also assumes you know that a container has a main axis and a cross axis.
What flex-direction does
flex-direction is set on the flex container. It picks the main axis and the direction items flow along it. The cross axis is always the perpendicular one.
| Value | Main axis | Items flow | Cross axis |
|---|---|---|---|
| `row` (default) | horizontal | start to end of the writing direction, left to right in English | vertical |
| `row-reverse` | horizontal | end to start, right to left in English | vertical |
| `column` | vertical | top to bottom | horizontal |
| `column-reverse` | vertical | bottom to top | horizontal |
The default is row, so a container with no flex-direction lays items out in a horizontal line.
A worked example
Here is a list of three items stacked vertically in a container.
<div class="stack">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>.stack {
display: flex;
flex-direction: column;
gap: 0.5rem;
height: 300px;
justify-content: space-between;
}With column, the main axis is vertical. So justify-content: space-between now spreads the items from the top of the 300px container to the bottom. In a row container it would spread them left to right. The property did not change. The axis it works on did.
The same swap applies to align-items. In a column container it aligns items horizontally, because the cross axis is now horizontal.
Reverse values
row-reverse and column-reverse flip where the main axis starts. The first item in the HTML goes to the opposite end, and the rest follow in reverse.
.reversed {
display: flex;
flex-direction: row-reverse;
justify-content: flex-start;
}Here flex-start means the start of the main axis. With row-reverse the start is the right edge, so the items pack against the right side. This is why the spec uses flex-start and flex-end instead of left and right.
Common errors
- Alignment seems to do the wrong thing. After changing to
column,justify-contentmoves items vertically. Re-check which axis each property uses. - Centering stops working.
justify-content: centerandalign-items: centerstill center on both axes, but a column container only has extra vertical space if it has a height. Give it aheightormin-height. - Reversed visual order does not change the DOM order. Keyboard focus and screen readers follow the HTML order, not the visual order. Reversing a row of links can make tabbing jump around visually.
Exercise
Exercise
A container has display: flex; flex-direction: column; height: 400px; align-items: center;. Along which direction are the items centered, and why?
Show answer
They are centered horizontally. In a column container the main axis is vertical, so the cross axis is horizontal, and align-items works on the cross axis.
Exercise
In a container with flex-direction: row-reverse and justify-content: flex-start, which side do the items pack against in an English page?
Show answer
The right side. row-reverse makes the main axis start at the right edge, and flex-start packs items at the start of the main axis.