Aligning Grid Items and Tracks, Overlapping Items, and the grid Shorthand
Alignment in grid on both axes: justify-items, align-items and the -self properties inside cells, justify-content and align-content for the tracks, place-items and place-content, layering items in the same cells, and the grid-template and grid shorthands.
kvi5hnuCode Sprout team member8 min readOn this page
Grid uses the same box alignment properties as flexbox (article 63), but because a grid has fixed cells on both axes, it can do more: every item can be aligned inside its cell horizontally and vertically, and the tracks themselves can be aligned inside the container. Grid also lets items share cells, which makes layering easy. This article covers both, and the shorthands that set a whole grid at once.
Two levels of alignment

| Property | Set on | Aligns | Axis |
|---|---|---|---|
justify-items | container | every item inside its cell | inline (row) axis |
align-items | container | every item inside its cell | block (column) axis |
justify-self | item | that item inside its cell | inline |
align-self | item | that item inside its cell | block |
justify-content | container | the tracks inside the container | inline |
align-content | container | the tracks inside the container | block |
In grid, the names are fixed to the axes: justify-* is always the inline axis (horizontal in English), align-* the block axis. Unlike flexbox, they don't swap with a direction property.
Aligning items in their cells
Each item sits in its grid area, and can be aligned inside it:
| Value | Effect |
|---|---|
normal (initial) | behaves as stretch for most items |
stretch | fill the area, if the item's size is auto |
start, end, center | at the start, end or middle of the area, at the item's own size |
baseline, last baseline | items in a row line up by text baseline |
Tested in Chrome, in 100px × 60px cells: with the defaults, an item with one letter fills its cell, 100 × 60; with justify-items: center; align-items: end, it shrinks to its content, 8 × 18, centered horizontally at the bottom.
One exception to stretching: elements with an aspect ratio, like images, videos and canvases, aren't stretched by normal, so they keep their proportions. Tested in Chrome: a 40 × 20 canvas in a 100px cell stays 40 × 20 at the start. To make an image fill its cell, set width: 100%; height: 100%; object-fit: cover (article 38). place-self: stretch also stretches it, but distorts it unless you add object-fit; align-self: stretch alone stretches the height and the width follows the aspect ratio, so it can overflow the cell.

Compare the first and last panels: normal stretches the text boxes but leaves the canvas at its own size, while an explicit stretch stretches the canvas too.
place-items and place-self
The shorthands set both axes, block first: place-items: align-items justify-items. One value sets both.
.icons { display: grid; place-items: center; } /* every item centered in its cell */
.icons .badge { place-self: start end; } /* this one top right: align start, justify end */
The shortest way to center anything on both axes:
.overlay { display: grid; place-items: center; }
Aligning the tracks
If the tracks don't fill the container, because they're fixed sizes and the container is larger, justify-content and align-content decide where the free space goes, with the same values as in flexbox: start, end, center, stretch, space-between, space-around and space-evenly.
Tested in Chrome: two 100px columns in a 300px grid with justify-content: center start at 50px; with space-between, at 0 and 200px. align-content: end in a 100px-tall grid with one 60px row moves the row down 40px.
.board { display: grid; grid-template-columns: repeat(8, 3rem); justify-content: center; } /* centered in the page */
The space-* values widen the space between tracks, so they make the gaps larger, and items spanning several tracks get wider. With fr tracks, there's usually no free space and these properties do nothing, unless the fr values add up to less than 1.
place-content is the shorthand: place-content: center centers the tracks on both axes.

The items never change size here: only the tracks move, and with fixed-size tracks stretch has nothing to stretch, so it behaves like start.
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
.controls { display: grid; gap: 0.4rem; margin-bottom: 0.75rem; }
select { font: inherit; }
.grid {
display: grid;
grid-template-columns: repeat(3, 4.5rem);
grid-auto-rows: 3.5rem;
gap: 0.3rem;
place-items: var(--pi, normal);
justify-content: var(--jc, start);
border: 2px dashed #8a3b12; border-radius: 8px; padding: 0.3rem;
}
.grid div {
min-inline-size: 1.8rem; min-block-size: 1.6rem;
display: grid; place-items: center;
background: #f4efe6; border: 1px solid #8a3b12; border-radius: 6px;
}
place-items changes where each item sits inside its 4.5rem × 3.5rem cell; with normal, items fill their cells. justify-content moves the three columns as a group, because they're narrower than the container.
Auto margins
margin: auto on a grid item works on both axes and takes the free space inside its area, so margin: auto centers an item in its cell, and margin-inline-start: auto pushes it to the end. Auto margins win over justify-self and align-self.
Overlapping items
Grid items can occupy the same cells: place them in the same area, or with overlapping lines. That's a clean way to layer things without absolute positioning:
.hero { display: grid; }
.hero > * { grid-area: 1 / 1; } /* everything in the same single cell */
.hero img { width: 100%; height: 100%; object-fit: cover; }
.hero .caption { align-self: end; justify-self: start; z-index: 1; } /* text in the bottom left */
- Overlapping items are painted in source order (or
order), so later ones are on top. Tested in Chrome: of two items ingrid-area: 1 / 1, the second one is on top. z-indexworks on grid items without positioning (article 58).- Unlike absolute positioning, every layer still takes part in sizing: the cell grows to fit the largest one, so a caption longer than the image makes the hero taller instead of overflowing.
The grid-template shorthand
grid-template sets grid-template-rows, grid-template-columns and grid-template-areas together. The most readable form writes each row's areas with its height, then the columns after a slash:
.site {
display: grid;
grid-template:
"head head" auto
"nav main" 1fr
"foot foot" auto
/ 12rem 1fr;
}
That's three rows (auto 1fr auto), two columns (12rem 1fr), and the areas, in one declaration that looks like the layout.
The grid shorthand
grid sets everything grid-template does, plus the implicit grid properties (grid-auto-rows, grid-auto-columns, grid-auto-flow), and resets the ones not given. Its main forms, with the values Chrome computes:
grid: | Equals |
|---|---|
"a a" 40px "b c" 1fr / 1fr 2fr | a grid-template value, with auto properties reset |
auto-flow / 1fr 1fr | grid-auto-flow: row, two 1fr columns, rows created as needed |
200px / auto-flow 50px | one 200px row, grid-auto-flow: column, 50px implicit columns |
auto-flow goes on the side the grid grows along; dense can be added, as in auto-flow dense. The shorthand is compact but easy to misread, and it silently resets the grid-auto-* properties (though not gap, which isn't part of it). Many teams prefer the separate properties for anything beyond grid-template areas.
What's current
- All grid alignment properties and values, including
place-*and baseline alignment, work in all browsers. align-contentalso works on block containers now (article 63), andjustify-selfandalign-selfwork on absolutely positioned elements (article 56).anchor-center(article 59) is a further alignment value for anchored elements.
Common questions
What's the difference between justify-items and justify-content?
justify-items aligns each item inside its cell; justify-content aligns the tracks inside the container, when they're smaller than it.
Why doesn't justify-content do anything in my grid?
The tracks already fill the container, usually because they use fr values that add up to at least 1.
Why isn't my image stretched to fill its cell?
Items with an aspect ratio aren't stretched by default. Set width: 100%; height: 100%; object-fit: cover.
How do I layer text over an image in grid?
Put both in the same area, grid-area: 1 / 1, and align the text with align-self and justify-self.
Which order does place-items: start end use?
Block axis first, then inline: align-items: start; justify-items: end.
Summary
justify-*is the inline axis andalign-*the block axis, always.justify-items,align-itemsand the-selfproperties align items inside their areas;normalstretches, except for items with an aspect ratio.justify-contentandalign-contentalign the tracks when there's free space;place-*shorthands set both axes, block first.- Items can share cells and are painted in source order;
z-indexworks without positioning. grid-templatesets rows, columns and areas together;gridalso sets and resets the implicit grid properties.
Practice questions
Write CSS that puts a single child in the exact center of a full-screen container.
Answer
.screen { display: grid; place-items: center; min-block-size: 100dvh; }
A grid has the rule below, in an 800px container. Where do the columns start?
.g { display: grid; grid-template-columns: repeat(3, 200px); justify-content: space-evenly; }
Answer
The free space is 800 − 600 = 200px, split into four equal gaps of 50px. The columns start at 50px, 300px and 550px.
Write CSS for a card where a "Sale" badge sits on the image's top right corner, using grid and no absolute positioning.
Answer
.card-media { display: grid; }
.card-media > * { grid-area: 1 / 1; }
.card-media .badge { place-self: start end; margin: 0.5rem; }
Rewrite this as separate properties.
.g { grid: auto-flow dense / repeat(4, 1fr); }
Answer
.g {
grid-template-columns: repeat(4, 1fr);
grid-auto-flow: row dense;
grid-auto-rows: auto;
grid-template-rows: none;
grid-template-areas: none;
grid-auto-columns: auto;
}
Previous: The Implicit Grid and Auto-Placement: grid-auto-rows, grid-auto-flow and dense

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.