flex-direction
Specifies the direction in which items are arranged inside a flex container (the direction of the main axis). This is the foundational property of Flexbox layout.
Sample Code
/* Horizontal (default) */
.container { display: flex; flex-direction: row;}
/* Vertical */
.container-vertical { display: flex; flex-direction: column;}
/* Horizontal (reversed) */
.container-reverse { display: flex; flex-direction: row-reverse;}
/* Vertical (reversed) */
.container-reverse-v { display: flex; flex-direction: column-reverse;}
Available Values
| Value | Description |
|---|---|
| row | Arranges items horizontally (left to right). This is the default value. |
| row-reverse | Arranges items horizontally (right to left). The reverse of row. |
| column | Arranges items vertically (top to bottom). |
| column-reverse | Arranges items vertically (bottom to top). The reverse of column. |
Specifications
| Item | Details |
|---|---|
| Initial value | row |
| Applies to | Flex containers (elements with 'display: flex' or 'display: inline-flex') |
| Inherited | No |
| Animation | Not animatable (discrete) |
Browser Compatibility
28 △
27 △
26 △
25 △
24 △
23 △
22 △
21 △
20 and earlier ×
27 △
26 △
25 △
24 △
23 △
22 △
21 and earlier ×
8 △
7.1 △
7 △
6.1 △
6 and earlier ×
8 ×
7 ×
6 ×
12.1 ○
12 ×
11 and earlier ×
8.4 △
8 △
7.1 △
7 △
6.1 and earlier ×
Android Browser
4.4 ○
4.3 △
4.2 △
4.1 △
4.0 ×
3.0 ×
2.3 ×
2.2 ×
2.1 ×※ Version data is based on Can I Use.
Overview
'flex-direction' is the most fundamental property in Flexbox layout. It applies to elements with 'display: flex' or 'display: inline-flex' (flex containers) and determines the direction in which child elements (flex items) are arranged.
Main Axis and Cross Axis
The most important concepts to understand in Flexbox are the "main axis" and the "cross axis." The value of 'flex-direction' determines the direction of these two axes, and all other Flexbox properties behave relative to them.
When flex-direction: row
When flex-direction: column
Direction of 'justify-content' and 'align-items'
The 'justify-content' property controls alignment along the main axis, while 'align-items' controls alignment along the cross axis. This means that changing the value of 'flex-direction' also changes the direction in which 'justify-content' and 'align-items' take effect.
/* With row: justify-content works "horizontally", align-items works "vertically" */
.row-container {
display: flex;
flex-direction: row;
justify-content: center; /* Center horizontally */
align-items: center; /* Center vertically */
}
/* With column: justify-content works "vertically", align-items works "horizontally" */
.column-container {
display: flex;
flex-direction: column;
justify-content: center; /* Center vertically */
align-items: center; /* Center horizontally */
}
row (default)
Arranges items horizontally in the same direction as the document's writing direction. In left-to-right languages (LTR) like English, items are placed from left to right. In right-to-left languages (RTL) like Arabic, items are placed from right to left. This is the most commonly used value and is ideal for navigation menus and horizontally arranged cards.
row-reverse
Arranges items in the opposite direction of 'row'. In LTR, items are placed from right to left. Only the visual order changes; the DOM order remains the same (see the accessibility note below).
column
Arranges items vertically along the block axis (normally top to bottom). Suitable for sidebar menus and vertically stacked form fields. Note that when using 'column', you need to explicitly set a 'height' on the flex container for 'flex-wrap' wrapping and 'justify-content' spacing to take effect.
column-reverse
Arranges items in the opposite direction of 'column'. Normally places items from bottom to top. Used in limited scenarios such as chat UIs where the latest message appears at the bottom.
Pattern 1: Navigation Bar
/* Place logo and navigation links side by side */
.navbar {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
padding: 10px 20px;
}
Pattern 2: Card Vertical Layout
/* Stack card elements vertically and pin the footer to the bottom */
.card {
display: flex;
flex-direction: column;
height: 300px;
}
.card-body {
flex: 1; /* Fill the remaining space */
}
.card-footer {
/* Without flex: 1, it sits at its natural height at the bottom */
}
Pattern 3: Responsive Layout (switching between horizontal and vertical)
/* Desktop: horizontal → Mobile: vertical */
.layout {
display: flex;
flex-direction: row;
gap: 20px;
}
@media (max-width: 768px) {
.layout {
flex-direction: column;
}
}
This is one of the most practical Flexbox patterns. Simply switching the 'flex-direction' value with a media query achieves a responsive layout that is horizontal on desktop and vertical on mobile.
Relationship with the 'dir' Attribute
The direction of 'row' and 'row-reverse' depends on the HTML 'dir' attribute (document writing direction). You usually don't need to think about this for Japanese or English sites, but it becomes relevant when working with right-to-left (RTL) languages like Arabic or Hebrew.
| dir attribute | Order with 'row' | Order with 'row-reverse' |
|---|---|---|
| ltr (default) | Left → Right | Right → Left |
| rtl | Right → Left | Left → Right |
<!-- Standard LTR site (e.g. English) -->
<div style="display: flex; flex-direction: row;">
<div>Item 1</div> <!-- Left -->
<div>Item 2</div>
<div>Item 3</div> <!-- Right -->
</div>
<!-- RTL site (e.g. Arabic) -->
<div dir="rtl" style="display: flex; flex-direction: row;">
<div>Item 1</div> <!-- Right -->
<div>Item 2</div>
<div>Item 3</div> <!-- Left -->
</div>
In other words, 'row' means "same direction as the document's writing direction," and 'row-reverse' means "the opposite direction." Note that 'column' and 'column-reverse' are not affected by the 'dir' attribute.
Relationship with 'writing-mode'
In elements with 'writing-mode: vertical-rl' (vertical text), the directions of the main axis and cross axis differ from the default.
| writing-mode | Direction of 'row' | Direction of 'column' |
|---|---|---|
| horizontal-tb (default) | Horizontal (left → right) | Vertical (top → bottom) |
| vertical-rl | Vertical (top → bottom) | Horizontal (right → left) |
When working with vertical text layouts, be aware that the effective directions of 'row' and 'column' are swapped.
Accessibility Considerations
When using 'row-reverse' or 'column-reverse', consider the accessibility implications.
These values only change the visual display order through CSS; the HTML DOM order remains unchanged. Screen readers and keyboard Tab navigation follow the DOM order, so there will be a discrepancy between the visual order and the interaction order.
/* Example: visually "C → B → A", but Tab key focuses in "A → B → C" order */
.container {
display: flex;
flex-direction: row-reverse;
}
/* If this contains links or buttons, */
/* you may click the right button visually but Tab focuses the left button first */
For purely decorative reordering (e.g., text-only with no interactive elements), this is not a major issue. However, if you use 'reverse' for interactive elements like navigation menus or forms, prefer changing the HTML order directly.
'justify-content' Not Working with 'column'
When using 'flex-direction: column' with 'justify-content' to position items vertically, the effect will not work if the flex container has no explicit 'height'. This is because the container's height equals the sum of its items, leaving no room for spacing adjustments.
/* NG: no height, so justify-content has no effect */
.container-ng {
display: flex;
flex-direction: column;
justify-content: center; /* No effect */
}
/* OK: specifying height makes it work */
.container-ok {
display: flex;
flex-direction: column;
justify-content: center;
height: 100vh;
}
Combining 'reverse' with 'justify-content'
When 'row-reverse' is specified, the main axis direction itself is reversed. So 'justify-content: flex-start' places items at the "right end," and 'flex-end' places them at the "left end." This can be counter-intuitive, so pay attention.
Confusing 'flex-direction' with the 'order' Property
To change the order of individual items, use the 'order' property. The 'reverse' values in 'flex-direction' reverse the entire layout at once and cannot move specific items individually.
Related Properties
| Property | Description |
|---|---|
| display | Set to 'flex' or 'inline-flex' to create a flex container. 'flex-direction' is only valid on flex containers. |
| flex-wrap | Controls whether items wrap. Specifies whether items that don't fit along the main axis determined by 'flex-direction' wrap to the next line. |
| flex-flow | Shorthand for 'flex-direction' and 'flex-wrap'. Example: 'flex-flow: row wrap;' |
| justify-content | Controls item alignment along the main axis. The effective direction changes based on the value of 'flex-direction'. |
| align-items | Controls item alignment along the cross axis. The effective direction changes based on the value of 'flex-direction'. |
| order | Specifies the display order of individual flex items. Use this when you want to move a specific item rather than reversing the entire layout. |
If you find any errors or copyright issues, please contact us.