CSS flex-direction: compare rows, columns, and focus order

CSS flex-direction sets the main axis and the direction in which flex items are laid out. In horizontal writing, use row for a horizontal row and column for a vertical stack. Apply the property to a parent with display: flex or inline-flex.

Distinguish four directions and two axes

Value Layout in horizontal left-to-right writing
row Left to right (default)
row-reverse Right to left
column Top to bottom
column-reverse Bottom to top

The main axis is the axis along which the items are placed. The cross axis is perpendicular to it. justify-content works on the main axis and align-items on the cross axis. In horizontal writing, changing from row to column changes justify-content: center from horizontal to vertical centering. See CSS Flexbox · flex-direction and CSS Flexbox · Alignment.

Try the same links in four directions

Save the code as flex-direction.html and open it. All four boxes contain links A, B, and C in the same HTML order. Only the parent’s direction changes. The example explicitly starts with left-to-right dir="ltr".

<!doctype html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare four flex directions</title>
  <style>
    body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
    * { box-sizing: border-box; }
    h1 { font-size: 24px; }
    h2 { margin: 14px 0 6px; font-size: 16px; }
    .samples { max-width: 560px; }
    .box { display: flex; gap: 8px; height: 132px; padding: 10px;
      border: 1px solid #88a4b6; background: #eef5f8; }
    .box a { display: grid; place-items: center; flex: none;
      width: 48px; height: 28px; color: white; background: #176b86;
      text-decoration: none; }
    .box a:focus-visible { outline: 3px solid #bc5b00; outline-offset: 2px; }
    .row { flex-direction: row; }
    .row-reverse { flex-direction: row-reverse; }
    .column { flex-direction: column; }
    .column-reverse { flex-direction: column-reverse; }
  </style>
</head>
<body>
<h1>Compare flex-direction values</h1>
<div class="samples">
<section><h2>row</h2>
<div class="box row">
  <a href="#result">A</a><a href="#result">B</a><a href="#result">C</a>
</div></section>
<section><h2>row-reverse</h2>
<div class="box row-reverse">
  <a href="#result">A</a><a href="#result">B</a><a href="#result">C</a>
</div></section>
<section><h2>column</h2>
<div class="box column">
  <a href="#result">A</a><a href="#result">B</a><a href="#result">C</a>
</div></section>
<section><h2>column-reverse</h2>
<div class="box column-reverse">
  <a href="#result">A</a><a href="#result">B</a><a href="#result">C</a>
</div></section>
</div>
<p id="result">All boxes use the HTML order A → B → C.</p>
</body>
</html>

In row, A starts on the left. In row-reverse, A starts on the right. In column, it starts at the top, and in column-reverse it starts at the bottom. Change the HTML to dir="rtl" and the two horizontal starting edges switch, while the vertical starting edges stay the same in horizontal writing. Restore ltr afterward. With a vertical writing-mode, row follows the vertical inline axis.

Switch a toolbar from row to column

For a toolbar that should stack on narrow screens, use a media query and let the height follow its content. In the first box, replace only the row class with toolbar and add the rules below at the end of style. Keep the box class, which sets display: flex.

.toolbar {
  flex-direction: row;
  height: auto;
}
@media (max-width: 640px) {
  .toolbar {
    flex-direction: column;
  }
}

At viewport widths of 640px or less the links stack vertically. At larger widths they form a row. Remove the new rules and restore row after comparing. If you need to keep the horizontal direction and let items flow onto more lines as space runs out, configure flex-wrap.

Compare the visual order with the Tab order

Tab into the second box. From left to right it displays C, B, A, while keyboard focus starts on A at the right and continues to B and C. The reverse value changes where elements are drawn. For steps or form fields where sequence matters, put them in the right order in the HTML. See CSS Flexbox · Reordering and Accessibility.