Skip to content
Tutorials Center
My learning

Topics

Lesson 63 of 139 ยท Layout Fundamentals

Centering Anything

Center text, blocks and whole components in CSS, horizontally and vertically, with text-align, margin auto, flexbox, grid, place-items and position plus transform.

"How do I center a div?" is probably the most famous question in CSS. For years the honest answer was "it depends," followed by a list of hacks. Today it is much simpler: a couple of lines of flexbox or grid center almost anything in both directions.

It still pays to know a few techniques, though, because "center" can mean different things. Centering a line of text is not the same as centering a fixed-width column, and centering a modal over the whole screen is different again. This lesson gives you a small toolbox and a simple way to pick the right tool.

First, ask two questions

Before writing any CSS, answer these:

  1. What are you centering? Inline content (text, links, inline images), or a block box (a card, a column, a dialog)?
  2. In which direction? Horizontally, vertically, or both?

Then choose from this cheat sheet:

Goal Technique
Text or inline content, horizontally text-align: center on the parent
A block with a width, horizontally margin-inline: auto on the block
Anything, both directions display: grid; place-items: center on the parent
A group of items, both directions display: flex; justify-content: center; align-items: center on the parent
An element over others (overlay, badge) position: absolute; inset: 0; margin: auto or translate: -50% -50%

The rest of this lesson walks through each one.

Centering text and inline content

text-align: center centers the inline content of a block: its lines of text, links, inline images and inline-block elements. You set it on the container, not on the thing being centered:

CSS
.hero {
  text-align: center;
}

It does not center a block child itself. A <div> with a width inside a text-align: center parent stays on the left; only its text is centered (because text-align inherits). That mix-up is behind many "why won't it center" moments.

Centering a block horizontally with margin: auto

A block box with a known width can be centered by setting its left and right margins to auto. The browser splits the leftover space equally between them:

CSS
.page {
  max-width: 70ch;
  margin-inline: auto;   /* same as margin-left: auto; margin-right: auto */
}

Two conditions apply: the element must be a block, and it must be narrower than its container (through width, max-width or fit-content). A block with the default width: auto already fills the container, so there is nothing to split. This is the classic way to center a page's main column, and you met it in Margins.

Auto margins do not center vertically in normal flow: margin-block: auto resolves to zero there. They do, however, work in both directions inside flex and grid containers, as you will see below.

text-align versus margin-inline: auto

Both directions with grid: place-items: center

The shortest way to center one element in both directions is a grid container:

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

place-items is a shorthand for align-items (vertical) and justify-items (horizontal), so this centers every child in its grid cell. With one child, the cell is the whole container. The container needs some height for vertical centering to be visible; by default a block is only as tall as its content.

Grid and flex centering side by side

The last box uses place-content: center instead of place-items. The difference: place-items centers each child inside its own cell, while place-content packs the whole grid into the middle of the container. With several children, place-content keeps them together as a tight group, and gap controls the spacing. You will learn these properties in depth in Grid Alignment.

Both directions with flexbox

Flexbox centers with two properties: justify-content along the main axis (horizontal in a row) and align-items along the cross axis (vertical in a row). It is the natural choice when you center a group of items that should sit in a row, such as an icon next to a label, or a set of buttons. With flex-direction: column, the axes swap, but the same two lines still center in both directions.

Flex items also accept margin: auto in both directions. A single child with margin: auto inside a flex container is centered horizontally and vertically, because auto margins absorb free space on every side:

CSS
.parent { display: flex; min-height: 200px; }
.child  { margin: auto; }

Overlays: positioning and translate

Sometimes the element must sit on top of other content, so it cannot take part in the parent's layout: a play button over a video thumbnail, a spinner over a loading card, a label over an image. Then you combine absolute positioning with one of two tricks.

Inset plus auto margins. If the element has a size, stretch its containing area with inset: 0 and let auto margins center it:

CSS
.play {
  position: absolute;
  inset: 0;
  width: 64px;
  height: 64px;
  margin: auto;
}

Top and left 50%, then translate back. If the size is unknown, move the element's top-left corner to the center, then shift it back by half its own size. Percentages in translate refer to the element itself:

CSS
.label {
  position: absolute;
  top: 50%;
  left: 50%;
  translate: -50% -50%;   /* or: transform: translate(-50%, -50%); */
}
Centering on top of an image

The button has a fixed size, so it uses the auto-margin method. The label's width depends on its text, so it uses translate. Change the label's top to 50% and it sits exactly in the middle too.

Blurry text with translate. A -50% shift can land on half a pixel, which sometimes makes text look slightly soft. If you notice it, prefer grid or the auto-margin method, which place elements on whole pixels.

Centering a full-screen section

For a hero or a "page not found" screen that fills the viewport, combine a height with grid:

CSS
.full-screen {
  min-height: 100dvh;
  display: grid;
  place-items: center;
}

100dvh follows the visible viewport height on mobile browsers, as explained in Viewport Units. Use min-height, not height, so long content can still grow.

A newer option: align-content on plain blocks

Since 2024, align-content also works on ordinary block containers, not just flex and grid. So a regular <div> with a height can center its content vertically with one line:

CSS
.banner {
  min-height: 200px;
  align-content: center;
}

The children stay in normal flow; they are simply moved down as a group. It works in current versions of Chrome, Edge, Firefox and Safari. Older browsers ignore it and show the content at the top, which is usually an acceptable fallback.

Vertical centering of a single line

You may see line-height set equal to a box's height to center a single line of text vertically. It works only for exactly one line and breaks as soon as the text wraps or the font size changes. Use flexbox or grid instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A div with width: 300px sits inside a parent with text-align: center, but stays on the left. Why?

    text-align affects inline content. To center the block itself, use margin-inline: auto.
  2. 2. What is the shortest way to center a single child horizontally and vertically?

    A grid container with place-items: center centers its child in both directions. Auto margins only center vertically inside flex or grid containers.
  3. 3. In a flex row, which property centers items vertically?

    In a row, the cross axis is vertical, and align-items aligns along the cross axis.
  4. 4. Why does translate: -50% -50% work for centering an absolutely positioned element of unknown size?

    After top: 50%; left: 50%, the element's corner is at the center. Translating by half its own size moves its middle there.

Summary

  • Decide what you are centering (inline content or a block) and in which direction.
  • text-align: center centers inline content; margin-inline: auto centers a block with a limited width.
  • display: grid; place-items: center centers anything in both directions; place-content: center centers a group.
  • Flexbox centers with justify-content and align-items, and auto margins work in both directions on flex and grid items.
  • For overlays, use absolute positioning with inset: 0; margin: auto, or top: 50%; left: 50%; translate: -50% -50%.

Next: Multi-column Layout โ€” flow text into newspaper-style columns.