Originally published on CodingCush.
Yes, you can center a div in CSS. The simplest reliable way is to make the parent a flex container with display: flex, justify-content: center and align-items: center. This article tries that and four other solid methods on the same small profile card.
<div class="card"> <div class="avatar">L</div> <h2>Lavkush Kumar</h2> <p>Full stack developer who builds websites, games and tutorials.</p> <button type="button">Say Hello</button> </div> That is our card: a circle with the letter L, a name, one line of text and a button. With no centering yet, the browser parks it in the top left corner. Every method below starts from this card. Only the CSS changes.
Meet the Card Before We Move It
The card needs its own styling, which stays the same through all five methods: a 300 pixel wide card with soft corners and a gentle shadow, an 84 pixel teal avatar circle with a white L, and simple sizing rules for the name, paragraph and button.
.card { width: 300px; padding: 28px 24px; background: #ffffff; border: 1px solid #dfe4e8; border-radius: 14px; text-align: center; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.10); } .avatar { width: 84px; height: 84px; margin: 0 auto 14px; border-radius: 50%; background: #0f766e; color: #ffffff; font-size: 36px; font-weight: bold; line-height: 84px; } Spot one detail: margin: 0 auto 14px in the avatar rule. The auto margins split the spare space evenly, so the circle sits in the middle of the card, a preview of method 3. If you are still sorting out which language does what, our guide to HTML vs CSS vs JavaScript and what each one does explains it in plain words.
Method 1: Flexbox, the One to Learn First
Flexbox is the method most developers reach for. Put three lines on the parent, here the body:
body { margin: 0; min-height: 100vh; display: flex; justify-content: center; align-items: center; } margin: 0 removes the browser's default page gap, and min-height: 100vh makes the body as tall as the window, because vertical centering needs a parent tall enough to have a middle. display: flex turns the body into a flex container, then justify-content: center and align-items: center center the card horizontally and vertically. I checked this in a real browser: at 1280 by 800 the card starts 490 pixels from the left and 256 from the top.
Use Flexbox when centering one item, or a row of items, inside a parent. The MDN guide to centering an element shows the same recipe as its first example, and our CSS Flexbox explained with examples guide walks through it step by step.
Method 2: Grid, One Line Does the Job
Grid is the newer layout system in CSS, and for pure centering it is even shorter:
body { margin: 0; min-height: 100vh; display: grid; place-items: center; } display: grid turns the body into a grid container, and place-items: center centers the item both ways in one go. The card looks identical.
Use Grid when the section is already a grid, or when you plan to center several items, like a gallery of cards. For a single card it is a tie with Flexbox. Our guide to CSS Grid explained with examples shows how grid rows and columns work.
The MDN guide to centring an element with CSS.
Method 3: Margin Auto, the Classic Horizontal Fix
This is the oldest method on the list, and it still works for one job: centering a block element left to right.
.card { width: 300px; margin: 0 auto; } When a block element is narrower than its parent, the browser has spare space on the sides. Setting the left and right margins to auto splits that space equally, which puts the card in the middle. It now sits in the horizontal center, but it stays at the top.
Use it when you only need horizontal centering, like a blog article column or a form. The element must be a block element with a set width. Without a width, the div stretches to fill the parent and there is no spare space to split.
Method 4: Absolute Positioning Plus Transform
This method is two steps: pull the card to the middle of the page, then nudge it back by half its own size so its center sits on the middle point.
.card { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } position: absolute takes the card out of the normal flow. top: 50% and left: 50% move its top left corner to the exact middle of its positioned parent, the page itself when no closer positioned ancestor exists. Now the card sits too far down and right, so transform: translate(-50%, -50%) shifts it back by half its own size. The percentages refer to the card itself, so this works at any card size.
Use this method to center something on top of other content, like a popup or a loading spinner. It is not for normal page layout, because an absolutely positioned element stops taking up space, so content below it can slide underneath.
Method 5: Text Align, for Inline Content Inside the Card
The last method does not center the card at all. It centers the things inside the card:
.card { text-align: center; } text-align: center centers inline content, like text, images and a button, inside their parent. That is why the name, paragraph and button line up neatly in the middle of the card. It works on the card's children, not on the card itself, so putting it on the body moves nothing, because the card is a block element.
Use it inside a card, a header or a footer whenever you want a heading or a line of text centered in its box. It is the simplest method of the five, and the most misunderstood, because beginners try it on the wrong element. Remember: text-align centers what is inside, Flexbox and Grid center the box itself.
When the Card Refuses to Move
Almost every stuck card is one of four mistakes. First, the parent has no height: if you use Flexbox or Grid and the parent is only as tall as the card, there is no middle to center in, so add min-height: 100vh to the parent. Second, you centered the wrong element: for Flexbox and Grid, the rules go on the parent, not on the card. Third, margin: auto did nothing because the card has no width. Fourth, the browser's default 8 pixel body margin throws the card off by a few pixels.
One more check: open the browser developer tools and look at the box model, which shows the card's real width, padding and margins. The MDN guide to aligning items is worth a bookmark too.
Questions Beginners Actually Ask
Why is my div not centering even though I used margin auto?
Almost always because the div has no set width. Without one, the div fills its parent, so there is no spare space for the auto margins to split. Add width: 300px and keep margin: 0 auto.
Should I use Flexbox or Grid to center a div?
For one item, either works and the result looks the same. Pick Flexbox for a row or column layout, and Grid if the section is already built with grid. Learn Flexbox first.
How do I center a div both horizontally and vertically?
Use Flexbox with justify-content: center and align-items: center on the parent, or Grid with place-items: center. Both need a tall parent, so add min-height: 100vh.
Does text align center work on a div?
No, not on the div itself. text-align: center centers the inline content inside an element, like text and images, but it does not move a block element. To move the div, use one of the four methods above.
Which Method When
| Method | Centers | Best for | Watch out for |
|---|---|---|---|
| Flexbox | Both ways | One item, or a row of items | Parent needs height for vertical centering |
| Grid | Both ways | Sections already built with grid | Overkill for one row |
| Margin auto | Horizontal only | Article columns and forms | Needs a width, horizontal only |
| Absolute plus transform | Both ways | Popups, modals and overlays | Leaves the page flow |
| Text align | Content inside | Headings, text and buttons in a card | Does not move the block itself |
Your Turn: Two Small Challenges
First, center the card vertically as well using only margin: auto on the card and Flexbox on the body, then remove the Flexbox lines and get the same result with Grid. Second, add a teammate card beside the first one, with a different letter and name, and center the pair with gap: 20px on the body.
A Short Ending
Centering a div feels hard only while the methods are a mixed up list in your head. Once each one is tied to a job, the choice gets easy: Flexbox for most days, Grid when the layout is already a grid, margin auto for a horizontal fix, absolute positioning for overlays, and text align for the words inside.

