Centering anything

Topic 2 of 2 in Choosing a layout tool7 min readBeginnerFree course

Overview

“How do I centre a div?” was a running joke for years because the answer depended on everything: inline or block, known size or not, horizontal or vertical. Modern CSS has made it easy — but there are still several techniques, each best in different situations. This lesson collects them in one place.

Centring techniques. Grid: display: grid; place-items: center — one or more items, both axes. Flexbox: display: flex; justify-content: center; align-items: center — a row of items. Block + auto margin: max-inline-size: 60ch; margin-inline: auto — a page column, horizontally. align-content: center — vertically, in a normal block. Absolute: position: absolute; inset: 0; margin: auto — an overlay of known size.

Horizontally

Inline content: text-align

Text, images, links and other inline or inline-block content is centred by its parent:

CSS
.hero { text-align: center; }

A block with a width: auto margins

A block element with a width narrower than its container can be centred with automatic inline margins — the classic centred page column:

CSS
.container {
  max-inline-size: 70rem;
  margin-inline: auto;
  padding-inline: 1rem;   /* keep content off the screen edges on small screens */
}

The block must have a width (or max-width) smaller than its container, or there’s no space to share. width: fit-content centres a block that should shrink to its content:

CSS
.callout { width: fit-content; margin-inline: auto; }

Both directions

Grid: the shortest

CSS
.parent {
  display: grid;
  place-items: center;
}

The child is centred horizontally and vertically, at its natural size — no widths, heights or transforms required. The parent needs a height larger than the child for vertical centring to be visible (min-height: 100dvh for a full-screen splash).

Flexbox

CSS
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

The flexbox version is natural when you’re centring a row of items as a group — a set of buttons, an icon and a label.

Auto margins in flex or grid

CSS
.parent { display: flex; }   /* or grid */
.child { margin: auto; }

An auto margin absorbs space on all sides. Useful when only one child should be centred and the container’s alignment is already set for something else.

Vertically in a normal block

Since 2024, align-content works in ordinary block layout — no flex or grid needed:

CSS
.panel {
  height: 20rem;
  align-content: center;   /* centres the block's content vertically */
}

It’s supported in all major browsers (Chrome 123, Firefox 125, Safari 17.4 and later). It’s handy when you don’t want to change an element’s display type, for example inside table cells or simple containers.

Overlays: absolute positioning

When something must sit on top of other content — a spinner over a card, a play button over a video:

CSS
.card { position: relative; }

/* Any size: move the top-left corner to the centre, then pull back by half */
.spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  translate: -50% -50%;
}

/* Known size: stretch the box's insets and let auto margins centre it */
.modal {
  position: fixed;
  inset: 0;
  margin: auto;
  width: min(30rem, 90vw);
  height: fit-content;
}

Grid stacking (grid-area: 1 / 1 for both children, from Placing grid items) is an alternative that keeps the overlay in the flow, so the container grows if the overlay is larger than the content beneath it.

Avoid these older techniques

You’ll still see them in old code — you no longer need them:

  • line-height equal to the container height (only works for a single line of text),
  • display: table-cell; vertical-align: middle,
  • negative margins of half a fixed width and height,
  • transform: translate(-50%, -50%) — still fine, but the individual translate property is simpler and doesn’t conflict with other transforms.

Watch out for overflow

When centred content is larger than its container (a tall dialog on a short phone screen in landscape), there’s nowhere to centre it. With flexbox centring, the extra height is split equally above and below, so the top of the content ends up above the container — and you can’t scroll up to it. In our tests, a 400px item centred in a 200px scrolling flex container started 100px above the top, and only 300px of it could ever be scrolled into view.

The safe keyword fixes it: it centres when there’s room and falls back to start when the content would overflow.

CSS
.dialog-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: safe center;   /* centred, unless that would cut off the top */
  overflow: auto;
}

Grid’s place-items: center and block align-content: center kept the overflowing content reachable in the same test, so this is mainly a flexbox trap — but adding safe anywhere you centre content of unknown height costs nothing and makes the intent clear.

Which one?

Situation Technique
Text or inline content text-align: center
A page or content column max-inline-size + margin-inline: auto
One element, both ways display: grid; place-items: center
A group of items in a row flexbox with justify-content + align-items
Vertically, without changing display align-content: center
On top of other content absolute + translate, or grid stacking

One to remember: display: grid; place-items: center;. Two lines, any size, both directions.

Exercise

Five centring jobs

Centre five things, each with the most fitting technique: (1) the text inside .banner; (2) the .article column (max 40rem) in the page; (3) an .icon both ways inside a 3rem round .avatar circle; (4) the .empty message both ways inside a 20rem-tall .panel using a normal block, without flex or grid; (5) a .spinner in the middle of a .card that has other content, overlaying it.

HTML
<div class="banner">Free shipping on all courses</div>
<article class="article">Article text…</article>
<span class="avatar"><span class="icon">★</span></span>
<div class="panel"><p class="empty">No courses yet</p></div>
<div class="card"><p>Loading your progress…</p><p>Please wait.</p><span class="spinner">⟳</span></div>

<style>
  * { box-sizing: border-box; }
  .banner, .panel, .card { background: #eaf0fc; margin-block: 1rem; }
  .avatar { width: 3rem; height: 3rem; border-radius: 50%; background: #2d5bd7; color: white; }
  .panel { height: 20rem; }
  .card { padding: 2rem; }
  .spinner { font-size: 2rem; }
  /* your code here */
</style>

Summary

  • Centre inline content (text, inline elements) with text-align: center.
  • Centre a block horizontally with a max width and margin-inline: auto.
  • Centre anything both ways with display: grid; place-items: center on the container.
  • align-content: center now centres content vertically in a plain block, with no flex or grid.
  • For overlays, use absolute positioning with inset: 0; margin: auto (known size) or translate: -50% -50% (any size).