Grid centering

OSCC · CSS, Layout · Dec 30, 2020

Horizontally and vertically centers a child element within a parent element using grid.

  • Use display: grid to create a grid layout.
  • Use justify-content: center to center the child horizontally.
  • Use align-items: center to center the child vertically.
Preview
Centered content.
<div class="grid-centering">
  <div class="child">Centered content.</div>
</div>
.grid-centering {
  display: grid;
  justify-content: center;
  align-items: center;
  height: 100px;
}

Recommended snippets & collections

  • CSS Centering

    Snippet collection

    A collection of techniques for centering HTML elements in any situation using CSS.

  • Horizontally and vertically centers a child element within a parent element using flexbox.

  • Vertically and horizontally centers a child element within its parent element, using display: table.

  • Vertically and horizontally centers a child element within its parent element using CSS transforms.