Center with margin: auto in CSS Grid

Center elements with auto-margins

In CSS Grid, margin: auto centers items in their grid area by distributing space evenly on both sides. It's less code than place-items when you want horizontal and vertical centering with different semantics.

Old way 7 lines
/* Old approach: absolute positioning + transform */.parent {  position: relative;  height: 100%;}.child {  position: absolute;  top: 50%;  left: 50%;  transform: translate(-50%, -50%);}/* brittle, removes from flow */
2 lines
.parent {  display: grid;  height: 100%;}.child   {  margin: auto;}/* margin: auto distributes space evenly */
Widely available Since 2017 96% global usage

This feature is well established and works across many devices and browser versions. It has been available across browsers since 2017.

Safe to use without fallbacks.

57+
52+
10.1+
16+
margin: auto centers within the grid area
margin: auto
margin: auto 0

Left: centered both ways. Right: centered horizontally only. Asymmetric centering with margin.

Single property

margin: auto on the child instead of alignment properties on parent. Different mental model, same result.

Allows gap control

Works naturally with grid gap. The child centers within its grid area while gap is preserved.

Asymmetric centering

Can use margin: auto 0 for horizontal centering only, or 0 auto for vertical. Fine-grained control.

Modern Approach
margin: auto
On the child in a grid context
Best For
Asymmetric spacing
When you need row OR column centering

How it works

In CSS Grid, when you set margin: auto on a child, the browser distributes available space evenly on both sides of that margin in both axes. This centers the item within its grid area.

The key difference from place-items: this is a property on the child, not the parent. It also plays well with grid gap—the margin centers the child within the area left after gap is accounted for.

This approach is useful when you want different behavior per axis (e.g., margin: auto 0 for horizontal centering only) or when you prefer thinking in terms of child properties rather than parent alignment.

ESC