Sub topic 3
Choosing a direction with flex-direction
This chapter assumes you can write basic HTML and CSS selectors, understand the box model, and have read the previous chapters on flex containers and the main and cross axes. Here you will learn what flex-direction changes, what it leaves alone, and which errors appear when you switch it.
What the property does
flex-direction is set on the flex container. It picks the direction of the main axis, which is the direction in which items are laid out one after another. The cross axis is always perpendicular to it.
.container {
display: flex;
flex-direction: row; /* the default */
}There are four values:
| Value | Main axis runs | Items are placed |
|---|---|---|
| `row` | Along the inline direction (left to right in English) | First item at the main-start edge |
| `row-reverse` | Along the inline direction, flipped | First item at the main-end edge |
| `column` | Along the block direction (top to bottom in English) | First item at the top |
| `column-reverse` | Along the block direction, flipped | First item at the bottom |
The initial value is row. If you never set the property, a flex container behaves as a row.
The axes follow the writing mode
row does not mean "left to right". It means "the inline direction of the container's writing mode". In a page with dir="rtl" (Arabic or Hebrew), row runs right to left. In a vertical writing mode, such as writing-mode: vertical-rl, row runs top to bottom, and column becomes the horizontal direction.
This is why the spec uses the terms main-start and main-end instead of left and right. The values flex-start and flex-end in justify-content refer to those edges, so they flip automatically with the direction.
Reversing is visual only
row-reverse and column-reverse swap main-start and main-end. The items are painted in the opposite order, but the DOM does not change. Tab order, screen reader order, and text selection still follow the HTML source.
.steps {
display: flex;
flex-direction: row-reverse;
justify-content: flex-start;
gap: 0.5rem;
}With this rule, the first .steps child appears at the right edge. justify-content: flex-start now packs items against the right edge, because the main-start edge has moved. If you copy a rule that used flex-start into a reversed container, the items jump to the other side.
Switching to a column
column stacks items vertically. Three things change at once when you switch from row:
justify-contentnow acts vertically.align-itemsnow acts horizontally.flex-basis,flex-growandflex-shrinknow apply to height, not width.
.sidebar {
display: flex;
flex-direction: column;
gap: 0.75rem;
width: 16rem;
}Because align-items defaults to stretch, each child of this column fills the full 16rem width. If you want children to keep their natural width, set align-items: flex-start.
The height problem
A row container gets its width from its parent, so there is usually free space on the main axis. A column container has height: auto by default, so it is only as tall as its content. There is then no free space on the main axis, and justify-content and flex-grow appear to do nothing.
.panel {
display: flex;
flex-direction: column;
justify-content: space-between;
min-height: 20rem;
}Here min-height gives the container more height than its content needs, so space-between can distribute the leftover space. Without a height, space-between has nothing to distribute. The same cause explains why flex: 1 on a child of a column container does nothing until the container has a definite or minimum height. The sticky-footer pattern, which you will see later, relies on min-height: 100vh for this reason.
Direction and the flex shorthand
The meaning of flex-basis depends on the direction. In a row, flex: 1 1 200px sets a starting width of 200px. In a column, the same declaration sets a starting height of 200px. Write the basis with this in mind:
.stack {
display: flex;
flex-direction: column;
height: 30rem;
}
.stack .header {
flex: 0 0 4rem; /* fixed 4rem tall */
}
.stack .body {
flex: 1 1 0; /* takes the remaining height */
min-height: 0;
}The min-height: 0 on .body matters when the body holds scrolling content. The automatic minimum size of a flex item stops it from shrinking below its content, and a later chapter covers this in detail.
The flex-flow shorthand
flex-flow combines flex-direction and flex-wrap in one declaration. The order of the two values does not matter.
.gallery {
display: flex;
flex-flow: row wrap;
}This is the same as writing flex-direction: row; flex-wrap: wrap;. A common slip is to use flex-flow: column and expect wrapping. The omitted flex-wrap falls back to nowrap, so only the direction is set.
Switching direction at breakpoints
A frequent use of the property is to stack items on small screens and put them in a row on larger ones.
.media {
display: flex;
flex-direction: column;
gap: 1rem;
}
@media (min-width: 40rem) {
.media {
flex-direction: row;
align-items: center;
}
}Mobile first, the column is the base rule and the row is the enhancement. Notice that align-items: center only appears in the row rule. In the column, you probably want the default stretch.
Common errors
- Using
align-itemsfor the wrong axis. After switching tocolumn,align-items: centercenters horizontally. Developers who wanted vertical centering must switch tojustify-content. - Expecting a column to fill its parent. Without a height, the column is as tall as its content. Set
min-heightorheight. - Reversing to fix a source-order problem. The visual order changes but the focus order does not. Fix the HTML order instead.
- Forgetting
wrap-reverseis separate.flex-directiondoes not reverse lines.flex-wrap: wrap-reversereverses the cross-axis direction in which lines stack. - Assuming
rowmeans left to right. It follows the writing mode anddirattribute.
Exercise
A container has display: flex; flex-direction: column; justify-content: center; and no height. A designer expects its single child to sit in the vertical middle of a 400px-tall section, but the child sits at the top. What are two valid fixes, and what does align-items: center do on this container?
Show answer
The container is only as tall as its child, so there is no free vertical space to distribute. Fix 1: give the container min-height: 400px (or height: 100% inside a parent with a definite height). Fix 2: make the section itself the flex container with a column direction and a set height. align-items: center acts on the cross axis, which is horizontal in a column, so it centers the child horizontally and shrinks it to its natural width instead of stretching.