Center anything with grid
Center an element horizontally and vertically using two lines of CSS.
- CSS
- GreenSnippets
- MIT
.center {
display: grid;
place-items: center;
min-height: 100dvh;
}Example / expected output
<!-- <div class="center"><div>I'm centered</div></div> -->How it works
place-items: center is shorthand for align-items and justify-items. Combined with display: grid it centers children in both axes. Use place-content: center instead if the child should size to its content rather than stretch.
This snippet has been reviewed but was not executed by an automated test. Test it in your own environment before relying on it.
Related snippets
- CSS
Header, sidebar, main and footer layout with grid
A full-height page layout with a sticky footer using grid-template-areas.
- CSS
CSS-only scroll-snap carousel
A horizontally scrolling gallery that snaps to each card, with no JavaScript.
- CSS
Keep media at a fixed aspect ratio
Reserve space for videos and images with aspect-ratio so the page does not jump while loading.
- CSS
Responsive card grid without media queries
Use CSS Grid with repeat(auto-fit, minmax()) so cards wrap automatically at any width.

