CSS Reference ยท Flexbox
The place-items property
Reference for the CSS place-items shorthand: set align-items and justify-items together, center content in grid cells, and avoid the flexbox pitfall.
The place-items property is a shorthand for align-items and justify-items. It sets the default alignment of every child inside its own grid area, on both axes at once. You can still override it per item with place-self.
If you give one value it is used for both axes. With two values the first is align-items (block axis) and the second is justify-items (inline axis).
.card { display: grid; place-items: center; }
Try it
Each coloured cell is itself a one-item grid, so its place-items value positions the white label inside it: centered, at the top right (start end), and at the bottom left (end start). The next example shows the more common use, on the outer grid.
Each item shrinks to its content and sits in the middle of its cell. Without place-items the items would stretch to fill their cells.
Syntax
place-items: <'align-items'> <'justify-items'>?
- Initial value:
normal - Applies to: all elements (the longhands decide what actually happens)
- Inherited: no
- Animatable: no
- Computed value: as specified for the longhands
Values
| Value | Description |
|---|---|
normal |
Default. In grid, items stretch to fill the cell. |
stretch |
Items grow to fill the cell in that axis unless a size is set. |
start, end |
Aligns to the start or end edge of the cell. |
center |
Centers the item in the cell. |
self-start, self-end |
Aligns by the item's own writing mode. |
baseline |
Aligns first baselines (useful for text in different font sizes). |
safe / unsafe |
Optional overflow keyword before a position. |
Usage notes
- Flexbox gotcha. In a flex container
justify-itemsis ignored, soplace-items: centeronly centers on the cross axis. Usejustify-contentfor the main axis, or switch to grid, wheredisplay: grid; place-items: centerreally centers. - Items shrink-wrap. Anything other than
stretchmakes items only as large as their content. Set a width or height if you want a specific size. - Cell, not container. The alignment is relative to each grid area; to move the whole grid inside its container use
place-content. - Exceptions. Override single items with
align-self,justify-selforplace-self.
Browser support
Supported in all current browsers.
Related
align-itemsโ block axisjustify-itemsโ inline axisplace-selfโ per-item versionplace-contentโ aligns tracks as a group- Lesson: Grid Alignment