liveBook · Manning

Flex-direction

As explained in Web Design Playground

A CSS flexbox property that sets the direction of the main axis and the order in which flex items flow.

Flexbox lets a container choose which way its items travel. The flex-direction property makes that choice: across like a row of seats, or down like a stack of cards. That chosen route is called the main axis; the perpendicular route is the cross axis. section 12.2

Set flex-direction on a flex container to choose its main axis:

  • row places items horizontally from left to right. It is the default, so writing flex-direction: row is optional. section 12.2
  • row-reverse keeps a horizontal main axis but runs items from right to left. section 12.2
  • column places items vertically from top to bottom; the cross axis then runs left to right. section 12.2
  • column-reverse uses a vertical main axis from bottom to top and reverses the item order. section 12.2
container {
    display: flex;
    flex-direction: row|row-reverse|column|column-reverse;
}

This rule shows the four available values. First make the element a flex container with display: flex, then pick the direction that fits the layout. section 12.2

Direction matters because other flexbox properties work along one axis or the other. After flex-direction picks the main axis, justify-content arranges items along that main axis, while align-items works across it on the cross axis. section 12.18 section 12.2

Center a portfolio layout, then stack its content

A page can use two containers with different directions. Set the body to row and use justify-content: center to center its content horizontally. Inside it, set a nested div to column with justify-content: flex-start to create a top-aligned vertical content stack. One direction handles the overall page; the other handles the content inside it. section 20.10

Try changing only the value from row to row-reverse, column, and column-reverse. Watch for two changes: whether the items run across or down, and which end receives the first item. Once that feels natural, properties such as justify-content and align-items become easier to aim. section 12.2 section 12.18

How Web Design Playground covers it

How deep it goes
Defines the main and cross axes, explains all four values, and shows their visible layout effects.
Examples
Uses div and header flex containers plus a nested body-and-div portfolio layout.
What it assumes
Requires a flex container using display: flex and introduces main and cross axes from zero.
What you can do after
Operate/configure it to make flex items flow horizontally or vertically, including reversed orders.
How it connects
Connects flex-direction to the main axis, cross axis, justify-content, align-items, flex-wrap, and align-content.

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. The section links point to the parts of the book it draws on.