Flex Direction – Row, Column and Reverse
Quick Answer: Use flex-direction on a flex container to choose its main axis and the direction in which items are placed. The four values are row (the default), row-reverse, column and column-reverse. Row places items horizontally, column places them vertically, and the reverse values flip the start and end of the axis. Reversing changes the visual order only, not the order in the HTML.
S1. Why Do We Need Flex Direction?
Imagine you are building a learning dashboard. It has a top navigation bar, a sidebar menu and a feature card with an image.
The navigation links should sit side by side. The sidebar links should stack from top to bottom. The feature card should show its image on the right, even though the image comes first in the HTML.
Would you use a different layout technique for each one? In the previous article we made a parent a flex container, and its items sat in a row. A row is not always what we want.
CSS Flexbox solves this with one property on the parent. Let's first see what happens when we leave the direction alone.
S2. The Problem: A Flex Container Stuck in the Default Direction
Main axis = the line along which a flex container places its items. By default, it is horizontal.
Imagine a narrow sidebar that holds three menu links. The developer makes the sidebar a flex container, because that is how we arrange items now. No direction is set.
Example 1: A sidebar menu with the default direction
Assumption: Exact widths depend on the font and browser, so the output below is approximate.
<!-- HTML -->
<nav class="sidebar">
<div class="side-link">Dashboard</div>
<div class="side-link">Courses</div>
<div class="side-link">Profile</div>
</nav>
/* CSS */
.sidebar {
display: flex;
width: 220px;
}
.side-link {
padding: 12px 20px;
background: #E2E8F0;
}
Expected output
Sidebar (220px wide): [ Dashboard ][ Courses ][ Profile ] The items sit side by side in one row. The row is wider than the 220px sidebar, so the last link can spill outside it.
The page renders, but it is not the sidebar we wanted. A flex container places its items in a row unless we say otherwise.
What goes wrong?
- Wrong direction: A menu that should read from top to bottom runs from left to right.
- Cramped or overflowing items: A row of links can be wider than a narrow container.
- Abandoned Flexbox: Developers fall back to block layout or extra wrapper elements for that one menu.
- Edited HTML order: To show the last item first, developers rewrite the HTML order, which changes the document itself.
- Poor clarity: The CSS does not say which way the items should flow.
Now think about many menus, forms and card rows on one website. Each one may need a different direction, and every workaround adds more to maintain.
Remember: The container is not broken. It is using its default main axis. The real problem is that nobody told it which axis to use.
Animation 1: How the default direction causes problems
Three links, one default direction
The red highlights show items squeezed into a row they were never meant for.
The animation highlights the container with no direction and the items it squeezes into a row.
S3. The Solution: Choose the Direction with flex-direction
Flexbox gives us a simpler design.
flex-direction = a property on the flex container that sets the main axis and the direction in which items are placed.
Instead of working around the row, tell the container which way to flow. It accepts four values.
rowplaces items along the horizontal axis. This is the default.row-reverseplaces items along the horizontal axis, starting from the opposite end.columnplaces items along the vertical axis, from top to bottom.column-reverseplaces items along the vertical axis, starting from the bottom.
Now the same three links stack as a sidebar should.
Example 2: A column sidebar
Assumption: This example uses standard CSS supported by all modern browsers, and assumes a left-to-right language.
.sidebar {
display: flex;
flex-direction: column;
width: 220px;
}
.side-link {
padding: 12px 20px;
background: #E2E8F0;
}
Expected output
[ Dashboard ] [ Courses ] [ Profile ] The items stack from top to bottom. They stretch to the width of the sidebar.
Notice the difference. The HTML stays the same, and one property on the parent changes the direction of every item.
Animation 2: The flex-direction design
One property, one axis
The container sets the main axis, and its items follow it.
The animation highlights each item and the container rules that set its direction.
Compare both approaches
| Without flex-direction | With flex-direction |
|---|---|
| Items always follow the default row. | You choose row, row-reverse, column or column-reverse. |
| A vertical menu needs block layout or extra wrappers. | A vertical menu is one declaration: column. |
| Flipping the visual order means editing the HTML order. | The reverse values flip the visual order in CSS. |
| Layout intent is hidden. | Layout intent is clear. |
| Changing direction on small screens means a new layout. | One property changes inside a media query. |
The idea to remember
- flex-direction = a property set on the flex container, not on the items.
- Main axis = the line along which items are placed.
rowandcolumnchoose it. - Cross axis = the line perpendicular to the main axis.
- Default =
row. - Reverse values = flip the start and end of the axis. They change the visual order only.
S4. Step-by-Step Code Breakdown
1. Start with a flex container
flex-direction only works on a flex container. Add display: flex or display: inline-flex to the parent first. Without it, the property has no effect.
2. Know the four values
Each value sets the main axis and where it starts. The table assumes a left-to-right language.
| Value | Main axis runs | Items start at | Typical use |
|---|---|---|---|
row |
Left to right | The left edge | Navigation bars and toolbars |
row-reverse |
Right to left | The right edge | A card with its image on the right |
column |
Top to bottom | The top edge | Sidebars and forms |
column-reverse |
Bottom to top | The bottom edge | Lists anchored to the bottom |
3. Compare all four values
The same three items behave differently under each value. Each container below has the same children. Only the direction changes.
Example 3: All four directions side by side
<!-- HTML (four containers with the same children) -->
<div class="demo row"><span>A</span><span>B</span><span>C</span></div>
<div class="demo row-reverse"><span>A</span><span>B</span><span>C</span></div>
<div class="demo column"><span>A</span><span>B</span><span>C</span></div>
<div class="demo column-reverse"><span>A</span><span>B</span><span>C</span></div>
/* CSS */
.demo {
display: flex;
gap: 8px;
}
.row { flex-direction: row; }
.row-reverse { flex-direction: row-reverse; }
.column { flex-direction: column; }
.column-reverse { flex-direction: column-reverse; }
.column,
.column-reverse { height: 140px; }
Expected output
row:
[ A ][ B ][ C ]
row-reverse:
[ C ][ B ][ A ]
column:
[ A ]
[ B ]
[ C ]
column-reverse (140px tall container):
[ C ]
[ B ]
[ A ]
In row-reverse, the first item (A) starts at the right edge. In column-reverse, the first item (A) starts at the bottom. That is why the container height matters in the last case. With automatic height, the container is only as tall as its content and you would not see the items sit at the bottom.
4. Understand how the axes swap
The properties justify-content and align-items do not mean "horizontal" and "vertical". justify-content works along the main axis, and align-items works along the cross axis. When you change the direction, they change with it.
Example 4: Centering content in a column
<!-- HTML -->
<div class="panel">
<h3>Welcome</h3>
<button>Start Learning</button>
</div>
/* CSS */
.panel {
display: flex;
flex-direction: column;
justify-content: center; /* main axis: vertical */
align-items: center; /* cross axis: horizontal */
height: 200px;
}
Expected output
+--------------------------+ | | | Welcome | | [ Start Learning ] | | | +--------------------------+ justify-content centers the items vertically (main axis). align-items centers them horizontally (cross axis).
5. Remember that reverse changes the visual order only
The reverse values change where items are drawn. They do not change the order of the elements in the HTML. Keyboard focus and screen readers follow the HTML order, so a reversed layout can feel out of sync.
Example 5: A reversed navigation row
<!-- HTML -->
<nav class="menu">
<a href="#">Home</a>
<a href="#">Courses</a>
<a href="#">Contact</a>
</nav>
/* CSS */
.menu {
display: flex;
flex-direction: row-reverse;
gap: 12px;
}
Expected output
Visual order (left to right): Contact Courses Home HTML order: Home Courses Contact Keyboard Tab order: Home -> Courses -> Contact The links are drawn in reverse, but the HTML is unchanged. Tabbing follows the HTML order, so focus moves from right to left on screen.
If the reading order matters for the content, change the HTML order instead of reversing it with CSS.
6. Change direction at different screen sizes
Because the direction is one property, a media query can change it. A row on wide screens can become a column on small ones.
Example 6: Row on desktop, column on mobile
.menu {
display: flex;
flex-direction: row;
gap: 12px;
}
@media (max-width: 600px) {
.menu {
flex-direction: column;
}
}
Expected output
Viewport wider than 600px: [ Home ][ Courses ][ Contact ] Viewport 600px or narrower: [ Home ] [ Courses ] [ Contact ]
S5. Architecture and Real-World Trade-Offs
The direction is chosen per container, so each part of a page can flow its own way. This is useful in navigation bars, sidebars, forms and cards.
How the design fits a website
Imagine a website with a top navigation bar, a feature card and a sidebar. Each one has its own parent with its own direction.
A focused direction flow
Each website area chooses the direction that suits its content.
The animation traces each website area to its own flex-direction value.
Benefits for larger websites
- Maintainability: The direction lives on the parent, so changes happen in one place.
- Responsiveness: One property can change inside a media query.
- Less markup: You do not need extra wrapper elements to stack items.
- Clear ownership: The container decides how its direct children flow.
- Axis-aware alignment:
justify-contentandalign-itemsfollow the direction you choose.
Important trade-offs
- The reverse values change the visual order only. Keyboard focus and screen readers still follow the HTML order.
- In a reversed direction,
flex-startandflex-endswap sides, which can surprise you. - In a column,
justify-contentneeds extra height in the container before it shows any effect. rowfollows the writing direction, so in a right-to-left language it starts on the right.- Many reversed containers on one page make the code harder to read.
Important: flex-direction decides how items flow, not how they are aligned. Choose the direction first, then use justify-content and align-items to position items along the axes.
S6. Common Mistakes and Best Practices
Mistake 1: Using flex-direction without display: flex
Wrong: Add flex-direction: column to a normal block element.
Why it fails: The property only affects flex containers, so it does nothing here.
Better: Add display: flex to the parent first.
Rule: Create the container before using any flex property.
Mistake 2: Setting flex-direction on the items
Wrong: Put flex-direction: column on each menu link to stack them.
Why it fails: The property belongs on the container. On the links it only affects their own children, and only if they are flex containers themselves.
Better: Set flex-direction: column on the parent that holds the links.
Rule: The container sets the direction. The items follow it.
Mistake 3: Using a reverse value to reorder meaningful content
Wrong: Use row-reverse or column-reverse to put steps or headings in a new reading order.
Why it fails: The HTML order does not change. Keyboard users and screen readers still move through the original order, which no longer matches what they see.
Better: Change the order of the elements in the HTML when the reading order matters.
Rule: Use reverse values for visual design, not for reordering content.
Mistake 4: Forgetting that the axes swap
Wrong: Keep using justify-content to center items horizontally after switching to column.
Why it fails: In a column, justify-content works along the vertical main axis, so the horizontal result changes.
Better: Use align-items for the horizontal direction in a column.
Rule: Think in main axis and cross axis, not horizontal and vertical.
Mistake 5: Expecting vertical spacing in a column with automatic height
Wrong: Use justify-content: center or column-reverse to move items down or to the middle, but leave the container height automatic.
Why it fails: The container is only as tall as its content, so there is no free space to distribute along the main axis.
Better: Give the container a height or min-height when you want vertical positioning.
Rule: Free space must exist before it can be distributed.
S7. How This Comes Up in Interviews
Question 1: What is the default value of flex-direction?
What the interviewer wants: They want to know whether you understand the default behaviour of a flex container.
Strong answer:
"The default is row. The items are placed along the main axis, which runs in the inline direction. That is left to right in a left-to-right language."
Question 2: What are the main axis and the cross axis?
Strong answer:
"The main axis is the line along which flex items are placed, and flex-direction sets it. The cross axis is perpendicular to it. justify-content works on the main axis, and align-items works on the cross axis, so both follow the direction I choose."
Question 3: What is the difference between row-reverse and reversing the HTML order?
Strong answer:
"row-reverse changes only the visual order. The HTML order stays the same, so keyboard focus and screen readers still follow the original order. If the reading order matters, I change the HTML instead."
Question 4: How would you stack a menu on small screens and keep it in a row on large screens?
Strong answer:
"I make the menu a flex container with flex-direction: row, then use a media query to set flex-direction: column below a chosen width. Only one property changes."
What should different experience levels explain?
- Junior developer: Explain the four values and show a row and a column.
- Senior developer: Explain the main and cross axes, how
justify-contentandalign-itemsfollow the direction, and the accessibility cost of reverse values. - Technical architect: Explain how direction conventions and responsive rules keep layouts consistent across a design system.
S8. Hands-On Practice
Beginner: Switch a row to a column
Create a flex container with three child elements. Leave the direction alone first, then add flex-direction: column and observe the result.
Expected result: The items first sit in a row, then stack from top to bottom.
Intermediate: Compare all four values
Create four containers with the same three children labelled A, B and C. Give each one a different flex-direction value. Give the column containers a fixed height.
Hint: Watch where item A appears in each container.
Advanced: Build a responsive card
Create a card with an image and a text block. Make the card a flex container with row-reverse so the image appears on the right. Use a media query to switch to column on small screens. Then press the Tab key through any links inside it.
Expected result: On wide screens the image is drawn on the right. On small screens the items stack. Tab order follows the HTML order.
S9. Next Steps and Community
Previous article: Display Flex – Creating a Flex Container.
Next article: Flex Wrap and Flex Flow – Managing Multiple Rows.
Continue the series by learning how flex-wrap lets items move onto new lines when a row runs out of space, and how the flex-flow shorthand sets direction and wrapping together.
Try the examples in your own HTML and CSS project. Switch between all four directions, resize the browser to test your media queries, and share your layouts with the Upgradix community. Help other developers understand Flexbox through practical examples.
Choose the axis first. Then the items follow.
Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.