---
title: "How to centre a div horizontally and vertically in CSS"
description: "Centre any element on both axes with CSS Grid and place-items, or with Flexbox. Learn why older tricks fail and which approach to pick for each layout."
url: https://www.danny.engineering/answer/css/how-to-centre-a-div-horizontally-and-vertically-in-css
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["CSS"]
---

# How to centre a div horizontally and vertically in CSS

## The short answer


Make the parent a grid container and use `place-items: center`. The child sits in the middle on both axes, whatever its size. The parent needs a height for vertical centring to be visible.


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


## Why it happens


Centring feels harder than it should because block layout, the default for a `div`, only really knows about the inline (horizontal) axis. `margin: 0 auto` works horizontally because a block with a fixed width can share leftover space on the left and right. There is no equivalent for the vertical axis in normal flow, because a block's height usually comes from its content, so there is no leftover space to share.


The other common surprise is height. A parent with no height set is only as tall as its content. If the parent is exactly as tall as the child, the child is already "centred" vertically, it just does not look like it. Give the parent a `min-height` (for example the viewport height) and the centring becomes visible.


Grid and Flexbox both have alignment properties that work on both axes, which is why they are the modern answer.


## How to fix it


### Grid with place-items


This is the shortest option and works for one child or several stacked children.


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


`place-items` is shorthand for `align-items` and `justify-items`. If you only want to centre one specific child, put `place-self: center` on that child instead.


### Flexbox with justify-content and align-items


Flexbox is a good choice when the parent also lays out other items in a row or column.


```css
.parent {
  display: flex;
  justify-content: center; /* main axis */
  align-items: center;     /* cross axis */
  min-height: 100vh;
}
```


In a row (the default), `justify-content` controls horizontal placement and `align-items` controls vertical placement. If you set `flex-direction: column`, the two swap roles.


### Auto margins inside a flex or grid container


Auto margins absorb free space on both axes inside a flex or grid parent, so this also centres the child:


```css
.parent {
  display: flex;
  min-height: 100vh;
}

.child {
  margin: auto;
}
```


This is handy when you want one child centred and you are not allowed to change the parent's alignment for its other children.


### Absolute positioning with inset and margin auto


For overlays such as a spinner on top of a card, absolute positioning still has its place. When the child has a known size, `inset: 0` plus `margin: auto` centres it:


```css
.card {
  position: relative;
}

.spinner {
  position: absolute;
  inset: 0;
  width: 48px;
  height: 48px;
  margin: auto;
}
```


If the size is unknown, use the older transform version:


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


### Horizontal only


If you only need horizontal centring of a block with a width, the classic approach is still fine:


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


For text and inline content inside a block, use `text-align: center` on the parent.


## Common pitfalls

- **The parent has no height.** Vertical centring has nothing to centre within. Add `min-height` to the parent.
- **Using** **`height: 100vh`** **on mobile.** The viewport unit can be taller than the visible area when the browser toolbar is showing. Consider `min-height: 100dvh` instead.
- **`text-align: center`** **on a block child.** It centres the text inside the block, not the block itself.
- **Mixing up axes in a column flex container.** With `flex-direction: column`, `justify-content` becomes vertical.
- **Translate blur.** The `translate(-50%, -50%)` trick can land on half pixels and make text look slightly soft. Grid or Flexbox avoids this.
- **Overflowing content.** If the child is taller than the parent, centring pushes part of it off the top where it cannot be scrolled to. Use `align-items: safe center` where supported, or fall back to `margin: auto` on the child, which never overflows the start edge.

## Related reading

- [MDN: place-items](https://developer.mozilla.org/en-US/docs/Web/CSS/place-items)
- [MDN: Box alignment in Flexbox](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_flexbox)
