Master CSS From Scratch

Clear, interactive, and structured CSS lessons designed for beginners.

CSS Flex Direction

The CSS flex-direction property defines the direction in which Flex Items are placed inside a Flex Container.

By default, Flexbox places items horizontally from left to right. You can change this behavior using flex-direction.

Syntax

.container {
    display: flex;
    flex-direction: value;
}

The value determines the direction in which the Flex Items are arranged.

Values of flex-direction

Value Description
row Places items horizontally from left to right.
row-reverse Places items horizontally from right to left.
column Places items vertically from top to bottom.
column-reverse Places items vertically from bottom to top.

Flex Direction Diagram

row

1
2
3

Items move horizontally from left to right.

column

1
2
3

Items move vertically from top to bottom.

1. flex-direction: row

The row value is the default value of flex-direction.

It places Flex Items horizontally from left to right.

.container {
    display: flex;
    flex-direction: row;
}

Output

HTML
CSS
JavaScript

2. flex-direction: row-reverse

The row-reverse value places items horizontally in the reverse direction.

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

Output

HTML
CSS
JavaScript

3. flex-direction: column

The column value places Flex Items vertically from top to bottom.

.container {
    display: flex;
    flex-direction: column;
}

Output

HTML
CSS
JavaScript

4. flex-direction: column-reverse

The column-reverse value places Flex Items vertically in reverse order, from bottom to top.

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

Output

HTML
CSS
JavaScript

Comparing the Four Values

row

Items are arranged horizontally from left to right.

row-reverse

Items are arranged horizontally in reverse direction.

column

Items are arranged vertically from top to bottom.

column-reverse

Items are arranged vertically in reverse direction.

Real-World Example

Imagine a CIIT Training Institute page containing course cards.

If the cards need to appear side by side, use row. If they need to appear one below another, use column.

.courses {
    display: flex;
    flex-direction: row;
}

With row, course cards can be displayed horizontally.

Changing the value to column changes the layout to a vertical arrangement.

Important: The default value of flex-direction is row. The direction of the Flex Items affects the main axis of the Flex Container. Properties such as justify-content and align-items work relative to these axes.

Summary

The flex-direction property controls the direction of Flex Items inside a Flex Container. Its four main values are row, row-reverse, column, and column-reverse. The default value is row.