How Do I Center a Div? Every Way, With Pictures
Disclosure: this page contains Amazon affiliate links. As an Amazon Associate we earn from qualifying purchases, at no extra cost to you.
Hey there, middle-maker!
How to center a div is the question every single person learning CSS types into a search box at some point, and the honest good news is that it now takes two lines, as long as you put them on the right box.
There are six good ways to do it in modern CSS, and each one is just two or three lines once you see which box gets the code.
If you like a printed reference on the desk while you practise, the CSS Visual Dictionary paperback on Amazon draws the layout properties as pictures too.
Short answer: To center a div both ways, make its parent a grid with display: grid; place-items: center; — two lines on the parent. Flexbox works the same with justify-content: center and align-items: center. To center only left to right, give the div a width and margin: 0 auto. The parent needs a height to center vertically.
What the diagram shows, in plain text:
- Grid — on the parent:
display: grid; place-items: center;Centers both ways. - Flexbox — on the parent:
display: flex; justify-content: center; align-items: center;Centers both ways. - margin: auto — on the div:
width: 60%; margin: 0 auto;Centers left to right only; the two side margins come out equal. - align-content — on a plain block parent:
align-content: center;Centers top to bottom only; the child block still fills the width. - Absolute + inset — on the div:
position: absolute; inset: 0; margin: auto;plus a width and height. Centers both ways, out of the normal flow. - Absolute + translate — on the div:
top: 50%; left: 50%; translate: -50% -50%;Centers both ways, at any size.
What is the easiest way to center a div?
The easiest way to center a div is to turn its parent into a grid and set place-items: center. That’s the whole recipe, and it centers the div horizontally and vertically at once, whatever size the div is.
.parent {
display: grid;
place-items: center;
min-height: 300px; /* room to center in */
}
place-items is a shorthand. The W3C CSS Box Alignment Module Level 3 says it sets both the align-items and justify-items properties in a single declaration
, and when you give one value, the second is copied from the first value
.
So place-items: center means align-items: center (up and down) plus justify-items: center (left and right). MDN Web Docs’ Center an element recipe uses exactly this. If grid lines and tracks are new to you, our visual guide to CSS Grid draws them.
What’s the difference between place-items and place-content?
place-items: center centers each item inside its own grid cell, while place-content: center centers the whole group of tracks inside the parent. With one div, both look the same.
With several children, the difference shows: in a grid, place-items: center stacks the children in rows that share the parent’s height, while place-content: center packs them together in the middle.
Adam Argyle’s Centering in CSS on web.dev calls the place-content version Content Center
and points out its one catch: The widest child (max-content) sets the width for all the rest.
How do you center a div with flexbox?
To center a div with flexbox, give the parent display: flex, justify-content: center and align-items: center. justify-content centers along the row (left to right), and align-items centers across it (top to bottom).
.parent {
display: flex;
justify-content: center; /* left-right, in a row */
align-items: center; /* up-down, in a row */
min-height: 300px;
}
Flexbox is the better pick when you center several things in a line — an icon and a label, or a row of buttons — because they stay side by side.
One thing to watch: in flex-direction: column the two properties swap jobs, so justify-content moves things up and down. Our justify-content vs align-items guide draws that swap, and our flexbox explainer covers the main and cross axes.
Can margin: auto center a flex item?
Yes — inside a flex container, margin: auto on the child centers it both ways, with no alignment properties on the parent.
The W3C CSS Flexible Box Layout spec explains why: any positive free space is distributed to auto margins in that dimension
, so the four auto margins split the spare room evenly.
.parent { display: flex; min-height: 300px; }
.child { margin: auto; }
It also has a handy safety feature.
The same spec says Overflowing boxes ignore their auto margins and overflow in the end direction
— so if the child grows taller than the parent, it starts at the top instead of spilling off both edges. web.dev’s Centering in CSS calls this trick Autobot
.
How do you center a div horizontally with margin: 0 auto?
To center a div horizontally, give it a width smaller than its parent and set margin: 0 auto (or margin-inline: auto). It’s the oldest centering trick in CSS, and it still works everywhere. The CSS 2.1 specification puts it in one sentence:
If both ‘margin-left’ and ‘margin-right’ are ‘auto’, their used values are equal. This horizontally centers the element with respect to the edges of the containing block.
.card {
width: 60%; /* or max-width: 40rem */
margin: 0 auto; /* 0 top and bottom, auto left and right */
}
The browser takes the spare width and splits it in half. We did the math for a 600px-wide parent:
| Div width | Spare width | Left margin | Right margin | Looks centered? |
|---|---|---|---|---|
60% = 360px | 240px | 120px | 120px | yes |
200px | 400px | 200px | 200px | yes |
max-width: 450px | 150px | 75px | 75px | yes |
auto (no width set) | 0px | 0px | 0px | no — the div fills the row, so there’s nothing to center |
Why doesn’t margin: 0 auto work?
margin: 0 auto usually fails for one of four reasons, and the first one is by far the most common:
- The div has no width. A block without a width fills its parent, so the auto margins come out as 0 (the last row of the table). Add
width,max-widthorwidth: fit-content. - The element is inline. A
span, anaor animgis inline by default, and auto side margins don’t center inline boxes. Adddisplay: block, or puttext-align: centeron the parent instead. - It’s floated or absolutely positioned.
floattakes the box out of the normal centering rules; for absolute boxes, see the inset method below. - You expected it to center vertically. In normal block layout, CSS 2.1 says:
If ‘margin-top’, or ‘margin-bottom’ are ‘auto’, their used value is 0.
Somargin: autoon a plain block only ever centers left to right.
Can you center a div vertically without flexbox or grid?
Yes — in current browsers, align-content: center on a plain block parent centers its contents vertically, with no flexbox or grid at all.
The W3C Box Alignment spec lists block containers among the boxes align-content applies to, and in block layout it moves The entire contents of the block, as a unit
along the block (vertical) axis.
.parent {
min-height: 300px;
align-content: center; /* no display change needed */
}
Two details from the spec are worth knowing. First, justify-content Does not apply to and has no effect on block containers
, so for left-to-right centering you still add margin-inline: auto to the child (with a width) or text-align: center for text.
Second, the alignment is “safe” by default here: if the content is taller than the parent, it starts at the top instead of being cut off at both ends.
This is the newest method on the page. According to MDN’s browser-compat-data, read on November 13, 2026, align-content works in block layout from Chrome 123, Edge 123, Firefox 125 and Safari 17.4 — all from 2024.
Older browsers simply ignore it and leave the content at the top.
How do you center a div with position: absolute?
To center an absolutely positioned div, give the parent position: relative, then either move the div with top: 50%; left: 50%; translate: -50% -50%, or stretch its box with inset: 0 and let margin: auto center it.
Use this for things that sit on top of other content — an overlay, a play button on a video, a badge, a loading spinner — because an absolute box leaves the normal flow and doesn’t push anything around.
web.dev’s Centering in CSS calls it Pop & Plop
: the absolute positioning pops the element out of normal flow
, and you plop
it on top of other UI.
How does translate(-50%, -50%) center a div?
translate(-50%, -50%) centers a div because its percentages are measured on the div itself, while top and left percentages are measured on the parent.
top: 50%; left: 50% puts the div’s top-left corner at the parent’s center; the translate then pulls the div back by half its own width and half its own height.
.parent { position: relative; }
.badge {
position: absolute;
top: 50%;
left: 50%;
translate: -50% -50%; /* newer property */
/* transform: translate(-50%, -50%); the classic version */
}
Here is the same math as the diagram, step by step:
| Step | Measured on | Horizontal | Vertical | Div’s top-left corner |
|---|---|---|---|---|
top: 50%; left: 50% | the parent (600 x 300px) | +300px | +150px | (300, 150) — off-center |
translate: -50% -50% | the div itself (200 x 100px) | -100px | -50px | (200, 100) — centered |
| Check | gaps around the div | 200 + 200 + 200 = 600px | 100 + 100 + 100 = 300px | equal gaps on both sides |
The rule behind it comes from CSS Transforms Module Level 1: translate percentages are relative to the dimension of this reference box
, and for a normal HTML element that reference box is the element’s own border box.
That’s why the trick works at any size — you never need to know the div’s width or height.
How does inset: 0 with margin: auto center a div?
inset: 0 with margin: auto centers an absolutely positioned div by stretching its “slot” to the whole parent and splitting the spare room into equal auto margins.
inset: 0 is shorthand for top: 0; right: 0; bottom: 0; left: 0. The W3C CSS Positioned Layout Module Level 3 says the leftover space is divided among any auto margins in the relevant axis
.
.parent { position: relative; }
.modal {
position: absolute;
inset: 0;
margin: auto;
width: 320px; /* the div needs a size, */
height: fit-content; /* or it stretches to fill */
}
The catch: the div needs a width and height (or fit-content), or it stretches to fill the parent and there’s nothing left to center.
Fun fact: browsers use this exact method for the modal <dialog> element — the HTML Standard’s rendering section gives every dialog width: fit-content; height: fit-content; margin: auto and sets a modal dialog’s insets to 0, which is why showModal() opens it in the middle of the screen.
Newer browsers also let you write place-self: center on an absolutely positioned box with inset: 0, with no auto margins. MDN’s browser-compat-data lists that from Chrome 122, Edge 122, Firefox 134 and Safari 26, so keep margin: auto if you need older Safari versions.
How do you center a div in the middle of the page?
To center a div in the middle of the page, make the wrapper at least as tall as the screen with min-height: 100vh, then center inside it with grid.
Little confession: the translate trick looked like pure magic to me until I drew it out. Once you see that top and left measure the parent while translate measures the div, the -50% stops being a spell and starts being plain arithmetic.
Without that height, the wrapper is only as tall as the div, so there’s no room to center vertically.
body { margin: 0; } /* drop the default margin */
.page {
min-height: 100vh; /* as tall as the screen */
display: grid;
place-items: center;
}
Removing the body’s small default margin stops 100vh plus that margin from adding a scrollbar.
For a box that stays in the middle of the screen while the page scrolls (a cookie banner or a popup), use position: fixed; inset: 0; display: grid; place-items: center; on a full-screen layer and put the box inside it.
Or use a real <dialog>, which the browser centers for you.
How do you center the text inside a div?
To center the text inside a div, use text-align: center on the div.
text-align centers the lines of text and any inline things (links, icons, inline images) inside the box — but it doesn’t move the box itself, which is why it can’t center a div.
.banner {
text-align: center; /* text left-right */
display: grid;
place-content: center; /* the text block up-down */
min-height: 200px;
}
For vertical centering of the text, combine it with any of the both-ways methods above; the grid version here is the shortest.
Why won’t my div center?
When a div won’t center, the code is usually on the wrong box or the parent has no spare room. Run down these six checks in order:
- Right box?
place-items,justify-content,align-itemsandalign-contentgo on the parent.margin: auto,translateandinsetgo on the div. - Parent has a height? A parent with no height is exactly as tall as its content, so vertical centering has nothing to do. Add
min-height. - Div has a width (for
margin: 0 auto)? A full-width block has no spare room to split. - Direct child? Flex and grid alignment only move the parent’s direct children, not grandchildren.
- Column direction? In
flex-direction: column,justify-contentis the vertical one. - Absolute box? Check that the parent has
position: relative— otherwise the div centers in the next positioned ancestor, often the whole page.
The fastest way to see what’s going on: open DevTools, hover the parent, and look at its size. In Chrome, the flexbox editor in Chrome DevTools lets you click center buttons and watch the div move.
Which way of centering a div should you use?
For most jobs, use display: grid; place-items: center on the parent; use flexbox when you center several items in a row, margin: 0 auto for a centered page column, and absolute positioning only for overlays.
We compared all seven methods on what they need and what they do:
| Method | Code goes on | Centers | Div needs a set size? | Stays in normal flow? | Works since (Chrome / Edge / Firefox / Safari) |
|---|---|---|---|---|---|
display: grid; place-items: center | parent | both ways | no | yes | 59 / 79 / 45 / 11 |
display: flex + justify-content / align-items: center | parent | both ways | no | yes | 29 / 12 / 20 / 9 |
display: flex + margin: auto on the child | both | both ways | no | yes | 29 / 12 / 20 / 9 |
margin: 0 auto | div | left-right only | yes (a width) | yes | every browser (CSS 2.1) |
align-content: center on a block | parent | up-down only | no | yes | 123 / 123 / 125 / 17.4 |
position: absolute; inset: 0; margin: auto | div | both ways | yes (or fit-content) | no | 87 / 87 / 66 / 14.1 (for inset) |
top: 50%; left: 50% + translate: -50% -50% | div | both ways | no | no | 104 / 104 / 72 / 14.1 (the translate property; transform: translate() works everywhere) |
If you’re torn between the first two rows, our flexbox vs Grid guide has a three-question test. Adam Argyle’s web.dev article opens with a line every CSS learner will recognise:
Centering in CSS is a notorious challenge, fraught with jokes and mockery.
With the table above, the joke is mostly over: pick the row that matches your job, copy two lines, done.
Do these centering methods work in every browser?
Yes — grid and flexbox centering work in every current browser, and only align-content on block layout and place-self on absolute boxes are recent enough to check.
The support numbers, from MDN’s browser-compat-data and Can I use (data updated November 11, 2026), read on November 13, 2026:
- CSS Grid
- 97.39% of users worldwide;
place-itemsfrom Chrome 59, Edge 79, Firefox 45, Safari 11 - Flexbox
- 97.4% of users worldwide
- 2D transforms (
transform: translate()) - 97.57% of users worldwide
inset- Chrome 87, Edge 87, Firefox 66, Safari 14.1
align-contentin block layout- Chrome 123, Edge 123, Firefox 125, Safari 17.4
place-selfon absolutely positioned boxes- Chrome 122, Edge 122, Firefox 134, Safari 26
Grab an empty div, give its parent a height and display: grid, add place-items: center, and enjoy the tiny thrill of a box landing dead center on the first try. Then try the other five on the same box — you will know exactly which one to reach for next time.
Sources and further reading
- W3C: CSS Box Alignment Module Level 3 — Working Draft, 8 October 2026: place-items, place-content, align-content on block containers
- W3C: CSS 2.1, visual formatting model details — equal auto margins center a block; auto top/bottom margins are 0
- W3C: CSS Flexible Box Layout Module Level 1, auto margins — Candidate Recommendation Draft, 14 October 2025
- W3C: CSS Positioned Layout Module Level 3 — Working Draft, 7 October 2025: inset, auto margins of absolutely positioned boxes
- W3C: CSS Transforms Module Level 1 — translate percentages use the element’s own border box
- WHATWG HTML Standard: rendering — the dialog element’s default centering styles
- MDN Web Docs: Center an element — flexbox and grid recipes
- web.dev: Centering in CSS, by Adam Argyle — five techniques stress-tested
- MDN browser-compat-data — browser versions, read November 13, 2026
- Can I use: CSS Grid Layout — support data updated November 11, 2026; flexbox and 2D transforms from the same data file
Questions and answers
How do I center a div horizontally and vertically?
Put display: grid; place-items: center; on the parent and give the parent a height, such as min-height: 300px. Flexbox works too: display: flex; justify-content: center; align-items: center; on the parent.
Why is margin: auto not centering my div vertically?
In normal block layout, auto top and bottom margins count as 0 — CSS 2.1 says so directly — so margin: auto only centers left to right. Auto margins center vertically only inside a flex or grid container, or on an absolutely positioned box with inset: 0.
How do you center a div inside another div?
Treat the outer div as the parent: give it display: grid; place-items: center; and a height, and the inner div sits in the middle. To center only horizontally, give the inner div a width and margin: 0 auto instead.
Is place-items: center the same as justify-content and align-items center?
Not exactly. place-items: center sets align-items and justify-items, which center each grid item in its own cell. In flexbox, justify-items is ignored, so a flex container needs justify-content: center for the main axis.
How do I center an image in a div?
An image is inline by default, so the quickest fix is text-align: center on the div. Or make the image a block with display: block; margin: 0 auto;. To center it both ways, use display: grid; place-items: center; on the div.