Grid Placement and Alignment
W3C CSS Snapshot 2026
Place items by grid line, control extra tracks, and align items inside their cells. Start with placement before trying subgrid.
On this page
Count tracks, cells and grid lines
A track is the space between two neighboring lines. A cell is the intersection of one row and one column. Three columns have four boundary lines. grid-column and grid-row each accept a start and an end. span 2 covers two tracks. Negative line numbers count back from the end of the explicit grid.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Count tracks, cells and grid lines</title>
<style>
.layout { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.layout > :first-child { grid-column: 1 / span 2; }
.layout > :last-child { grid-row: 2; grid-column: 1 / -1; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<div class="layout">
<div>Pen</div>
<div>Notebook</div>
<div>Folder</div>
</div>
</body>
</html>
What to expect Pen covers columns 1 and 2. Notebook takes column 3. Folder spans the next row. Turn on the Grid overlay in browser DevTools to see the line numbers.
Size rows that the browser adds
grid-template-rows and grid-template-columns define explicit tracks. When an item needs another row, the browser creates an implicit track. grid-auto-rows sets its size. With column-based placement, grid-auto-columns sizes additional columns. gap with two values sets row-gap first and column-gap second.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Size rows that the browser adds</title>
<style>
.layout { display: grid; grid-template-columns: repeat(2, 1fr);
grid-template-rows: 32px; grid-auto-rows: minmax(64px, auto); gap: 12px 6px; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<div class="layout">
<div>Pen</div>
<div>Notebook</div>
<div>Folder</div>
</div>
</body>
</html>
What to expect The first row is 32px high. Folder starts a new row at least 64px high. Add a fourth and a fifth item to see which row sizes repeat.
Name lines and size tracks around content
Names in square brackets label lines. max-content fits a label without soft wrapping. min-content uses its narrowest intrinsic width, and fit-content(10rem) allows growth toward the content width up to a limit, subject to the minimum. minmax(0, 1fr) lets the flexible track shrink below its automatic minimum.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Name lines and size tracks around content</title>
<style>
.layout { display: grid; gap: 6px;
grid-template-columns: [start] max-content [details] minmax(0, 1fr) [end]; }
small { grid-column: start / end; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<div class="layout"><strong>Status</strong><span>Ready for review</span><small>Note for the editor</small></div>
</body>
</html>
What to expect Status uses just enough width for its label. The description fills the rest. The note spans both columns using their named outside lines.
Align the tracks, then align items inside cells
In the usual horizontal writing mode, justify works across columns and align works down rows. place-content combines align-content and justify-content to position the tracks in spare container space. place-items sets align-items and justify-items within every cell. place-self overrides those two settings for an individual item.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Align the tracks, then align items inside cells</title>
<style>
.layout { display: grid; min-height: 150px; gap: 6px;
grid-template-columns: repeat(2, 86px); grid-auto-rows: 50px;
place-content: center; place-items: center; }
.layout > :first-child { place-self: start; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<div class="layout">
<div>Pen</div>
<div>Notebook</div>
<div>Folder</div>
</div>
</body>
</html>
What to expect The grid sits in the center of the dashed frame. Pen sits at the start of its cell while the others sit in the middle. Change place-items to stretch to see the full cells.
Follow the automatic placement order
grid-auto-flow: row fills each row before moving down. column fills a column first and works well with a set of explicit rows. The dense option lets later items fill earlier holes. That can separate visual order from reading and keyboard order, so use a meaningful HTML order.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Follow the automatic placement order</title>
<style>
.layout { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; grid-auto-flow: row; }
.layout > :nth-child(-n+2) { grid-column: span 2; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<div class="layout">
<div>Pen</div>
<div>Notebook</div>
<div>Folder</div>
</div>
</body>
</html>
What to expect The two wide items leave a hole at the end of row one. Change row to row dense. Folder moves up to fill that hole.
Layer items in the same grid area
Multiple items can occupy the same cell. The four values of grid-area give row-start, column-start, row-end and column-end. Use z-index to choose the front layer. This also works on a grid item with its default position.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Layer items in the same grid area</title>
<style>
.layout { display: grid; }
.layout > * { grid-area: 1 / 1 / 2 / 2; }
.cover { min-height: 80px; padding: 12px; background: #d6efea; z-index: 1; }
.badge { place-self: start end; padding: 4px; background: gold; z-index: 2; }
</style>
</head>
<body>
<div class="layout"><div class="cover">Course notes</div><strong class="badge">New</strong></div>
</body>
</html>
What to expect New appears over the upper corner of the cover. Change the badge to z-index: 0 and the cover hides it.