Menu

How to Center a Div in CSS: Every Method, Measured

Every way to center a div horizontally, vertically or both: flexbox, grid, margin auto, absolute position with transform, inset with margin auto, and align-content. Each one runs live and measures its own centering, with a table of when to use which.

This page includes runnable editors - edit, run, and see output instantly.

The short answer

To center a div both horizontally and vertically inside its parent, make the parent a grid and center its content:

.parent {
  display: grid;
  place-items: center;
  min-height: 200px; /* the parent needs room to center in */
}

That works for any size of child and needs nothing on the child itself. Below are six methods side by side. Each box measures itself after it renders and prints how far its center is from the parent's center:

All six report 0px on both axes (the stage's 1px border is equal on every side, so it does not shift the center). Change a box's width or text and run it again: the methods that do not depend on a fixed size stay at 0.

Which method to use

MethodCentersChild needs a fixed size?Best for
Grid place-items: centerBoth axesNoThe default choice: one child in a box
Flexbox justify-content + align-itemsBoth axesNoA row or column of items, centered as a group
margin: auto in a flex or grid parentBoth axesNoCentering one item while its siblings are laid out differently
margin: 0 autoHorizontal onlyA width or max-widthPage containers, articles, forms in normal flow
Absolute + translate(-50%, -50%)Both axesNoOverlays, badges and modals on top of other content
Absolute + inset: 0; margin: autoBoth axesYes, width and heightFixed size dialogs
align-content: center on a blockVerticalNoVertical centering without changing the parent's display

Method 1: flexbox

Flexbox centers along two axes with two properties. justify-content handles the main axis (horizontal in a row), and align-items the cross axis:

With several children, flexbox centers them as a group, keeping them in a row. That is where it beats grid, which would stack the three items in one centered column. The justify-content page covers every alignment value.

Method 2: grid

place-items: center is shorthand for align-items: center plus justify-items: center. On a grid container with a single child it centers that child in both directions:

min-height rather than height lets the parent grow if the card gets taller than 180px, instead of the card overflowing.

Method 3: margin: 0 auto (horizontal)

A block element with a width smaller than its container can be centered horizontally with automatic side margins. The browser splits the free space equally between the two sides:

The second box fills the whole width, so there is no free space and nothing moves. That is the usual reason margin: 0 auto "does not work". It also only centers horizontally in normal flow; vertical auto margins become 0 there. Inside a flex or grid parent, though, margin: auto centers in both directions, which is the third box in the comparison above.

Method 4: absolute position and transform

When the element must sit on top of other content (a badge on a photo, a modal over the page), position it absolutely at 50% and pull it back by half its own size:

top: 50%; left: 50% puts the element's top left corner at the center. The percentages in translate() refer to the element's own size, so -50% moves it back by half its width and height whatever they are. The parent must be positioned (position: relative), or the element centers on the page instead. Positioning is covered on the position page.

A variant for elements with a known size: position: absolute; inset: 0; margin: auto; plus a width and height. The auto margins share the space on all four sides.

Method 5: align-content on a normal block

align-content: center now also works on ordinary block containers, not only flex and grid ones. It centers the content vertically without changing how the children are laid out:

.parent {
  min-height: 200px;
  align-content: center;   /* vertical centering, still display: block */
}
.child {
  max-width: 300px;
  margin-inline: auto;     /* horizontal centering */
}

It is supported in current Chrome, Firefox and Safari. Older browsers ignore it, so use grid if you have to support them.

Centering in the whole screen

To center a dialog in the viewport regardless of scrolling, combine a fixed full-screen layer with grid:

.overlay {
  position: fixed;
  inset: 0;                     /* covers the viewport */
  display: grid;
  place-items: center;
  background: rgb(0 0 0 / 50%);
}

For centering a whole page's content vertically, use min-height: 100dvh on the parent rather than 100vh: on phones, 100vh includes the area behind the browser's toolbars, so the "center" sits too low.

Centering text is a different job

Everything above centers a box. To center the text inside a box, use text-align: center on it, as shown on the HTML center text page. You often need both: grid to center the card, and text-align to center the lines inside it.

Common mistakes

  • A parent with no height. Vertical centering needs free space; a parent that shrinks to its content has none.
  • margin: 0 auto on a full-width block. Give it a width or max-width first.
  • margin: auto to center vertically in normal flow. Vertical auto margins are 0 there; use flex or grid.
  • Absolute centering without a positioned parent. The element centers in the page, not the box.
  • align-items: center on the child. Alignment properties go on the parent (the flex or grid container), except margin: auto and align-self.
  • text-align: center to center a block. It centers inline content (text, images, inline-block children), not a block child.

Frequently Asked Questions

How do I center a div horizontally and vertically?

Make the parent a grid and center its content: .parent { display: grid; place-items: center; }. The flexbox version is display: flex; justify-content: center; align-items: center;. Either way the parent needs a height taller than the div.

How do I center a div horizontally only?

Give it a width and auto side margins: .box { width: 300px; margin: 0 auto; } (or max-width for a responsive block). margin-inline: auto is the same thing in logical properties.

Why is margin: 0 auto not centering my div?

The div has no width smaller than its container, so there is no free space to share (a block fills the full width by default). Set width or max-width. It also does nothing on inline elements and never centers vertically in normal flow.

Why doesn't my div center vertically?

The parent is only as tall as its content, so there is no extra space to center in. Give the parent a height (or min-height), then use grid or flexbox centering.

What is the easiest way to center a div?

display: grid; place-items: center; on the parent: two declarations, both axes, any size of child. Use flexbox when the parent also lays out other items in a row.

How do I center text inside a div?

Use text-align: center on the div for text and inline content. Centering the div itself and centering the text inside it are separate jobs.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED