Centering a div is the CSS joke that never retires, but the punchline is out of date. Modern CSS has several one- or two-line ways to center anything, horizontally, vertically, or both. Most of the frustration left comes from one detail: picking a method that doesn't match what you're centering, or forgetting that the parent needs a height.
Here's every method worth knowing, when each one fits, and why "it isn't centering" usually has nothing to do with the centering code.
The Quick Answer
To center a child both horizontally and vertically inside its parent, put this on the parent:
.parent {
display: grid;
place-items: center;
min-height: 100vh; /* or any height you need */
}
That's it. The rest of this post covers the cases where something else fits better.
Horizontal Centering
Block element with a width: auto margins
.card {
max-width: 600px;
margin-inline: auto;
}
The oldest method still works. A block element with a width (or max-width) narrower than its container splits the leftover space between the left and right margins. margin-inline: auto is the logical-property version of margin: 0 auto, and it also behaves correctly in right-to-left layouts. Without a width, the element fills the container, so there's nothing to center.
Inline content: text-align
.button-row {
text-align: center;
}
text-align: center centers inline and inline-block children, such as text, links, images, and buttons. It doesn't affect block-level children. That's why it seems to work on a heading's text but not on the div below it.
Vertical and Horizontal Centering
Flexbox
.parent {
display: flex;
justify-content: center; /* main axis */
align-items: center; /* cross axis */
min-height: 300px;
}
Flexbox is the right choice when the container also lays out other items in a row or column, such as a navbar with a centered logo or a row of buttons. Keep in mind that justify-content follows the main axis, so if you switch to flex-direction: column the two properties swap roles. If the axes are hard to picture, the Flexbox Visualizer shows the result live as you change each property, and the Flexbox cheatsheet covers every property.
Grid
.parent {
display: grid;
place-items: center;
}
place-items is shorthand for align-items and justify-items. For a single child, this is the shortest reliable answer in CSS. You can also center just one item inside a larger grid with place-self: center on that item. The CSS Grid Generator is handy when the centered element sits inside a more complex layout, and the Grid cheatsheet covers the alignment properties in depth.
Block layout with align-content
.parent {
align-content: center;
min-height: 300px;
}
This one is newer. Since 2024, all major browsers support align-content on ordinary block containers, not just flex and grid. It centers the children vertically without changing the parent's display type, so you don't change how its children lay out. Check your audience's browser versions before relying on it for anything critical.
Absolute positioning with transform
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
This was the standard trick before flexbox, and it's still useful for overlays: a play button over a video thumbnail, a badge on an image, or a loading spinner over a card. top and left move the child's top-left corner to the center, and the transform shifts it back by half its own size. One side effect: when the child's size isn't a whole number of pixels, the transform can leave text slightly blurry.
Absolute positioning with inset and auto margins
.modal {
position: fixed;
inset: 0;
margin: auto;
width: min(90vw, 480px);
height: fit-content;
}
With all four offsets at zero and auto margins, the browser centers the element in both directions. It needs a defined width and height (fit-content works for the height), and it's a clean way to center a modal in the viewport without any transforms.
Which Method Should You Use?
- One child, centered in a box: Grid with
place-items: center - Several items in a row or column: Flexbox
- A page-width content column:
max-widthplusmargin-inline: auto - Text and buttons:
text-align: center - Something layered over something else: absolute positioning
- A modal or dialog: fixed position with
inset: 0andmargin: auto
If you're choosing between flexbox and grid for the whole layout, not just the centering, see CSS Grid vs Flexbox: when to use each.
Why Your Div Still Isn't Centered
The parent has no height. This causes most failed vertical centering. A div is only as tall as its content, so centering a child vertically inside a parent exactly that tall does nothing visible. Give the parent a min-height. For full-screen centering, min-height: 100vh works, though 100dvh handles mobile browser toolbars better.
The styles are on the wrong element. Flexbox and grid centering properties go on the parent. margin: auto goes on the child. Mixing them up does nothing and produces no error.
Another rule is winning. A more specific selector elsewhere may override your display or margin. Inspect the element in DevTools and look for struck-through declarations. CSS specificity explained covers why the other rule wins.
The element is full width. It may be centered already, but you can't tell because it spans the whole container. Add a temporary outline: 1px solid red to see its real edges.
Once you know which element gets which property, centering stops being a joke and becomes one or two lines you type without thinking.