How to center an element in CSS

Copy-paste CSS for centering horizontally, vertically or both, using grid, flexbox, auto margins and absolute positioning.

Updated

The shortest way to center one element both horizontally and vertically is to make its parent a grid container and set place-items: center. The parent needs a height taller than the child for vertical centering to show.

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

The rest of this page covers the other cases.

Center both ways with flexbox

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

justify-content works along the main axis, which is horizontal by default, and align-items works along the cross axis. With flex-direction: column the two swap.

Choose flexbox over grid when there are several children that should sit in a row or column and be centered as a group.

Center one child with auto margins

Inside a flex or grid container, margin: auto on the child absorbs the free space on every side.

.parent {
  display: flex;
}
.child {
  margin: auto;
}

This is useful when only one of several children should be centered, or pushed to one side with margin-left: auto.

Center a block horizontally

For a block with a set width or max-width, such as a page column:

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

margin-inline: auto sets the left and right margins to auto. The element must be narrower than its parent for this to have any effect.

Center text and inline content

.title {
  text-align: center;
}

text-align centers the inline content inside a block: text, links, inline images and inline-block elements. It does not center the block itself.

Center an overlay with absolute positioning

When the element has to sit on top of other content, such as a modal or a badge:

.parent {
  position: relative;
}
.overlay {
  position: absolute;
  inset: 0;
  margin: auto;
  width: fit-content;
  height: fit-content;
}

The older version, which works when the size is not known and in every browser:

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

For something centered in the viewport, use position: fixed with the same rules.

Which one to use

Situation Use
One child, both axes Grid with place-items: center
Several children in a row or column Flexbox with justify-content and align-items
A page column max-width with margin-inline: auto
Text inside a box text-align: center
An element layered over others Absolute positioning

Why vertical centering "does nothing"

Nearly always, the parent is only as tall as its content, so there is no spare space to distribute. Give the parent a height:

.parent {
  min-height: 100vh;
}

On mobile browsers, 100vh can be taller than the visible area because of the browser's toolbars. 100dvh tracks the visible height:

.parent {
  min-height: 100vh;
  min-height: 100dvh;
}

The first line is the fallback for browsers that do not support dvh.

More layout: a responsive card grid without media queries.