Learn CSS Visually - CSS explained one diagram at a time - flexbox, grid, the box model and more

How Do I Center a Div? Every Way, With Pictures

By · Updated

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.

Six panels showing six ways to center a div with CSS: a grid parent with place-items center, a flex parent with justify-content and align-items center, a block with a width and margin 0 auto centered left to right only, a block parent with align-content center centered top to bottom only, an absolutely positioned box with inset 0 and margin auto, and an absolutely positioned box moved with top and left 50% plus translate -50% -50%.
Six ways to center a div. The dashed box is the parent; in methods 1, 2 and 4 the code goes on the parent, in 3, 5 and 6 it goes on the div itself.

What the diagram shows, in plain text:

  1. Grid — on the parent: display: grid; place-items: center; Centers both ways.
  2. Flexbox — on the parent: display: flex; justify-content: center; align-items: center; Centers both ways.
  3. margin: auto — on the div: width: 60%; margin: 0 auto; Centers left to right only; the two side margins come out equal.
  4. align-content — on a plain block parent: align-content: center; Centers top to bottom only; the child block still fills the width.
  5. Absolute + inset — on the div: position: absolute; inset: 0; margin: auto; plus a width and height. Centers both ways, out of the normal flow.
  6. 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:

What margin: 0 auto does in a 600px parent — worked out by Learn CSS Visually with the CSS 2.1 width rules, November 13, 2026
Div widthSpare widthLeft marginRight marginLooks centered?
60% = 360px240px120px120pxyes
200px400px200px200pxyes
max-width: 450px150px75px75pxyes
auto (no width set)0px0px0pxno — 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:

  1. 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-width or width: fit-content.
  2. The element is inline. A span, an a or an img is inline by default, and auto side margins don’t center inline boxes. Add display: block, or put text-align: center on the parent instead.
  3. It’s floated or absolutely positioned. float takes the box out of the normal centering rules; for absolute boxes, see the inset method below.
  4. 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. So margin: auto on 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.

Two-step diagram drawn to scale: in a 600 by 300 pixel parent, top 50% and left 50% put the top-left corner of a 200 by 100 pixel div at the point 300, 150, so the box hangs down and to the right of center; then translate -50% -50% moves the div back 100 pixels and up 50 pixels, so its corner lands at 200, 100 and its center matches the parent's center.
Step 1 centers the corner; step 2 centers the box. Percentages in top and left use the parent’s size, percentages in translate use the div’s own size.
.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:

top/left 50% + translate -50% -50% for a 200 x 100px div in a 600 x 300px parent — worked out by Learn CSS Visually from the CSS Positioned Layout and CSS Transforms rules, November 13, 2026
StepMeasured onHorizontalVerticalDiv’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
Checkgaps around the div200 + 200 + 200 = 600px100 + 100 + 100 = 300pxequal 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:

  1. Right box? place-items, justify-content, align-items and align-content go on the parent. margin: auto, translate and inset go on the div.
  2. 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.
  3. Div has a width (for margin: 0 auto)? A full-width block has no spare room to split.
  4. Direct child? Flex and grid alignment only move the parent’s direct children, not grandchildren.
  5. Column direction? In flex-direction: column, justify-content is the vertical one.
  6. 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:

Seven ways to center a div compared — compiled by Learn CSS Visually from the W3C Box Alignment, Flexbox, Positioned Layout, Transforms and CSS 2.1 specifications and MDN browser-compat-data, November 13, 2026
MethodCode goes onCentersDiv needs a set size?Stays in normal flow?Works since (Chrome / Edge / Firefox / Safari)
display: grid; place-items: centerparentboth waysnoyes59 / 79 / 45 / 11
display: flex + justify-content / align-items: centerparentboth waysnoyes29 / 12 / 20 / 9
display: flex + margin: auto on the childbothboth waysnoyes29 / 12 / 20 / 9
margin: 0 autodivleft-right onlyyes (a width)yesevery browser (CSS 2.1)
align-content: center on a blockparentup-down onlynoyes123 / 123 / 125 / 17.4
position: absolute; inset: 0; margin: autodivboth waysyes (or fit-content)no87 / 87 / 66 / 14.1 (for inset)
top: 50%; left: 50% + translate: -50% -50%divboth waysnono104 / 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-items from 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-content in block layout
Chrome 123, Edge 123, Firefox 125, Safari 17.4
place-self on 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

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.

How this was written: Written from the W3C CSS Box Alignment Module Level 3 (Working Draft, 8 October 2026), CSS 2.1 (visual formatting model details), the CSS Positioned Layout Module Level 3 (Working Draft, 7 October 2025), the CSS Flexible Box Layout Module Level 1 (Candidate Recommendation Draft, 14 October 2025), CSS Transforms Module Level 1, the HTML Standard’s rendering section, MDN Web Docs’ Center an element recipe, Adam Argyle’s Centering in CSS on web.dev, MDN’s browser-compat-data and the Can I use support data (updated November 11, 2026), all read on November 13, 2026. The method matrix and the pixel math are our own work, done by applying the specifications’ rules to each example. Code samples are simplified for learning.

Oh, one more thing…

If this helped, you’ll like our free updates: the insights we discover while researching, and the moments worth sharing with other people learning CSS — no spam, no promotions.

One short email when there’s something worth your time. Unsubscribe any time.