Align and overlap items in a grid.
Grid has two kinds of alignment: moving items inside their areas, and moving the rows and columns as a group inside the container.
.labels {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
grid-auto-rows: 6rem;
align-items: center;
justify-items: start;
gap: 1rem;
}In a horizontal writing mode, each label sits at the left of its area in left-to-right text, centered vertically. Unlike Flexbox, Grid's alignment axes do not switch through flex-direction:
| Property | What it aligns |
|---|---|
align-items | All items within their areas on the block axis. |
justify-items | All items within their areas on the inline axis. |
align-self | One item on the block axis. |
justify-self | One item on the inline axis. |
For horizontal text, the block axis is vertical and the inline axis is horizontal. Inline start follows the text direction. Block start follows the writing mode, so in horizontal writing it stays at the top for both left-to-right and right-to-left text. Items with automatic sizes usually stretch to fill their areas, though images with a natural aspect ratio can behave differently.
In horizontal left-to-right text, which property centers every item vertically inside its area?
Use align-items: center;: the block axis is vertical in this writing mode.
Now keep the other items at inline start and move only .featured to inline end. Which property belongs on that item?
Use justify-self: end; on .featured. It overrides the container's inline item alignment for that item.
.centered {
display: grid;
min-height: 16rem;
place-items: center;
}place-items: center sets both align-items and justify-items to center. With one child, that child is centered within its area. This shorthand does not center a Flexbox main axis because justify-items is ignored in flex layout.
For one exception in the label grid:
.labels > .featured {
justify-self: end;
}Its inline alignment changes while its siblings keep justify-items: start.
.small-grid {
display: grid;
width: 30rem;
height: 20rem;
grid-template-columns: repeat(2, 6rem);
grid-template-rows: repeat(2, 4rem);
gap: 1rem;
justify-content: center;
align-content: center;
}The fixed tracks occupy less space than the container, so both content-alignment properties center the entire track group. By comparison, justify-items: center would only center each item inside its existing area.
If fractional tracks already consume all available inline space, there is no remaining inline room to move the track group.
.cover {
display: grid;
}
.cover > * {
grid-column: 1 / 2;
grid-row: 1 / 2;
}
.cover > .badge {
justify-self: end;
align-self: start;
z-index: 1;
}Both children use the same cell. Set z-index to put the badge in front; grid items can use it without position. Make sure the overlap leaves text readable and controls reachable.
items and self properties align within assigned areas.content properties distribute the track group when space remains.place-items sets both item-alignment axes.z-index can create intentional layers.Try it yourself
HTML + CSS