
Short answer: make the parent a grid and use place-items: center. That centers the div horizontally and vertically in two lines. Flexbox works just as well with justify-content: center and align-items: center. To center a block horizontally only, give it a width and margin-inline: auto.
Below: every method, which one to pick, and why text-align and vertical-align so often seem to do nothing.
Center both ways with grid
The shortest way. place-items is a shorthand for align-items (vertical) and justify-items (horizontal). The parent needs a height, otherwise it’s only as tall as the child and there’s nothing to center in. More on grid in the CSS grid guide.
.parent {
display: grid;
place-items: center;
min-height: 300px;
}Center both ways with flexbox
Flexbox needs one line more. justify-content works along the main axis (horizontal in a row), align-items along the cross axis (vertical). With flex-direction: column the two swap. This is the better pick when you center a row of several items, like icons in a button. See CSS flexbox for the full property list.
.parent {
display: flex;
justify-content: center;
align-items: center;
min-height: 300px;
}Center horizontally with margin auto
The classic way for a block element. auto margins on the left and right split the free space equally. It only works when the element is narrower than its parent, so it needs a width or max-width. A block without a width fills the whole row, and there’s nothing to center.
In normal flow, margin: auto doesn’t center vertically: top and bottom auto margins become 0.
.container {
max-width: 800px;
margin-inline: auto; /* same as margin-left: auto; margin-right: auto; */
}margin: auto inside flex or grid
Inside a flex or grid container, auto margins work in both directions. One line on the child centers it in its parent.
This has a real advantage over align-items: center: when the child is taller than a scrollable parent, centering with align-items pushes its top out of the parent, where you can’t scroll to it. Auto margins drop to zero when there’s no free space, so the child starts at the top and stays fully scrollable. align-items: safe center does the same, but only in newer browsers (Safari 17.6 and later).
.parent {
display: flex;
min-height: 300px;
}
.child {
margin: auto;
}Center an absolutely positioned element
For overlays, badges and modals that sit on top of other content. The parent needs position: relative (or any non-static position). If the difference is fuzzy, read position absolute vs relative.
Option 1 works with unknown sizes: move the top-left corner to the middle, then pull the element back by half its own size with transform. Option 2 stretches the element’s box with inset: 0 and lets margin: auto center it; it needs a width and height. Both work for position: fixed too, centered in the viewport.
.parent {
position: relative;
}
/* Option 1: any size */
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Option 2: known size */
.child {
position: absolute;
inset: 0;
margin: auto;
width: 200px;
height: 100px;
}Center vertically without flex or grid
align-content: center now works on regular block containers too, not only in flex and grid. It centers the content vertically while the parent stays a normal block. It’s supported in Chrome 123+, Firefox 125+ and Safari 17.4+, so it’s safe for current browsers. Combine it with margin-inline: auto on the child to center both ways.
.parent {
min-height: 300px;
align-content: center;
}Why text-align: center doesn’t center the div
text-align centers inline content inside a block: text, links, images, inline-block elements. It doesn’t move a block-level child. If you put text-align: center on a parent, the text inside the child div gets centered, but the div itself stays on the left.
So: to center text, use text-align: center on the element that holds the text. To center the box, use one of the layout methods above.
Why vertical-align: middle does nothing
vertical-align only applies to inline and inline-block elements (aligning them within a line of text) and to table cells. On a regular div it has no effect. It’s useful for lining up an icon with the text next to it, not for centering a div in its parent. For that, use grid, flexbox, or align-content.
How to center an image
An <img> is inline by default, so margin: auto on it does nothing. Either center it like text from the parent, or make it a block first. display: block also removes the small gap below the image that inline images get from the text baseline.
/* Option 1: center as inline content */
.figure {
text-align: center;
}
/* Option 2: make it a block */
img.centered {
display: block;
margin-inline: auto;
}Which method should you use?
| You want to center | Use |
|---|---|
| One element, both ways | display: grid; place-items: center; |
| A row of items | Flexbox with justify-content and align-items |
| A page container, horizontally | max-width + margin-inline: auto |
| An overlay or modal | position: absolute or fixed + transform or inset: 0; margin: auto |
| Text or an image | text-align: center on the parent |
| Content that might overflow a scroll box | margin: auto on a flex or grid child |
The box alignment guide on MDN explains how the alignment properties behave in each layout mode.
FAQ
How do I center a div horizontally and vertically?
Put display: grid; place-items: center; on the parent and give the parent a height. Flexbox with justify-content: center and align-items: center does the same.
Why isn’t margin: 0 auto centering my div?
The div is probably as wide as its parent, which is the default for block elements. Give it a width or max-width. Also check it isn’t an inline element, like a <span> or an <img>.
How do I vertically align text in a div?
Use display: flex; align-items: center; or align-content: center on the div. vertical-align won’t do it on a block element.
What’s the difference between place-items and place-content?
place-items aligns each item inside its grid cell. place-content aligns the whole grid inside the container. With a single child, both center it.
How do I center a fixed modal on the screen?
Use position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);. If the modal has a known size, position: fixed; inset: 0; margin: auto; with a width and height also works.





