The short answer

#

Make the parent a grid container and use place-items: center. The child sits in the middle on both axes, whatever its size. The parent needs a height for vertical centring to be visible.


_10
.parent {
_10
display: grid;
_10
place-items: center;
_10
min-height: 100vh;
_10
}

Why it happens

#

Centring feels harder than it should because block layout, the default for a div, only really knows about the inline (horizontal) axis. margin: 0 auto works horizontally because a block with a fixed width can share leftover space on the left and right. There is no equivalent for the vertical axis in normal flow, because a block's height usually comes from its content, so there is no leftover space to share.

The other common surprise is height. A parent with no height set is only as tall as its content. If the parent is exactly as tall as the child, the child is already "centred" vertically, it just does not look like it. Give the parent a min-height (for example the viewport height) and the centring becomes visible.

Grid and Flexbox both have alignment properties that work on both axes, which is why they are the modern answer.

How to fix it

#

Grid with place-items

This is the shortest option and works for one child or several stacked children.


_10
.parent {
_10
display: grid;
_10
place-items: center;
_10
min-height: 100vh;
_10
}

place-items is shorthand for align-items and justify-items. If you only want to centre one specific child, put place-self: center on that child instead.

Flexbox with justify-content and align-items

Flexbox is a good choice when the parent also lays out other items in a row or column.


_10
.parent {
_10
display: flex;
_10
justify-content: center; /* main axis */
_10
align-items: center; /* cross axis */
_10
min-height: 100vh;
_10
}

In a row (the default), justify-content controls horizontal placement and align-items controls vertical placement. If you set flex-direction: column, the two swap roles.

Auto margins inside a flex or grid container

Auto margins absorb free space on both axes inside a flex or grid parent, so this also centres the child:


_10
.parent {
_10
display: flex;
_10
min-height: 100vh;
_10
}
_10
_10
.child {
_10
margin: auto;
_10
}

This is handy when you want one child centred and you are not allowed to change the parent's alignment for its other children.

Absolute positioning with inset and margin auto

For overlays such as a spinner on top of a card, absolute positioning still has its place. When the child has a known size, inset: 0 plus margin: auto centres it:


_11
.card {
_11
position: relative;
_11
}
_11
_11
.spinner {
_11
position: absolute;
_11
inset: 0;
_11
width: 48px;
_11
height: 48px;
_11
margin: auto;
_11
}

If the size is unknown, use the older transform version:


_10
.spinner {
_10
position: absolute;
_10
top: 50%;
_10
left: 50%;
_10
transform: translate(-50%, -50%);
_10
}

Horizontal only

If you only need horizontal centring of a block with a width, the classic approach is still fine:


_10
.container {
_10
max-width: 60rem;
_10
margin-inline: auto;
_10
}

For text and inline content inside a block, use text-align: center on the parent.

Common pitfalls

#
  • The parent has no height. Vertical centring has nothing to centre within. Add min-height to the parent.
  • Using height: 100vh on mobile. The viewport unit can be taller than the visible area when the browser toolbar is showing. Consider min-height: 100dvh instead.
  • text-align: center on a block child. It centres the text inside the block, not the block itself.
  • Mixing up axes in a column flex container. With flex-direction: column, justify-content becomes vertical.
  • Translate blur. The translate(-50%, -50%) trick can land on half pixels and make text look slightly soft. Grid or Flexbox avoids this.
  • Overflowing content. If the child is taller than the parent, centring pushes part of it off the top where it cannot be scrolled to. Use align-items: safe center where supported, or fall back to margin: auto on the child, which never overflows the start edge.
#
file under:
CSS

quick question

Was this resource helpful?