CSS flex-direction Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Flexbox

What You’ll Learn

The flex-direction property controls which way flex items flow inside a container — horizontally in a row or vertically in a column, including reversed directions.

01

Main axis

Row or column.

02

Default row

Left to right.

03

column

Stack vertically.

04

Reverse values

Flip the flow.

05

Container only

On flex parent.

06

Responsive

Switch on mobile.

Introduction

The flex-direction property in CSS is a crucial part of the Flexible Box Layout Module, commonly known as Flexbox. This property specifies the direction in which the flex container’s children (the flex items) are placed. It allows developers to control the flow of items in a container, making it easier to design responsive and flexible layouts.

Definition and Usage

Apply flex-direction on a flex container (an element with display: flex or display: inline-flex). Use row for horizontal toolbars and nav bars, column for stacked cards or mobile menus, and reverse values when you need the visual order flipped without changing HTML source order.

💡
Beginner Tip

flex-direction defines the main axis. Properties like justify-content align items along this axis, while align-items works on the cross axis.

📝 Syntax

The syntax for the flex-direction property is as follows:

syntax.css
container {
  flex-direction: direction;
}

Here, direction can be one of the following values:

  • row
  • row-reverse
  • column
  • column-reverse

Basic Example

flex-direction-row.css
.container {
  display: flex;
  flex-direction: row;
}
flex-direction: row; flex-direction: column; flex-direction: row-reverse; flex-direction: column-reverse;

Default Value

The default value of the flex-direction property is row, which means the flex items are laid out in a horizontal direction, from left to right.

Syntax Rules

  • Apply on the flex container, not on individual flex items.
  • Requires display: flex or display: inline-flex on the same element.
  • Changing direction swaps the main and cross axes for alignment properties.
  • Reverse values flip visual order but keep the DOM source order for accessibility.
  • The property is not inherited by child elements.

⚡ Quick Reference

QuestionAnswer
Initial valuerow
Applies toFlex containers
InheritedNo
AnimatableNo
Common mobile patternflex-direction: column on small screens

💎 Property Values

ValueDescription
rowLays out the flex items horizontally, from left to right.
row-reverseLays out the flex items horizontally, from right to left.
columnLays out the flex items vertically, from top to bottom.
column-reverseLays out the flex items vertically, from bottom to top.

Main Axis vs Cross Axis

flex-directionMain axisCross axis
row / row-reverseHorizontalVertical
column / column-reverseVerticalHorizontal

👀 Live Preview

The same three items with each flex-direction value:

row
123
column
123
row-reverse
123
column-reverse
123

Examples Gallery

In this example, we’ll use the flex-direction property to arrange items in a row — plus column stacking, reversed row order, and a responsive layout that switches direction on mobile.

↔ Row & Column Directions

Start with the reference example — a horizontal row of flex items.

Example 1 — Flex Direction: Row

Arrange three items horizontally in the default left-to-right direction.

flex-direction-row.html
<style>
  .container {
    display: flex;
    flex-direction: row;
    background-color: lightgray;
  }
  .item {
    padding: 20px;
    background-color: steelblue;
    margin: 10px;
    color: white;
  }
</style>

<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
</div>
Try It Yourself

How It Works

flex-direction: row is the default. Items sit side by side along the horizontal main axis.

Example 2 — Flex Direction: Column

Stack navigation links or cards vertically with flex-direction: column.

flex-direction-column.css
.nav {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
Try It Yourself

How It Works

With column, the main axis runs vertically. Items stack from top to bottom.

🔄 Reverse & Responsive Layouts

Flip item order visually or change direction at breakpoints.

Example 3 — Flex Direction: row-reverse

Display items from right to left while keeping the same HTML order.

flex-direction-row-reverse.css
.toolbar {
  display: flex;
  flex-direction: row-reverse;
  gap: 0.75rem;
}
Try It Yourself

How It Works

The first item in HTML appears on the right. Tab order still follows the DOM, so use reverse carefully for interactive content.

Example 4 — Responsive Row to Column

Switch from a horizontal row on desktop to a vertical column on small screens.

flex-direction-responsive.css
.cards {
  display: flex;
  flex-direction: row;
  gap: 1rem;
}

@media (max-width: 600px) {
  .cards {
    flex-direction: column;
  }
}
Try It Yourself

How It Works

A media query changes only the direction. The same HTML works for both horizontal and stacked layouts.

♿ Accessibility

  • Reverse values change visual order, not tab order — keyboard focus still follows DOM order.
  • Prefer logical source order — do not rely on row-reverse alone to fix confusing navigation sequences.
  • Use semantic HTML — wrap nav links in <nav> regardless of direction.
  • Test with keyboard and screen readers when using reverse directions on interactive lists.
  • Consider RTL languages — writing mode and direction affect how row vs row-reverse appear.

🧠 How flex-direction Works

1

Container becomes flex

display: flex activates Flexbox on the parent element.

Setup
2

flex-direction sets the main axis

Row = horizontal flow. Column = vertical flow. Reverse flips the start and end.

Direction
3

Items flow along the main axis

Flex items are placed in source order (or reversed) along that axis.

Layout
=

Controlled item flow

Switch between horizontal and vertical layouts with one property.

🖥 Browser Compatibility

The flex-direction property is well-supported in all modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. It is safe to use for production websites, but it’s always a good practice to test your layouts across different browsers to ensure compatibility.

Baseline · Universal support

flex-direction everywhere

All four direction values work in every modern browser. Flexbox is a core layout feature on the web today.

99% Universal support
Google Chrome 29+ · Desktop & Mobile
Full support
Mozilla Firefox 28+ · Desktop & Mobile
Full support
Apple Safari 9+ · macOS & iOS
Full support
Microsoft Edge 12+ · All versions
Full support
Opera 17+ · Modern versions
Full support
flex-direction property 99% supported

Bottom line: All four flex-direction values are safe for production. Test responsive direction changes on real mobile devices.

🎉 Conclusion

The flex-direction property is a fundamental aspect of Flexbox, offering a simple way to control the layout direction of items within a flex container.

By understanding and utilizing this property, you can create responsive and flexible designs that adapt to various screen sizes and orientations. Experiment with different values to see how they affect the layout of your flex items and enhance your web design projects.

💡 Best Practices

✅ Do

  • Use row for horizontal nav bars and toolbars
  • Use column for stacked mobile menus and card lists
  • Switch to column in media queries for responsive layouts
  • Pair with gap for consistent spacing between items
  • Remember that direction defines the main axis for justify-content

❌ Don’t

  • Apply flex-direction on flex items instead of the container
  • Use row-reverse on forms or keyboard-heavy UI without testing focus order
  • Forget display: flex on the parent
  • Assume column makes items full width automatically — set widths if needed
  • Rely on reverse values when reordering HTML is clearer for accessibility

Key Takeaways

Knowledge Unlocked

Five things to remember about flex-direction

Use these points when building Flexbox layouts.

5
Core concepts
02

Default row

Horizontal flow.

Default
03

column

Vertical stack.

Common
🔄 04

Reverse

Flip visual order.

Advanced
📱 05

Responsive

Switch at breakpoints.

Pattern

❓ Frequently Asked Questions

The flex-direction property sets the main axis of a flex container. It controls whether flex items are laid out horizontally (row) or vertically (column), and whether the direction is normal or reversed.
The initial value is row, which lays out flex items horizontally from left to right in left-to-right languages.
row places items side by side along a horizontal main axis. column stacks items vertically along a vertical main axis.
They flip the main axis direction. row-reverse goes right to left; column-reverse goes bottom to top, while still following the flex container's writing mode.
Yes. flex-direction defines the main axis, so flex-grow, flex-shrink, and flex-basis apply along that axis. In a column container, flex-basis sets height instead of width.

Practice in the Live Editor

Open the HTML editor, set flex-direction: row and column, and compare how items flow in each direction.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful