Course contentsShow
← Web Development
Lesson 257 of 2,9825. FlexboxFree lesson

flex-direction: Controlling the Main Axis

Using row, row-reverse, column, and column-reverse to set the direction of flex items.

flex-direction: Controlling the Main Axis

What you'll learn: You'll discover how to change the direction your flex items flow using the flex-direction property.

The Core Concept

Remember that Flexbox has a main axis that determines the primary direction flex items flow? By default, it runs left-to-right (like reading English). The flex-direction property lets you rotate that axis to flow in different directions—right-to-left, top-to-bottom, or even bottom-to-top.

Think of it like a train track: you're deciding whether the train runs horizontally or vertically, and in which direction along that track.

The Four Values

You have four options for flex-direction:

  • row (default): Items flow left-to-right horizontally
  • row-reverse: Items flow right-to-left horizontally
  • column: Items flow top-to-bottom vertically
  • column-reverse: Items flow bottom-to-top vertically
.container {
  display: flex;
  flex-direction: row; /* left → right */
}

.vertical-container {
  display: flex;
  flex-direction: column; /* top → bottom */
}

.reversed-container {
  display: flex;
  flex-direction: row-reverse; /* right → left */
}

When to Use Each Direction

Use row for horizontal navigation menus or image galleries. Use column for vertical lists or stacked cards. The -reverse versions are useful for visual reordering (like placing important items at the end in HTML but displaying them first visually) or for right-to-left language layouts.

Key Takeaway: flex-direction rotates the main axis of your flex container, controlling whether items flow horizontally (row, row-reverse) or vertically (column, column-reverse), and in which direction along that axis.