Codectionary / Developer documentation / CSS

Grid Alignment: place-items, place-content

Grid alignment properties control how content is positioned within grid cells and how the grid itself is positioned within its container. justify-items/align-items control individual item alignment along each axis, and their shorthand place-items sets both at once. justify-content/align-content position the whole grid when it is smaller than its container, with place-content as their shorthand.

Syntax

place-items: align justify;
place-content: align justify;

Examples

Centering with place-items

The famous one-line perfect-centering trick using Grid.

.center {
  display: grid;
  place-items: center; /* centers child both vertically and horizontally */
  height: 300px;
}

Positioning the Grid Itself

When the grid is smaller than its container, place-content controls its position.

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  place-content: center; /* centers the whole grid within a larger container */
  height: 400px;
}

Parameters and values

  • justify-items (start | center | end | stretch): Aligns items horizontally within their cells
  • align-items (start | center | end | stretch): Aligns items vertically within their cells
  • place-items (shorthand): Sets align-items and justify-items together
  • place-content (shorthand): Positions the entire grid within its container

Best practices

  • Use place-items: center as the simplest, most reliable way to perfectly center a single child element
  • Remember justify-items/align-items affect items within their cells, while justify-content/align-content position the grid tracks as a whole within the container
  • These same alignment properties work in Flexbox too, though the axis they refer to depends on flex-direction there
  • Use align-self/justify-self on an individual grid item to override the container's place-items just for that one item

At a glance

Purpose
Presentation and layout
File extension
.css
Runs in
Web browsers
Usually used with
HTML and JavaScript

Specifications & further reading

Related CSS documentation