🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Flex-direction

AI & DATA SCIENCE // flex-direction

The flex-direction property sets the main axis of a flex container, determining whether flex items are laid out in a row or a column, and in which order.

Syntax

flex-direction: row | row-reverse | column | column-reverse;

Deep Dive Course

row, the default, lays items out horizontally from left to right, in a left-to-right language, row-reverse lays them out horizontally but in reverse order, column stacks items vertically from top to bottom, and column-reverse stacks them vertically but in reverse order. Changing flex-direction between row and column also swaps which axis is considered the main axis versus the cross axis, which directly affects how justify-content and align-items behave, since justify-content always aligns along the main axis and align-items always aligns along the cross axis, regardless of which direction that currently corresponds to.

1Understanding Flex-direction

row, the default, lays items out horizontally from left to right, in a left-to-right language, row-reverse lays them out horizontally but in reverse order, column stacks items vertically from top to bottom, and column-reverse stacks them vertically but in reverse order. Changing flex-direction between row and column also swaps which axis is considered the main axis versus the cross axis, which directly affects how justify-content and align-items behave, since justify-content always aligns along the main axis and align-items always aligns along the cross axis, regardless of which direction that currently corresponds to.

💡

Remember that changing flex-direction to column swaps the meaning of justify-content and align-items too, since justify-content always follows the main axis and align-items always follows the cross axis — what was horizontal alignment in row mode becomes vertical alignment in column mode, and vice versa.

editor.html
.sidebar {
  display: flex;
  flex-direction: column;
}
localhost:3000

2Practical Example

Here is a real-world application of Flex-direction showing how it is used in production CSS code.

editor.html
.row {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.column {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Flex-direction:

1. Use flex-direction: column for a vertically stacked layout, like a sidebar or a card's internal content, rather than trying to force a row-based layout to behave vertically

2. Remember that justify-content and align-items swap their practical, visual effect when switching between row and column, since they always follow the main and cross axes respectively, not fixed horizontal/vertical directions

3. Use row-reverse or column-reverse sparingly, and be mindful they can create a confusing mismatch between visual order and the underlying DOM/tab order for keyboard and screen-reader users

⚠️

Tip: Remember that changing flex-direction to column swaps the meaning of justify-content and align-items too, since justify-content always follows the main axis and align-items always follows the cross axis — what was horizontal alignment in row mode becomes vertical alignment in column mode, and vice versa.

editor.html
.sidebar {
  display: flex;
  flex-direction: column;
}
localhost:3000

Examples

Example 01Basic Usage
.sidebar {
  display: flex;
  flex-direction: column;
}
Example 02Advanced Example
.row {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.column {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

Best Practices

  • Use flex-direction: column for a vertically stacked layout, like a sidebar or a card's internal content, rather than trying to force a row-based layout to behave vertically
  • Remember that justify-content and align-items swap their practical, visual effect when switching between row and column, since they always follow the main and cross axes respectively, not fixed horizontal/vertical directions
  • Use row-reverse or column-reverse sparingly, and be mindful they can create a confusing mismatch between visual order and the underlying DOM/tab order for keyboard and screen-reader users

Interview Question

Why does justify-content: center align items horizontally in a row-direction flex container, but vertically in a column-direction one, despite using the exact same declaration?

Hint: Think about what justify-content is actually defined to align along — a fixed, literal horizontal direction, or something more abstract tied to flex-direction.

justify-content isn't defined to align items along a fixed, literal horizontal or vertical direction at all, it's specifically defined to align items along the flex container's main axis, whichever direction that currently happens to be, and the main axis's actual direction is determined entirely by the flex-direction property. In a row-direction container, the main axis runs horizontally, so justify-content: center naturally centers items along that horizontal axis, but switching flex-direction to column reorients the main axis to run vertically instead, and justify-content, still faithfully aligning along whatever the current main axis is, now centers items vertically instead, without justify-content's own definition or behavior changing in any way. This axis-relative, rather than direction-fixed, definition is a deliberate, core part of flexbox's design, letting the exact same alignment properties, justify-content and align-items, work consistently and predictably regardless of which direction the container's main axis happens to be oriented in.

Exercises

MediumPractice using Flex-direction in a real scenario.
View Solution
.sidebar {
  display: flex;
  flex-direction: column;
}

Frequently Asked Questions

Why does justify-content: center align items horizontally in a row-direction flex container, but vertically in a column-direction one, despite using the exact same declaration?

justify-content isn't defined to align items along a fixed, literal horizontal or vertical direction at all, it's specifically defined to align items along the flex container's main axis, whichever direction that currently happens to be, and the main axis's actual direction is determined entirely by the flex-direction property. In a row-direction container, the main axis runs horizontally, so justify-content: center naturally centers items along that horizontal axis, but switching flex-direction to column reorients the main axis to run vertically instead, and justify-content, still faithfully aligning along whatever the current main axis is, now centers items vertically instead, without justify-content's own definition or behavior changing in any way. This axis-relative, rather than direction-fixed, definition is a deliberate, core part of flexbox's design, letting the exact same alignment properties, justify-content and align-items, work consistently and predictably regardless of which direction the container's main axis happens to be oriented in.

Related Functions

Display-flexJustify-contentAlign-items