This property defines the direction of the main axis, which is the primary axis along which flex items are laid out. It allows you to control whether flex items are arranged in a row (horizontally) or a column (vertically), and in which direction.
Example 1: Default Row Direction
HTML
<div class="flex-container row-direction">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
/* Arranges items from left to right in a row. */
.row-direction {
flex-direction: row; /* This is the default value. */
}
Explanation
flex-direction: row
arranges the flex items horizontally from left to right. This is the default behavior of a flex container, so explicitly setting it is often unnecessary unless overriding another style.
Example 2: Row Reverse Direction
HTML
<div class="flex-container row-reverse-direction">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
/* Arranges items from right to left in a row. */
.row-reverse-direction {
flex-direction: row-reverse; /* The items are laid out in reverse order. */
}
Explanation
flex-direction: row-reverse
lays out the flex items horizontally but in the opposite direction, from right to left. The first item in the HTML source appears last in the rendered layout.
Example 3: Column Direction
HTML
<div class="flex-container column-direction">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
height: 250px; /* Giving the container height to visualize the column. */
}
/* Stacks items vertically from top to bottom. */
.column-direction {
flex-direction: column; /* Changes the main axis to vertical. */
}
Explanation
With flex-direction: column
, the main axis is switched from horizontal to vertical. Flex items are now stacked on top of each other, from top to bottom.
Example 4: Column Reverse Direction
HTML
<div class="flex-container column-reverse-direction">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
height: 250px;
}
/* Stacks items vertically from bottom to top. */
.column-reverse-direction {
flex-direction: column-reverse; /* Lays out items from bottom to top. */
}
Explanation
flex-direction: column-reverse
also stacks the flex items vertically, but it reverses the order. The layout starts from the bottom of the container and goes up.
Example 5: Responsive Direction Change
HTML
<div class="responsive-container">
<div class="item">Sidebar</div>
<div class="item">Main Content</div>
</div>
CSS
.responsive-container {
display: flex;
flex-direction: column; /* Start with a column layout for mobile. */
border: 2px solid #4CAF50;
}
/* On larger screens, switch to a row layout. */
@media (min-width: 768px) {
.responsive-container {
flex-direction: row; /* Side-by-side layout for tablets and desktops. */
}
}
Explanation
This demonstrates a common responsive design pattern. The layout is a single column on small screens and switches to a horizontal row on screens wider than 768px by changing the flex-direction
.
Example 6: Reversing Chat Message Order
HTML
<div class="chat-window">
<div class="message">Hello!</div>
<div class="message">How are you?</div>
<div class="message">This is the latest message.</div>
</div>
CSS
.chat-window {
display: flex;
flex-direction: column-reverse; /* New messages appear at the bottom. */
border: 2px solid #337ab7;
height: 200px;
overflow-y: auto; /* Allow scrolling if messages overflow. */
}
.message {
padding: 10px;
margin: 5px;
background-color: #f0f8ff;
border-radius: 5px;
}
Explanation
flex-direction: column-reverse
is perfect for chat interfaces. It keeps the container scrolled to the bottom where new messages appear, while older messages are pushed upwards.
Example 7: Alternating Content Flow
HTML
<section class="alternating-layout">
<div class="content-block">
<div class="text">Text First</div>
<div class="image">Image Second</div>
</div>
<div class="content-block reverse">
<div class="text">Text Second</div>
<div class="image">Image First</div>
</div>
</section>
CSS
.content-block {
display: flex;
align-items: center;
margin-bottom: 20px;
}
/* The second content block has its direction reversed. */
.content-block.reverse {
flex-direction: row-reverse; /* Flips the order of the image and text. */
}
Explanation
By applying a class that sets flex-direction: row-reverse
, you can easily create an alternating layout where the order of elements like images and text is swapped in subsequent sections.