
How to Center a Div in CSS: The Guide You Will Google Again Next Week
Some things never change. AI agents can refactor whole services now. And somewhere, right now, a developer is searching how to center a div in CSS. I have done it too, more times than I would like to admit.
So let me save you a search for today. Here is the answer:
.parent {
display: grid;
place-items: center;
}
You are welcome. Bookmark it, because you will need it again next week. And since you are here, let us go a little deeper, so you know which method to pick and why centering sometimes refuses to work.
Four ways to center a div
Method 1: CSS Grid with place-items
This is my default today. Two lines on the parent, and the child sits in the middle both ways.
.parent {
display: grid;
place-items: center;
min-height: 100vh;
}
place-items is a shorthand for align-items and justify-items. It centers every child inside its grid area. Use it when you have one child, or when you want each item centered in its own cell.
Method 2: Flexbox
Flexbox is the classic answer, and it is still great, especially when the parent is already a flex container.
.parent {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
justify-content works along the main axis (horizontal by default). align-items works along the cross axis. If you change flex-direction to column, they swap roles. That swap is behind a lot of "why is this not centering?" moments.
Use flexbox when you have several items in a row or column and want them grouped in the middle.
Method 3: Margin auto for horizontal centering
If you only need to center a block horizontally, you do not need grid or flexbox at all.
.child {
width: 600px;
margin: 0 auto;
}
This only works when the element has a width smaller than its parent. It is perfect for page containers and article bodies. It does not center vertically.
Method 4: Absolute positioning with transform
Sometimes the element must sit on top of other content, like a modal or a badge. Then absolute positioning is the right tool.
.parent { position: relative; }
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
The top and left move the child's top left corner to the center. The transform then pulls it back by half its own size. Modern CSS also offers inset: 0; margin: auto; for elements with a fixed size.
Which method should you use?
A simple rule of thumb:
- One child, center both ways: grid with
place-items: center. - A group of items in a row or column: flexbox.
- Only horizontal, fixed width block:
margin: 0 auto. - Overlay on top of other content: absolute positioning.
The same ideas in Tailwind CSS
Many React and Next.js projects use Tailwind, so here are the same patterns as utility classes:
- Grid:
grid place-items-center min-h-screenon the parent. - Flexbox:
flex items-center justify-centeron the parent. - Horizontal only:
mx-autoplus a width such asmax-w-3xlon the child. - Overlay:
absolute inset-0 m-autoon a child with a fixed size.
The class names change, but the layout rules underneath are exactly the same. If you understand the CSS, the utilities make sense on their own.
Why your div still is not centered
When you try to center a div in CSS and nothing moves, it is usually one of these:
- The parent has no height. Vertical centering needs space. A parent that is only as tall as its child has nothing to center within. Add
min-height. - The styles are on the wrong element. Grid and flex centering go on the parent, not the child.
- The flex direction changed. In a column,
justify-contentis vertical, not horizontal. - Text is centered, not the box.
text-align: centercenters inline content, not the block itself. - Something else is overriding you. Check the browser dev tools for crossed out rules.
The bigger lesson
Centering is a running joke, but it points to something real. CSS rewards understanding layout models, not memorising snippets. Once you understand how grid and flexbox think about space, centering stops being a search and becomes a habit.
If you want to strengthen those foundations, my article on front-end patterns, principles and architectures is a good next read, along with these JavaScript tips. For teams building larger React apps, I also offer React front-end architecture help, and you can always get in touch.
What is the thing you always end up googling?
First shared on LinkedIn.
Subscribe to my newsletter
Read articles from Tech With Asim | Learn, Build, Lead directly inside your inbox. Subscribe to the newsletter, and don't miss out.
More Articles

Spec-Driven Development: Why Specs Are Back in the Age of AI Agents
With AI agents doing more of the implementation, the quality of your spec decides the quality of your output. Here is how to practise spec-driven development well.

AI Coding Prompts: Always Tell the Model What Must Not Change
Technically correct, the best kind of correct. AI does exactly what you ask, so good AI coding prompts must say what must not change, not only what to improve.

5 Habits of Great Engineers That Matter More Than Coding Speed
The best engineers I have worked with were not always the fastest coders. Here are the habits of great engineers that turn skill into trust.
Comments