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 horizontallyrow-reverse: Items flow right-to-left horizontallycolumn: Items flow top-to-bottom verticallycolumn-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.