CSS flex-direction Property
The flex-direction property defines the main axis of a flex container and determines the direction in which its flex items are arranged.
It supports horizontal and vertical arrangements, including reversed directions. The property is applied to the flex container, while its direct children are arranged as flex items along the selected main axis.
flex-direction Syntax
The flex-direction property accepts four directional keywords and the standard CSS-wide keywords.
Basic Syntax
selector {
flex-direction: row;
}
Directional Values
.container {
flex-direction: row;
}
.container {
flex-direction: row-reverse;
}
.container {
flex-direction: column;
}
.container {
flex-direction: column-reverse;
}
CSS-Wide Keywords
.container {
flex-direction: inherit;
}
.container {
flex-direction: initial;
}
.container {
flex-direction: unset;
}
.container {
flex-direction: revert;
}
.container {
flex-direction: revert-layer;
}
The initial value is row. The container must use display: flex or display: inline-flex for the direction to control flex item placement.
Property Values
The four directional values determine the main axis and its starting direction.
| Value | Description |
|---|---|
row |
Arranges items along the inline axis, from main-start to main-end. This is the initial value. |
row-reverse |
Arranges items along the inline axis in the opposite direction. |
column |
Arranges items along the block axis, from main-start to main-end. |
column-reverse |
Arranges items along the block axis in the opposite direction. |
CSS-Wide Values
| Value | Description |
|---|---|
inherit |
Uses the computed value of the parent element. |
initial |
Restores the initial value, row. |
unset |
Restores row because the property is not inherited. |
revert |
Reverts the property according to the CSS cascade origins. |
revert-layer |
Reverts the property according to the CSS cascade layers. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | row |
| Inherited | No |
| Applies to | Flex containers |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not applicable |
The property controls the arrangement of flex items, not their source order in the HTML document.
How It Works
Flexbox arranges items along a main axis. The flex-direction property selects that axis and determines which end is considered main-start.
Step 1: Create a Flex Container
.container {
display: flex;
}
The default direction is row, so items follow the inline axis.
Step 2: Change the Direction
.container {
display: flex;
flex-direction: column;
}
The items now follow the block axis, which is vertical in a typical horizontal writing mode.
Step 3: Reverse the Direction
.container {
display: flex;
flex-direction: column-reverse;
}
The items are visually arranged from the opposite end of the block axis.
Step 4: Combine with Other Flex Properties
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: stretch;
gap: 12px;
}
Changing the main axis also changes the axis along which properties such as justify-content distribute available space.
row Value
The row value arranges flex items along the inline axis. In a typical left-to-right horizontal writing mode, items appear from left to right.
Syntax
.container {
display: flex;
flex-direction: row;
}
HTML Example
<div class="row-layout">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
CSS Example
.row-layout {
display: flex;
flex-direction: row;
gap: 10px;
}
.row-layout > div {
padding: 15px;
background-color: #dceaf7;
}
How It Works
The items follow the inline direction of the container. The main axis is horizontal in a horizontal writing mode, and the cross axis is perpendicular to it.
Important Consideration
Items do not automatically wrap onto another line. Use flex-wrap: wrap when wrapping is required.
row-reverse Value
The row-reverse value reverses the main-start and main-end directions along the inline axis.
Syntax
.container {
display: flex;
flex-direction: row-reverse;
}
Example
.reverse-row {
display: flex;
flex-direction: row-reverse;
gap: 10px;
}
In a left-to-right horizontal writing mode, the first flex item is positioned at the right side, followed by subsequent items toward the left.
Source Order vs. Visual Order
<div class="reverse-row">
<button>First</button>
<button>Second</button>
<button>Third</button>
</div>
The visual arrangement is reversed, but the HTML source order remains First, Second, Third. Keyboard navigation and assistive technology reading order generally follow the document order rather than the reversed visual arrangement.
Important Consideration
Avoid using row-reverse to rearrange meaningful reading or interaction sequences. The visual order can differ from keyboard focus order.
column Value
The column value arranges flex items along the block axis. In a typical horizontal writing mode, the items appear from top to bottom.
Syntax
.container {
display: flex;
flex-direction: column;
}
Example
.vertical-layout {
display: flex;
flex-direction: column;
gap: 12px;
}
.vertical-layout > div {
padding: 15px;
background-color: #dceaf7;
}
Common Uses
Column layouts are useful for stacked form controls, vertical navigation, card content, and page sections that need to follow a vertical arrangement.
Using Flexible Height
.panel {
display: flex;
flex-direction: column;
height: 350px;
}
.panel-content {
flex: 1;
min-height: 0;
overflow: auto;
}
The content area can expand into the available vertical space between other flex items.
Important Consideration
In a column layout, flex-basis affects the main-axis height in a horizontal writing mode, rather than the width.
column-reverse Value
The column-reverse value arranges items along the block axis with main-start and main-end reversed.
Syntax
.container {
display: flex;
flex-direction: column-reverse;
}
Example
.reverse-column {
display: flex;
flex-direction: column-reverse;
gap: 10px;
}
In a horizontal, top-to-bottom writing mode, the first item appears below subsequent items.
Example with a Fixed Height
.reverse-column {
display: flex;
flex-direction: column-reverse;
height: 300px;
gap: 10px;
}
.reverse-column > div {
padding: 15px;
background-color: #dceaf7;
}
The first item is positioned toward the bottom of the container, with later items arranged above it.
Important Consideration
As with row-reverse, the reversed visual arrangement does not change the HTML source order. Use it carefully when the order conveys meaning.
Main and Cross Axes
Flexbox uses two perpendicular axes. Understanding them helps explain how flex-direction affects sizing and alignment.
Main Axis
The main axis is the direction along which flex items are laid out. Its direction is determined by flex-direction.
Cross Axis
The cross axis is perpendicular to the main axis. Properties such as align-items control alignment along this axis.
Axis Comparison
| Direction | Main Axis | Cross Axis |
|---|---|---|
row |
Inline axis | Block axis |
row-reverse |
Inline axis, reversed | Block axis |
column |
Block axis | Inline axis |
column-reverse |
Block axis, reversed | Inline axis |
Why the Axes Matter
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: flex-start;
}
In a typical horizontal writing mode, justify-content centers items horizontally, while align-items aligns them vertically.
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: flex-start;
}
With column, the main axis becomes vertical and the cross axis becomes horizontal.
Writing Direction
Flex direction is based on logical writing axes, not fixed physical directions. The direction and writing-mode properties can influence the visual result.
Left-to-Right Writing
.container {
display: flex;
direction: ltr;
flex-direction: row;
}
In a horizontal writing mode, items progress from left to right.
Right-to-Left Writing
.container {
display: flex;
direction: rtl;
flex-direction: row;
}
In a horizontal writing mode, items progress from right to left.
Right-to-Left with row-reverse
.container {
display: flex;
direction: rtl;
flex-direction: row-reverse;
}
The main direction is reversed, so the items progress from left to right in this example.
Vertical Writing Mode
.container {
display: flex;
writing-mode: vertical-rl;
flex-direction: row;
}
With vertical-rl, the inline axis is vertical. Consequently, row follows a vertical axis instead of a horizontal one.
Important Consideration
Do not assume that row always means left to right or that column always means top to bottom. Their physical directions depend on the writing mode and text direction.
Alignment
Changing flex-direction also changes how the container's main-axis and cross-axis alignment properties behave.
Main-Axis Alignment
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
}
In a horizontal writing mode, justify-content distributes the items horizontally.
Column Alignment
.container {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 300px;
}
The same property now distributes the items vertically, provided there is positive free space along the main axis.
Cross-Axis Alignment
.container {
display: flex;
flex-direction: column;
align-items: center;
}
In a horizontal writing mode, align-items: center centers items horizontally because the cross axis is horizontal.
Alignment Properties
| Property | Purpose |
|---|---|
justify-content |
Aligns or distributes items along the main axis. |
align-items |
Controls the default cross-axis alignment of items. |
align-self |
Overrides cross-axis alignment for an individual item. |
align-content |
Distributes flex lines along the cross axis when applicable. |
Important Consideration
Alignment properties operate relative to the flex axes. Changing the flex direction can change the visible effect of existing alignment declarations without changing those declarations.
Flex Item Sizing
The selected flex direction determines the dimension used by the flex-basis, flex-grow, and flex-shrink properties.
Row Sizing
.container {
display: flex;
flex-direction: row;
}
.item {
flex: 1 1 200px;
}
In a horizontal writing mode, the items start with a horizontal flex basis of 200px before flexible sizing.
Column Sizing
.container {
display: flex;
flex-direction: column;
height: 500px;
}
.item {
flex: 1 1 100px;
}
The items start with a vertical flex basis of 100px and can grow or shrink along the container's height.
Sizing Comparison
| Property | Row Layout | Column Layout |
|---|---|---|
flex-basis |
Initial inline-axis main size | Initial block-axis main size |
flex-grow |
Distributes positive space along the inline axis | Distributes positive space along the block axis |
flex-shrink |
Handles negative space along the inline axis | Handles negative space along the block axis |
Important Consideration
A column flex container may need a definite height or another definite block size for flexible vertical growth to produce the intended result.
flex-flow Shorthand
The flex-flow shorthand combines flex-direction and flex-wrap into a single declaration.
Individual Properties
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
Equivalent Shorthand
.container {
display: flex;
flex-flow: row wrap;
}
Additional Examples
.container-one {
flex-flow: column nowrap;
}
.container-two {
flex-flow: row-reverse wrap;
}
.container-three {
flex-flow: column-reverse wrap;
}
Shorthand Components
| Property | Initial Value | Purpose |
|---|---|---|
flex-direction |
row |
Determines the main axis and direction. |
flex-wrap |
nowrap |
Controls whether items can wrap onto multiple lines. |
Important Consideration
A flex-flow declaration resets both component properties. If either value is omitted, its initial value is used.
Responsive Layouts
The flex-direction property is commonly used with media queries to change between horizontal and vertical layouts.
Horizontal Layout
.layout {
display: flex;
flex-direction: row;
gap: 20px;
}
.sidebar {
flex: 0 0 220px;
}
.main-content {
flex: 1;
min-width: 0;
}
Stacked Layout on Smaller Screens
@media (max-width: 700px) {
.layout {
flex-direction: column;
}
.sidebar {
flex: 0 1 auto;
}
.main-content {
flex: 1 1 auto;
}
}
At narrower viewport widths, the sidebar and main content stack vertically rather than sharing a horizontal row.
Responsive Navigation
.navigation {
display: flex;
flex-direction: row;
gap: 15px;
}
@media (max-width: 600px) {
.navigation {
flex-direction: column;
}
}
Important Consideration
When changing the direction, review any existing flex bases, minimum sizes, and alignment properties. Values intended for horizontal sizing may produce unexpected vertical sizing after switching to column.
flex-direction Example
This example demonstrates all four flex-direction values using the same three items. The numbered labels make the visual direction easy to compare.
HTML Example
<div class="direction-demo">
<h3>row</h3>
<div class="direction-row">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<h3>row-reverse</h3>
<div class="direction-row-reverse">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<h3>column</h3>
<div class="direction-column">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<h3>column-reverse</h3>
<div class="direction-column-reverse">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</div>
CSS Example
.direction-demo > div {
display: flex;
gap: 8px;
padding: 10px;
margin-bottom: 20px;
background-color: #f1f5f9;
}
.direction-demo > div > div {
display: flex;
align-items: center;
justify-content: center;
width: 60px;
min-height: 40px;
background-color: #336699;
color: #fff;
font-weight: bold;
}
.direction-demo .direction-row {
flex-direction: row;
}
.direction-demo .direction-row-reverse {
flex-direction: row-reverse;
}
.direction-demo .direction-column {
flex-direction: column;
}
.direction-demo .direction-column-reverse {
flex-direction: column-reverse;
}
row
row-reverse
column
column-reverse
In a left-to-right horizontal writing mode, row displays items from left to right, while row-reverse displays them from right to left. The column value stacks items from top to bottom, and column-reverse reverses that vertical arrangement.
All four layouts retain the same HTML source order. Only the visual direction changes.
Accessibility
The flex-direction property can change visual order without changing the order in which content appears in the document or is encountered by assistive technologies.
Preserve Meaningful Source Order
Write HTML in a logical reading sequence. Do not rely on reversed flex directions to correct an unsuitable document order.
Keyboard Navigation
Interactive elements generally receive keyboard focus according to document order, not their reversed visual positions. A reversed navigation row can therefore produce a confusing focus sequence.
Responsive Layout Changes
When switching from a row to a column, verify that the visual arrangement continues to match the intended reading and interaction sequence.
Text Resizing
Ensure that horizontal layouts can accommodate enlarged text. A responsive column layout may provide additional space for longer labels and controls.
Avoid Visual and Semantic Mismatches
Use reversed directions primarily for decorative or nonessential arrangements where the difference between visual and source order will not confuse users.
Important Consideration
CSS visual reordering does not replace semantic HTML structure. Meaningful reading and keyboard navigation order should be established in the document itself.
Important Considerations
Keep the following points in mind when using flex-direction:
- Purpose: Defines the main axis and direction of a flex container.
- Initial value:
row. - Inheritance: The property is not inherited.
- Applies to: Flex containers.
- row: Arranges items along the inline axis.
- row-reverse: Reverses the inline-axis main direction.
- column: Arranges items along the block axis.
- column-reverse: Reverses the block-axis main direction.
- Writing modes: Physical directions depend on
writing-modeanddirection. - Main axis: Determines the direction used by main-axis flex sizing and alignment.
- Cross axis: Runs perpendicular to the main axis.
- justify-content: Distributes items along the main axis.
- align-items: Controls cross-axis alignment.
- flex-basis: Sets the initial main size of flex items.
- flex-grow: Distributes positive free space along the main axis.
- flex-shrink: Handles negative free space along the main axis.
- Wrapping: Controlled separately by
flex-wrap. - Shorthand:
flex-flowcombines direction and wrapping. - Responsive layouts: Media queries can switch between row and column arrangements.
- Visual order: Reversed directions do not change HTML source order.
- Keyboard navigation: Focus order generally follows the document rather than the reversed visual layout.
- Animation: Changes between directional values are discrete.
- Accessibility: Keep visual and meaningful interaction sequences consistent.
Use flex-direction to select the appropriate main axis for a layout. Combine it with flex sizing, alignment, wrapping, and responsive rules to control how items are arranged at different viewport sizes.
Browser Support
The CSS flex-direction property is Widely available and supported by modern browsers, including Chrome, Edge, Firefox, and Safari.
All four standard directional values are broadly supported. Older browsers may have used prefixed or earlier Flexbox implementations.
Supported Example
.container {
display: flex;
flex-direction: column;
}
Feature Detection
@supports (display: flex) and (flex-direction: column) {
.container {
display: flex;
flex-direction: column;
}
}
Fallback Example
.container > div {
display: block;
}
@supports (display: flex) {
.container {
display: flex;
flex-direction: row;
}
}
The fallback leaves items in normal block layout when Flexbox is unsupported. Browsers that support Flexbox arrange the items in a row.
Compatibility Considerations
Although support is broad, test reversed layouts and writing modes when supporting right-to-left languages or vertical text.
For current compatibility information, visit Can I Use? .
Specifications
The flex-direction property is defined in the CSS Flexible Box Layout Module Level 1. The specification describes the main axis, directional values, and how writing modes influence flex item placement.
CSS Flexible Box Layout Module Level 1: flex-direction Property
The specification also defines the flex formatting model and the relationship between main and cross axes.
CSS Flexible Box Layout Module Level 1: Flex Layout Box Model
These definitions establish the behavior of row and column layouts, reversed directions, and alignment relative to the flex axes.
