flex-direction: Row and Column Layouts
You'll learn: How to control whether flex items line up horizontally or vertically using flex-direction.
The Direction Switch
When you create a flex container with display: flex, your items automatically line up in a row (horizontally, side by side). But Flexbox gives you complete control over this direction with the flex-direction property.
Think of flex-direction as choosing whether to arrange books on a shelf horizontally (spine out, side by side) or vertically (stacked one on top of another). It's the fundamental decision that shapes your entire layout.
The Four Values
flex-direction accepts four values:
row(default): Items line up horizontally, left to rightrow-reverse: Items line up horizontally, right to leftcolumn: Items stack vertically, top to bottomcolumn-reverse: Items stack vertically, bottom to top
.flex-container {
display: flex;
flex-direction: row; /* horizontal layout */
}
.vertical-layout {
display: flex;
flex-direction: column; /* vertical layout */
}
Why This Matters
Remember that flex-direction determines your main axis. When you switch from row to column, you're rotating your entire coordinate system. The properties you learned like justify-content always work along the main axis—so changing direction changes what they control.
Row layouts are perfect for navigation bars or horizontal card displays. Column layouts excel at vertical stacking scenarios like sidebars or mobile-first designs.
Key Takeaway: flex-direction controls whether flex items flow horizontally (row) or vertically (column), fundamentally changing your layout's main axis direction.