How to center a div with CSS
Centering a div is easy once you know which box to style. Here are the five reliable ways, from grid and flexbox to margin auto, each with a short code sample and when to use it.

You've built a small sign-in box, and you want it right in the middle of the page. You add text-align: center, and the words inside move, but the box stays stuck on the left. Here's how to center a div every way that works, and when to use each one.
To center a div, put display: grid and place-items: center on its parent, and the div sits in the middle both ways. Flexbox works too: use display: flex, justify-content: center, and align-items: center. To center it only from side to side, give the div a width and margin: 0 auto. A div is a plain HTML box used to group things, and every method below moves that box.
Why is centering a div tricky?
Think of putting a vase in the middle of a table. You don't measure the vase. You look at the table and find its middle. CSS works the same way: most centering is set on the parent, the box that holds the div.
Two more things trip people up. A div is as wide as its parent unless you give it a width, so there's no room to move it sideways. And a parent is only as tall as what's inside it, so there's no spare room to center anything up and down.
How do you center a div with flexbox?
Every example below uses this HTML: a parent box with one card inside.
<div class="parent">
<div class="card">Sign in</div>
</div>
.parent {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
The first line, display: flex, turns the parent into a flex container. That's a box that lines up its children in a row. Then justify-content: center centers them along the row, which is side to side. And align-items: center centers them up and down.
The last line, min-height: 100vh, makes the parent at least as tall as the browser window. One vh is 1% of the window's height. Without it, the parent is only as tall as the card, and there's nothing to center in.
How do you center a div with grid?
.parent {
display: grid;
place-items: center;
min-height: 100vh;
}
Here, display: grid turns the parent into a grid container. The place-items property is a shorthand, one property that sets two others. It sets align-items for up and down, and justify-items for side to side. With center, the card sits in the exact middle. This is the shortest way to center a single box.
How do you center a div horizontally?
.card {
width: 300px;
margin: 0 auto;
}
Margin is the empty space outside a box. The value 0 auto means no margin on the top and bottom, and auto on the left and right. The browser splits the leftover space equally between the two sides, so the card lands in the middle.
This only works with a width. Without one, the div stretches to fill its parent, and there's no space left to split. A max-width works too, and it lets the box shrink on small screens. It's a simple way to center a page's main column of text.
In a normal page, margin: 0 auto won't center up and down. Inside a flex or grid parent, though, margin: auto on the child centers it both ways.
How do you center text inside a div?
.card {
text-align: center;
}
The text-align property centers the inline content inside a box. Inline content is anything that flows along a line, like words in a sentence. That includes text, links, images, and elements set to display: inline-block. It never moves the box itself. That's why it moved the words but not the sign-in box at the start.
How do you center a div on top of other content?
.parent {
position: relative;
}
.card {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Setting position: absolute lifts the card out of the normal flow, the way boxes stack down the page by default. It no longer takes up space. The card is placed against the nearest parent that has a position. That's why the parent gets position: relative.
Next, top: 50% and left: 50% put the card's top-left corner at the parent's center. Then translate(-50%, -50%) moves the card back by half its own width and half its own height. Now its center sits on the parent's center.
Use this for things that float over other content, like a play button on a video or a spinner over a card. The parent won't grow to fit the card, so give the parent its own size.

Which way should you use?
| What you're centering | Best way | The CSS |
|---|---|---|
| One box, both ways | Grid | display: grid; place-items: center; on the parent |
| A row of items, both ways | Flexbox | display: flex; justify-content: center; align-items: center; on the parent |
| A page column, side to side | Margin auto | max-width: 700px; margin: 0 auto; on the box |
| Text, links, or images in a box | text-align | text-align: center; on the box |
| A box on top of other content | Absolute and transform | position: absolute; plus top, left, and transform on the box |
If you're not sure, start with grid for one box and flexbox for several. Both keep the box in the normal flow, so the page grows around it.
Why is my div not centering?
- The parent has no spare height. Flexbox and grid can only center up and down when the parent is taller than the child. Give the parent a
min-height. - The CSS is on the wrong box. Flexbox and grid properties go on the parent, not on the div you want to move.
- Using
margin: 0 autowith no width. A div without a width fills its parent, so it has nowhere to move. - Using
text-alignon a box. It centers what's inside a box, never the box. - Using
vertical-align: middleon a div. It only works on inline boxes and table cells, so a normal div ignores it. - An absolute box with no positioned parent. Without
position: relativeon the parent, the box is placed against the page instead.
Where to go next
Centering gets easier once you know how a box gets its size. The CSS box model shows how padding and borders change a box's width. Flexbox vs grid explains which layout fits beyond centering. New to all this? Start with HTML vs CSS vs JavaScript.
You'll also see margin-inline: auto. It sets the margins at the start and end of each line, which are left and right in English. Right-to-left layouts explains these start and end properties. The transform trick moves a box without shifting anything around it. That same idea makes animations smooth, as compositor animations shows.
Try each method in the code editor by changing one line at a time. To center things from code, read styles and classes in the JavaScript course, or styling components in React. MDN's center an element recipe has live versions of the flexbox and grid methods to play with.


