SQLExcelHTML & CSSTouch typingBlogChoose a course
Lurndo › Blog › HTML & CSS
  1. Short answer
  2. How do you center a div horizontally and vertically?
  3. How do you center a div with CSS grid?
  4. How do you center a div horizontally with margin: 0 auto?
  5. How do you center vertically without flexbox or grid?
  6. How do you center a div with position: absolute?
  7. Why doesn't text-align: center center my div?
  8. Which way to center, and when?
  9. Why isn't my div centering?
  10. How do you get centering right every time?
  11. Frequently asked questions

How to center a div in CSS: six ways, and when to use each

Centering horizontally is easy enough; vertically is where it goes wrong. Here are the six ways that work today, when to pick each one, and a few food trucks for you to center yourself, right in this page.

C

Christophe, founder of Lurndo
November 4, 2026 · 10 min read

The easiest way to center a div in CSS is flexbox on the parent: display: flex, justify-content: center and align-items: center. The child then sits in the middle, both horizontally and vertically. With grid, place-items: center does the same, margin-inline: auto centers horizontally only, and since October 2026 align-content: center on a plain block is Baseline widely available in every major browser.

Exercise: right in your browser, on real data

Practise this: the lesson on The cross axis, free

Sources: 15 sources you can check

A wide paper picture frame shaped like a browser window, with a single coral paper square exactly in its center, held by four thin strings, and a small spirit level on top, on soft peach paper.

How do you center a div horizontally and vertically?

Make the parent a flex container and let it center its children: display: flex, justify-content: center and align-items: center. That's the shortest way to the middle on both axes, and it works with any content: one button, a card or a group of food trucks. The rules go on the parent, not on the div you want to move: putting them on the child is the first classic slip.

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}

Main axis: the direction flexbox lays out the children in, left to right by default. That's where justify-content works.

Cross axis: the axis across it, top to bottom. That's the one for align-items.

Watch that min-height. By default a block is only as tall as its content, so there's nothing to center vertically: the child already touches the top and the bottom. Give the parent a height or a min-height, and only then will you see the child drop to the middle. A missing height is the best-known reason why "vertical centering doesn't work".

Try it now. On the tall square of the Wheels & Meals food truck festival, the client in our flexbox module, three trucks stand against the left, stretched to the full height. The square is already 200 pixels tall, so there's plenty of room. After you answer, the check also runs your CSS on a square with one more truck and longer names, so a fixed margin won't get you there.

Try it: center the trucks

Use flexbox to put the trucks together in the middle of the square, across and in height.

1.square {2  display: flex;3}
1<link rel="stylesheet" href="style.css">2<main>3  <section class="square tall">4    <div class="truck chips">Chips</div>5    <div class="truck ice-cream">Ices</div>6    <div class="truck pizza">Pizza</div>7  </section>8</main>
1body {2  margin: 0;3  font-family: system-ui, sans-serif;4  background: #fff8ec;5  color: #2b2118;6}7header {8  padding: 12px 16px;9  background: #264653;10  color: #ffffff;11}12header h1 {13  margin: 0;14  font-size: 1.5rem;15}16nav a {17  padding: 6px 10px;18  border-radius: 999px;19  color: #ffffff;20  text-decoration: none;21}22main {23  padding: 0 16px 24px;24}25.slogan {26  margin: 16px 0 0;27}28.square {29  margin: 16px 0;30  padding: 12px;31  border-radius: 18px;32  background-color: #efe6d4;33  background-image: radial-gradient(rgb(110 90 60 / 0.16) 1px, transparent 1.5px);34  background-size: 14px 14px;35  box-shadow: inset 0 0 0 1px rgb(110 90 60 / 0.14);36}37.square.tall {38  height: 200px;39}40.square.narrow {41  max-width: 250px;42}43/* A real truck: the cab, awning and wheels are only for the look.44   The truck fits inside the box, so its nose never hides under the45   next truck, and it stretches with the box. */46.truck {47  position: relative;48  isolation: isolate;49  margin-bottom: 6px;50  padding: 16px 14px 12px;51  color: #ffffff;52  font-weight: 700;53}54.truck::before {55  content: "";56  position: absolute;57  z-index: -1;58  inset: -3px 0 -1px 0;59  border-style: solid;60  border-width: 32px 20px 12px 12px;61  border-image: url(truck-taco.png) 128 80 48 48 fill round stretch;62  filter: drop-shadow(0 3px 3px rgb(43 33 24 / 0.25));63}64.truck::after {65  content: "";66  position: absolute;67  z-index: -1;68  left: 12px;69  right: 4px;70  bottom: -3px;71  height: 12px;72  background: url(wiel.png) left / 12px no-repeat, url(wiel.png) right / 12px no-repeat;73}74.chips {75  color: #3a2a00;76}77.chips::before {78  border-image-source: url(truck-friet.png);79}80.taco::before {81  border-image-source: url(truck-taco.png);82}83.pizza::before {84  border-image-source: url(truck-pizza.png);85}86.waffle::before {87  border-image-source: url(truck-wafel.png);88}89.ice-cream {90  padding-top: 30px;91  padding-bottom: 26px;92  color: #4a1030;93}94.ice-cream::before {95  border-image-source: url(truck-ijs.png);96}97.curry {98  color: #3a1f00;99}100.curry::before {101  border-image-source: url(truck-curry.png);102}103.burger::before {104  border-image-source: url(truck-burger.png);105}106.soup::before {107  border-image-source: url(truck-soep.png);108}109.card {110  padding: 4px 16px;111  border-radius: 12px;112  background: #ffffff;113  box-shadow: 0 1px 2px rgb(43 33 24 / 0.08);114}115.card h2 {116  font-size: 1.15rem;117}118footer {119  padding: 12px 16px;120  background: #264653;121  color: #ffffff;122}
foodtruck.example

The page you just changed ran in your own browser. Lurndo checks it on what the browser makes of it: where every truck ends up, on that hidden square with four trucks too. So you don't have to copy our solution word for word. To see both axes first, with an inspector that draws them for you, start with display: flex and then justify-content.

How do you center a div with CSS grid?

Grid makes it even shorter: display: grid and place-items: center on the parent. place-items is shorthand for align-items and justify-items together, and it puts every child in the middle of its own cell. For a single child, like a message in an empty box or a logo on a tile, it's the shortest rule there is. place-items has worked in all major browsers since January 2020 (MDN).

.parent {
  display: grid;
  place-items: center;
  min-height: 300px;
}

Cell: the box a grid item sits in, between two row lines and two column lines.

With more than one child, things change. A grid without columns stacks its children, each in a row of its own, and those rows share the parent's height. Each child is then centered in its own row, not together with the others in the middle of the box. To keep the group together, use place-content: center or flexbox. Aligning in a cell shows the difference on a campsite full of tents.

On the web, flexbox is the clear favorite. According to Chrome Platform Status, 84.4% of page loads in Chrome used flexbox on November 3, 2026 (Chrome Platform Status), against 46.5% for grid (Chrome Platform Status). Grid has been growing fast, though: the Web Almanac found it on 2% of mobile pages in 2019 and on 12% by 2022 (Web Almanac).

Page loads in Chrome that use flexbox or grid
  1. Flexbox84.4%
  2. Grid46.5%

Source: Chrome Platform Status, November 3, 2026

Source: a share of all page loads in Chrome, not of all websites. It doesn't say what a page uses flexbox or grid for either.

How do you center a div horizontally with margin: 0 auto?

For a block that only needs to sit in the middle horizontally, like the main content of a page, you don't need flexbox. Give it a width or a max-width and set the left and right margins to auto: the browser splits the leftover space evenly between both sides. margin: 0 auto is the classic way to write it. margin-inline: auto is the modern one, and it has worked in all major browsers since April 2021 (MDN).

main {
  max-width: 48rem;
  margin-inline: auto;
}

margin-inline: the margin at the start and the end of a line. In English, that's left and right.

Tip: in a flex container, margin: auto on the child does center both ways. The automatic margins take up all the free space.

Is margin: 0 auto not working? Then the block almost always has no width. Without width or max-width, a block is as wide as its parent, and there's nothing left to share: auto becomes zero. Vertically, auto does nothing in a plain block, which is why this way only centers horizontally. The lesson on centering with margin auto shows the margins move in the box model as you type.

How do you center vertically without flexbox or grid?

With align-content: center, the newest way of the six. It now works on a plain block, with display: block, and its content moves to the middle vertically. Chrome and Edge 123, Safari 17.4 and Firefox 125 shipped it in spring 2024, and since October 16, 2026 it's Baseline widely available (webstatus.dev). A feature gets that label 30 months after the last major browser caught up (web.dev).

.message {
  min-height: 200px;
  align-content: center;
  text-align: center;
}

New: widely available since October 2026. If you have to support much older browsers, use flexbox.

Baseline: the label on MDN and web.dev for what works in Chrome, Edge, Firefox and Safari. Widely available means: for at least 30 months.

The upside: you don't touch the HTML, and the children stay ordinary blocks instead of flex items that shrink or stretch. When it reached all three browser engines in April 2024, Rachel Andrew wrote on web.dev that vertical centering, once close to impossible, had already been made easier by flexbox and grid, and that it now works in plain blocks too (web.dev).

Yet you rarely see it. In the centering guides we read for this article, in English and in Dutch, none says it's ready to use yet. Even MDN's centering recipe shows only flexbox and grid (MDN). So if you learn it now, you're a step ahead of most tutorials.

When each way to center worked everywhere
  1. March 2018Flexbox and transform become Baseline widely available
  2. January 2020place-items works in all major browsers
  3. April 2020Grid becomes Baseline widely available
  4. April 2021inset and margin-inline work in all major browsers
  5. April 2024align-content on a plain block works in all three browser engines
  6. October 2026align-content on a plain block becomes Baseline widely available

Source: webstatus.dev and MDN

Two kinds of dates: "works in all major browsers" is the day the last one caught up. Widely available comes 30 months later.

How do you center a div with position: absolute?

Sometimes something has to sit on top of everything else: a dialog over the page, a play button on a photo. Then you take it out of the flow with position: absolute, and set the parent to position: relative so the child positions itself against that parent. With inset: 0 and margin: auto it lands in the middle, as long as the child has a width and a height. inset has worked in all major browsers since April 2021 (MDN).

.photo {
  position: relative;
}
.play-button {
  position: absolute;
  inset: 0;
  width: 64px;
  height: 64px;
  margin: auto;
}

inset: shorthand for top, right, bottom and left. inset: 0 sets all four to zero.

Don't know the size? Use the older trick. top: 50% and left: 50% put the child's top-left corner in the middle, and transform: translate(-50%, -50%) pulls it back by half its own width and height. That transform has been Baseline widely available since March 2018 (webstatus.dev). You can practice how a child positions itself against its parent in relative and absolute.

.dialog {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

An absolute element no longer counts toward its parent's height. So the parent doesn't grow when the dialog gets bigger, and long text can spill out or slide over the rest of the page. That's why this is the way for things that float above the page, not for its ordinary content.

Why doesn't text-align: center center my div?

Because text-align doesn't move the block itself, only what's inside it. MDN puts it precisely: the property sets the horizontal alignment of "the inline-level content inside a block element" (MDN). Text, links, an <img> or a button with display: inline-block move to the middle. The div itself stays as wide as its parent and doesn't move an inch.

Inline: content that sits in a line like words in a sentence: text, links and, by default, images.

For text, though, text-align: center is the right choice: a heading, a button under a form, a row of icons. Put it on the block around the text. Want to center an image? You have two routes: leave it inline and put text-align: center on the parent, or turn it into a block with display: block and margin-inline: auto.

.heading {
  text-align: center;
}
.logo {
  display: block;
  margin-inline: auto;
}

Which way to center, and when?

There's no best way, but there is a best way for each case. The question is always the same: do you want to center in one direction or in two, is it one child or a group, and can the element stay in the normal flow of the page? This table puts the six ways side by side.

WayGoes onDirectionPick it for
Flexboxthe parentbotha group of items, and whenever in doubt
place-items: centerthe parent, as a gridbothone child in a box
margin-inline: autothe block itself, with a widthhorizontalthe main content of a page
align-content: centerthe parent, a plain blockverticaltext in a box, without flexbox
position: absolutethe child, with the parent relativebothsomething floating over the rest
text-align: centerthe block around the texthorizontaltext, links and images in a line

When in doubt, start with flexbox: it works for one child and for ten, and the children stay in the flow. Grid is handy once the parent is a grid anyway. To learn to read both, with puzzles that get a little harder each time, see flexbox and grid practice. Every property is on the HTML and CSS cheat sheet, with an example.

Why isn't my div centering?

Almost always because of one of these five mistakes. They look harmless, and CSS never shows an error: nothing happens at all, or it works on your screen and not on someone else's. Go through them one by one before you start doubting your whole stylesheet.

  1. The parent has no height. A block is as tall as its content. Without a height or min-height on the parent, there's nothing to center vertically, whichever way you use.
  2. `text-align` on the block. It moves the text inside the block, not the block itself. For the div, pick one of the other ways.
  3. `margin: auto` without a width. A block without width or max-width fills the whole line, so there's nothing left to share on either side.
  4. `absolute` without a positioned parent. Without position: relative on the parent, the child positions itself against an ancestor further up, or against the page itself. Then it may end up in the middle of the first screen instead of inside its own box.
  5. Fixed sizes that break with longer text. A margin-left: 300px or a top: 85px is right for one screen width and one text. With a longer title or one more item, it's all off. Let the browser do the math, with auto or center.

You're not the only one who trips over this. In the State of CSS 2025, a survey with 5,506 respondents, people writing in about what's hard in layout named grid most (272 times), but also height (94 times), positioning (63 times) and alignment (55 times) (State of CSS).

Source: free-text answers, not fixed choices. People who fill in a survey like this follow CSS closely, so it isn't a cross-section of all beginners.

How do you get centering right every time?

Centering doesn't stick because you learned six recipes by heart. It sticks once you've seen what each rule does. That's why the inspector in our lessons draws the flexbox axes and the grid lines under the page, and why we also check every solution on a variant with longer texts and one more item. Carry on with align-items or display: grid, or read the difference between HTML and CSS first. The first three modules are free.

Frequently asked questions

How do I center a div in CSS?

Put display: flex, justify-content: center and align-items: center on the parent. The child then sits in the middle, horizontally and vertically. Grid works too, with display: grid and place-items: center. Either way, give the parent a height, or there's nothing to center vertically.

Why is margin: 0 auto not working?

Almost always because the block has no width. Without width or max-width, a block is as wide as its parent, and there's no space left on either side to share. Or the element is inline, like a <span> or an <img>, and then auto just becomes zero. Make it a block first.

How do I center a div inside another div?

Put the centering rules on the outer div, the parent: display: flex, justify-content: center and align-items: center, plus a height. The inner div then sits in the middle both ways. To center it only horizontally, give the inner div a max-width and margin-inline: auto instead.

How do I vertically center text without flexbox?

Use align-content: center on the block around the text, and give that block a height. It has worked in Chrome, Edge, Firefox and Safari since 2024 and has been Baseline widely available since October 2026. For a single line, a line-height equal to the height works too, but that trick breaks once the text wraps onto two lines.

How do I center an image in CSS?

An image is inline by default, like a word in a sentence. So put text-align: center on the block around it, or make the image a block with display: block and margin-inline: auto. If it also needs to be centered vertically, use flexbox or grid on the parent, and give that parent a height.

Is align-content: center safe to use?

Yes, for everyday websites. It works in Chrome and Edge 123, Safari 17.4 and Firefox 125, all from spring 2024, and has been Baseline widely available since October 16, 2026. If you also have to support much older browsers, for example on a device that no longer updates, use flexbox instead.

Practise this in the course

Every lesson explains first and then lets you practise yourself, with a real browser in your browser.

  • The cross axisFlexbox · Align-items
  • CentringThe box model · Centring with margin auto
  • Aligning items in their cellGrid · Aligning in a cell
  • position, relative and absolutePositioning and responsive · Relative and absolute
Start HTML & CSS for free

Sources

  1. webstatus.dev, align-content in block layout (2026-10-16)
  2. web.dev (Rachel Andrew), align-content in block layout, in all three browser engines (2024-04-17)
  3. web.dev, Baseline
  4. webstatus.dev, Flexbox
  5. webstatus.dev, Grid
  6. webstatus.dev, 2D transforms
  7. MDN, place-items
  8. MDN, margin-inline
  9. MDN, inset
  10. MDN, text-align
  11. MDN, Center an element
  12. Chrome Platform Status, Feature popularity, flexbox (2026-11-03)
  13. Chrome Platform Status, Feature popularity, grid (2026-11-03)
  14. HTTP Archive, Web Almanac 2022, CSS (2022)
  15. State of CSS, State of CSS 2025, Layout (2025)
C

Christophe, founder of Lurndo

Christophe builds Lurndo from Belgium, with short, playful exercises for SQL, Excel, HTML & CSS and touch typing that show straight away what your answer does. On this blog, Christophe answers the questions people run into while learning, with sources you can check yourself.

Read next

  • HTML vs CSS: what's the difference, and how do they work together?Every web page has two layers, what's on it and how it looks. Here's how HTML and CSS split that work, and you'll write your first CSS rule right in this page.HTML & CSS · November 4, 2026 · 10 min read
  • How to calculate an average in Excel: blanks, zeros, conditions and weighted averagesIt's one function, yet averages go wrong all the time. See how AVERAGE works, what it skips, and how to average without zeros, with a condition or with weights, on real spreadsheets.Excel · November 5, 2026 · 11 min read

SQL

  • Topics
  • All lessons
  • Cheat sheet
  • Sandbox

Excel

  • Topics
  • All lessons
  • Cheat sheet
  • Shortcuts

HTML & CSS

  • Topics
  • All lessons
  • Cheat sheet

Touch typing

  • Topics
  • All lessons
  • Cheat sheet
  • Typing test

Lurndo

  • Dashboard
  • Leaderboard
  • Premium
  • Account
  • Blog
  • FAQ
  • Contact & support
  • Privacy
Nederlands