CSS propertyFlexbox

flex-direction

Sets the direction of the main axis: flex items line up in a row or in a column.

Initial value
row
Inherited
No
Animatable
no
Applies to
flex containers
JavaScript
element.style.flexDirection = "column"
On this page

Example

#
CSS
.cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

@media (min-width: 600px) {
  .cards { flex-direction: row; }
}
Preview

Definition and usage

#

The flex-direction property chooses the main axis of a flex container. With row items sit side by side horizontally, with column they stack one under another. The -reverse variants flip the display order.

The direction affects how other properties behave. justify-content always positions items along the main axis, and align-items along the cross axis. After switching to column, justify works vertically and align horizontally.

Reversing the order only changes the visual appearance. Screen readers and Tab-key navigation still follow the order in the code, so don’t use -reverse to order content that matters.

Syntax

#
Syntax
flex-direction: row | row-reverse | column | column-reverse;

Values

#
  • row

    Items in a row, in the text-writing direction.
  • row-reverse

    A row in reverse order, starting from the right edge.
  • column

    Items in a column, from top to bottom.
  • column-reverse

    A column in reverse order, starting from the bottom.

Values in practice

#

row

Items in a row, in the text-writing direction.
.demo { flex-direction: row; }

row-reverse

A row in reverse order, starting from the right edge.
.demo { flex-direction: row-reverse; }

column

Items in a column, from top to bottom.
.demo { flex-direction: column; }

column-reverse

A column in reverse order, starting from the bottom.
.demo { flex-direction: column-reverse; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • On phones it’s common to start with column and switch to row on wider screens inside a @media query.
  • Changing the direction swaps the roles of justify-content and align-items. That’s the most common source of surprise with Flexbox.

Related entries

#

Spotted a mistake or missing an example? Write to us.