π Introduction
The flex-direction property defines the main axis of a flex container β determining the direction in which flex items are laid out. It is one of the core properties of Flexbox and heavily influences layout structure, alignment, and responsiveness. Understanding flex-direction is essential for mastering Flexbox layouts. πβ¨
π― What Does flex-direction Control?
- Direction of item placement
- Whether items flow horizontally or vertically
- Order of placement (normal or reversed)
- Controls how other properties (justify-content, align-items) behave
β¨ Syntax
Syntax
container {
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
}π§© flex-direction Values
1. row (default)
Items are placed left β right in a horizontal line.
row
flex-direction: row;2. row-reverse
Items are placed right β left (reverse horizontal direction).
row-reverse
flex-direction: row-reverse;3. column
Items stack top β bottom in a vertical layout.
column
flex-direction: column;4. column-reverse
Items stack bottom β top (reverse vertical direction).
column-reverse
flex-direction: column-reverse;π₯ Practical Examples
1. Horizontal Navigation Bar
Horizontal Layout
nav {
display: flex;
flex-direction: row;
gap: 20px;
}2. Vertical Sidebar Menu
Vertical Sidebar
.sidebar {
display: flex;
flex-direction: column;
gap: 12px;
}3. Reverse Order List
Reverse Order
.reverse-list {
display: flex;
flex-direction: column-reverse;
}4. Chat Messages (Bottom-Up Layout)
Chat UI
.chat {
display: flex;
flex-direction: column-reverse;
height: 400px;
overflow-y: auto;
}5. Product Grid Using Flex Direction Row
Product Layout
.products {
display: flex;
flex-direction: row;
flex-wrap: wrap;
gap: 20px;
}π How flex-direction Affects Other Flex Properties
| Property | Main Axis (row) | Main Axis (column) |
|---|---|---|
| justify-content | Aligns items horizontally | Aligns items vertically |
| align-items | Aligns items vertically | Aligns items horizontally |
| flex-wrap | Wraps left to right | Wraps top to bottom |
π§ Tips & Best Practices
- Use row for horizontal layouts (navbars, grids)
- Use column for vertical stacking (sidebars, forms)
- Use column-reverse for chat UIs where new messages appear at the bottom
- Flex-direction heavily affects alignment β always think main axis vs cross axis
π Conclusion
The flex-direction property defines how flex items are laid outβhorizontally or vertically, forward or reversed. Once you understand how direction affects the main axis, alignment, wrapping, and item flow, Flexbox becomes intuitive and powerful. Use it wisely to build clean, responsive layouts with minimal effort. π