CSS Grid order: auto-placement, fixed cells, and Tab order

In CSS Grid, order makes auto-placement process items from the smallest integer to the largest. Ties follow HTML order. An item with both its row and column specified keeps that cell regardless of its order value. order alone also leaves default Tab navigation unchanged.

What order changes during auto-placement

Set order on the grid item. The initial value is 0, so assigning 1 to one item puts it after the items that still use 0. The -1 in the snippet does the opposite. Fractional values and lengths are invalid. See CSS Grid · The order property.

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.featured {
  order: -1;
}

The example uses horizontal left-to-right writing, default row-wise auto-placement, and one cell per item. Items with definite row and column positions occupy their cells first, and auto-placed items skip those cells. Spanning multiple cells or changing grid-auto-flow can change how the remaining items fit. See CSS Grid · Grid item placement algorithm.

Compare automatically and explicitly placed items

Save and open the code as grid-order.html. No other files or JavaScript are needed. Both grids contain A, B, C, and D in the HTML, with order values 2, 4, 1, and 3. The second grid also assigns a row and column to every button.

<!doctype html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare Grid order and explicit placement</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: 16px 0 6px; font-size: 18px; }
    .grid { display: grid; grid-auto-flow: row; grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 8px; max-width: 420px; padding: 12px; border: 1px solid #7795a3; background: #edf5f7; }
    .item { min-width: 0; min-height: 48px; border: 0; padding: 8px;
      background: #176f80; color: white; font: inherit; }
    .item:focus-visible { outline: 3px solid #b75c00; outline-offset: 2px; }
    .a { order: 2; }
    .b { order: 4; }
    .c { order: 1; }
    .d { order: 3; }
    .fixed .a { grid-row: 1; grid-column: 1; }
    .fixed .b { grid-row: 1; grid-column: 2; }
    .fixed .c { grid-row: 2; grid-column: 1; }
    .fixed .d { grid-row: 2; grid-column: 2; }
  </style>
</head>
<body>
<h1>Compare Grid order</h1>
<h2 id="grid-automatic">Automatic placement</h2>
<div class="grid automatic" role="group" aria-labelledby="grid-automatic">
  <button class="item a" type="button">A</button>
  <button class="item b" type="button">B</button>
  <button class="item c" type="button">C</button>
  <button class="item d" type="button">D</button>
</div>
<h2 id="grid-fixed">Explicit row and column positions</h2>
<div class="grid fixed" role="group" aria-labelledby="grid-fixed">
  <button class="item a" type="button">A</button>
  <button class="item b" type="button">B</button>
  <button class="item c" type="button">C</button>
  <button class="item d" type="button">D</button>
</div>
<p>Both grids have the same order values. Tab follows A, B, C, D in the HTML. The buttons are for comparison only.</p>
</body>
</html>

Reading left to right, the first grid puts C and A above D and B. The second shows A and B above C and D because their cells are explicit. Add the first rule below to the end of style to clear those positions and make the second grid match the first.

.fixed .item {
  grid-area: auto;
}

Then append the second rule to pin B alone in row 1, column 2. Even though B has the highest order, its cell is occupied before automatic placement. The empty cells receive C, A, and D in that order, producing C and B above A and D. Remove both added rules to restore the example.

.fixed .b {
  grid-row: 1;
  grid-column: 2;
}

Test negative values, ties, and overlapping items

In the original example, replace 2, 4, 1, and 3 with the declarations below. D has -1 and the others have 0, so the auto-placed grid shows D and A above B and C. The second grid keeps its assigned cells.

.a, .b, .c { order: 0; }
.d { order: -1; }

Restore the original values and change only B from 4 to 2. The top row becomes C and A, and the bottom row B and D. A precedes B because tied items follow the HTML order. If you overlap items in one area, order can also affect painting order. Check z-index to control that stacking explicitly. See CSS Grid · Z-axis ordering.

Follow default Tab order and diagnose placement

With default Tab navigation, both grids follow A, B, C, and D. In the automatic grid, that means top right, bottom right, top left, and bottom left. The orange outline identifies focus. These buttons have no other action.

Put forms, instructions, and other ordered content in a logical HTML sequence. A separate property, reading-flow, can change sequential navigation where supported, but this example leaves it unset. Keep the source order meaningful even without CSS. See CSS Display · Reading Flow.

If the result differs, check that the target actually participates as a grid item and inspect its computed order. Then see which cells are claimed by grid-area, grid-row, or grid-column. Separating occupied cells from the items left for auto-placement makes the result easier to predict.