Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Practical Pages

Centering cheat sheet

Every practical way to center in CSS: text, a block, and an element both horizontally and vertically with flexbox, grid, margins and positioning.

"How do I center this?" has a different answer depending on what you center and in what. This cheat sheet groups the techniques by situation. In modern CSS, the shortest answer for centering one thing in both directions is display: grid; place-items: center; on the parent.

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

Try it

Six parents of the same size, each using a different technique. Every pink square must end up in the middle of its dashed box.

Six ways to center

Horizontal only

What you center Technique
Inline content (text, links, inline images) text-align: center on the parent
A block with a set width margin-inline: auto (needs a width or max-width)
Flex items justify-content: center on the flex container
A block that shrinks to its content width: fit-content; margin-inline: auto
CSS
.page { max-width: 60rem; margin-inline: auto; }
.label { width: fit-content; margin-inline: auto; }

Vertical only

  • Single line of text in a box of known height: set line-height equal to the height. It breaks as soon as the text wraps.
  • Anything, in a flex container: align-items: center (the container needs a height to center within).
  • Grid: align-content: center for the whole grid, or align-items: center for items in their cells.
  • Table-cell trick (legacy): display: table-cell; vertical-align: middle. Use flexbox or grid instead.

Both directions

  1. Grid + place-items: center is the shortest. For several children use place-content: center so the whole group is centered.
  2. Flexbox with justify-content: center; align-items: center is equally reliable and familiar.
  3. Absolute positioning is for overlays and badges: either top: 50%; left: 50%; translate: -50% -50%, or inset: 0; margin: auto with a fixed size.
  4. A single flex or grid child can center itself with margin: auto, no properties on the parent other than the display type.

Usage notes

  • The parent needs space. Vertical centering does nothing if the parent is only as tall as its content. Give it a height, min-height (for example 100dvh for a full-screen hero) or let a layout stretch it.
  • margin: auto does not center vertically in normal flow. Auto vertical margins compute to 0 in block layout; they work in flex, grid and absolutely positioned boxes.
  • Overflow in flex: justify-content: center can push content off the start edge when it is bigger than the container, where it becomes unreachable by scrolling. Use safe center or a margin approach if that matters.
  • Subpixel blur: translate: -50% -50% can land on half pixels and blur text on some displays; grid or flex avoid that.
  • Centering text inside a centered box is a separate step: add text-align: center as well.

Browser support

Flexbox, grid, margin: auto and text-align are supported everywhere; the individual translate property and the safe alignment keyword are newer, so check a compatibility table if you must support old browsers.