Item Placement
By default, grid items are auto-placed. But you can explicitly place any item on specific grid lines using grid-column and grid-row properties.
1. grid-column-start & grid-column-end
Place an item by telling it where it starts and ends using column line numbers. Lines are numbered from 1 (left) to n+1 (right).
.item-1 {
grid-column-start: 1;
grid-column-end: 3; /* occupies col lines 1β3 */
}
/* item spans from line 1 to line 3 = 2 columns wide */2. grid-column Shorthand
The shorthand grid-column: start / end combines both properties. Use a slash to separate start from end.
.a { grid-column: 1 / 3; } /* line 1 to line 3 */
.b { grid-column: 3 / 5; } /* line 3 to line 5 */
.c { grid-column: 2 / 4; } /* line 2 to line 4 */3. grid-row Placement
grid-row works exactly like grid-column but for rows. You can control both axes independently to place items anywhere in the grid.
.a {
grid-column: 1; grid-row: 1 / 3;
}
.b {
grid-column: 2 / 4; grid-row: 1;
}
.e {
grid-column: 1 / 4; grid-row: 3;
}4. span Keyword
Instead of specifying the end line, use span N to say "this item should span N tracks". span 2 means "take up 2 columns/rows from wherever you start".
.a { grid-column: span 2; } /* 2 columns wide */
.b { grid-column: span 2; } /* 2 columns wide */
.c { grid-column: span 3; } /* 3 columns wide */
.d { grid-column: span 4; } /* full width */
/* equivalent: */
.a { grid-column: 1 / span 2; } /* start at 1, span 2 */5. Negative Line Numbers
Negative line numbers count from the right (or bottom for rows). Line -1 is always the last line. This is great for placing items that should always span to the end.
/* -1 = last line, -2 = second-to-last, etc. */ /* Span full width regardless of column count: */ .full { grid-column: 1 / -1; } /* Last column: */ .last { grid-column: -2 / -1; } /* From second to last: */ .rest { grid-column: 2 / -1; }
6. Overlapping Items
Multiple items can occupy the same grid cells. They overlap like absolutely positioned elements. Use z-index to control stacking order.
.bg { grid-column: 1 / 3; grid-row: 1 / 3; z-index: 0; }
.over { grid-column: 2 / 4; grid-row: 1 / 3; z-index: 1; }
.top { grid-column: 2; grid-row: 1; z-index: 2; }