liveBook · Manning

Flex-direction

As explained in Web Design Playground, Second Edition

The CSS flex-direction property specifies the direction in which flex items are placed along the main axis of a flex container.

`flex-direction` chooses which way a flex container’s items run. Think of it as picking the direction for a line of boxes before you decide how to space or align them. Flexbox does not make you march only across the page or only down it—you choose the main axis. section 12.2

Set `display: flex` on the container, then choose one of four directions:

  • `row` runs items horizontally from left to right. It is the default, so writing `flex-direction: row` is optional. section 12.2
  • `row-reverse` runs items horizontally from right to left. section 12.2
  • `column` runs items vertically from top to bottom. section 12.2
  • `column-reverse` runs items vertically in reverse order. section 12.2
container {
    display: flex;
    flex-direction: row|row-reverse|column|column-reverse;
}

The property belongs on the flex container, not on each child item. Swap in a value to change the container’s main-axis direction. section 12.2

Stack a thumbnail and its caption

To put a caption below a photo, make the thumbnail wrapper a flex container and use `flex-direction: column`. Its image and caption then follow a vertical main axis. The same trick can make an overlaid caption flow downward before `justify-content: flex-end` places that content at the bottom. section 15.8 section 15.8

Direction changes more than the order of items: it defines the main axis. `justify-content` arranges items along that axis, while `align-items` works across it on the cross axis. Pick the direction first; the other Flexbox choices make much more sense afterward. section 12.2 section 12.7

How Web Design Playground, Second Edition covers it

How deep it goes
Defines main and cross axes and explains all four direction values through container syntax and visual results.
Examples
Demonstrates row and row-reverse layouts, then stacks photo-gallery thumbnails and captions with column.
What it assumes
Requires a flex container created with display: flex and introduces main and cross axes in plain terms.
What you can do after
Apply it via CSS to arrange flex items horizontally or vertically and build stacked image-caption layouts.
How it connects
Connects to Flexbox, justify-content, align-items, flex-wrap, align-content, flex-grow, flex-shrink, and flex-basis.

Also explained in

Compare how 3 books explain flex-direction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.