Skip to content
article

How to center a div: 5 methods, ranked by how much they made me cry

Plain English for the code, Pidgin for the pain

Kelechi Nnaji@abeg_compile

July 18, 2026 · 4 min read

Everybody laughs at "how do I center a div" until they are the one at 11pm with a client on WhatsApp asking why the logo is "small small shifting to the left". I have been that person. So this is the post I wish I had when I started. The code is real and it works. The tears are also real.

5. position: absolute + transform (the old faithful)

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

Works everywhere, even in the browser your client's uncle is still using. But the child is taken out of the normal flow, so the parent no longer knows how tall it is. If the parent has no height, everything collapses and you are staring at nothing. The transform can also make text a bit blurry on some screens when it lands on half pixels.

Cry level: 😭😭😭😭. E don cast for me many times.

4. margin: 0 auto (horizontal only)

.child {
  width: 600px; /* or max-width */
  margin: 0 auto;
}

Only centers horizontally, and only for a block element that has a width. No width, no centering, because a block element already takes the full width and there is no free space to share. This one confused me for a whole week in 2021.

Cry level: 😭😭😭. Na small small tears.

3. text-align: center (for inline things)

.parent { text-align: center; }

Centers inline and inline-block children like text, images and buttons. Does nothing for a normal div child, and it also centers all the text inside, which the designer did not ask for. Then you add text-align: left on the child to undo it. Then you question your life.

Cry level: 😭😭.

2. Flexbox

.parent {
  display: flex;
  justify-content: center; /* main axis */
  align-items: center;     /* cross axis */
  min-height: 100vh;       /* the parent needs height to center vertically */
}

This is what I use most of the time. The one thing that catches everybody: justify-content follows the main axis, so if you set flex-direction: column, the two properties swap jobs. Vertical becomes justify, horizontal becomes align. Na there plenty people dey fall.

Also, if the parent has no height, vertical centering "works" but you cannot see it, because the parent is exactly as tall as the child. Give it a min-height.

Cry level: 😭. Only when I forget the column thing.

1. Grid with place-items

.parent {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

Two lines. Centers both ways. place-items is shorthand for align-items plus justify-items. If you have one child to center, this is the cleanest thing in CSS. I showed my senior and he said "since when?" and I felt like Arsenal lifting a trophy.

Cry level: 0. Pure joy.

Bonus: margin auto inside flex or grid

.parent { display: flex; min-height: 300px; }
.child { margin: auto; }

Inside a flex or grid container, margin: auto eats all the free space on every side, so the child sits in the middle. Nice when you only control the child's CSS.

The real reason your div is not centered

  • The parent has no height. I keep saying it because it is the answer most of the time
  • You centered the wrong element. Open DevTools, hover the elements and see which box is actually wide
  • Something else has a fixed width, or a margin from a CSS reset you forgot about
  • The image inside has extra space below it because images are inline by default. display: block on the img fixes that

That is all. Go and center your div. If it still no gree, post the code here, not a screenshot, and we go look am together.

Written by

Kelechi Nnaji

Frontend dev & certified meme distributor. Enugu. My code no dey work but my memes dey work 😂

Get the best of #meme_driven_dev weekly

One email each Monday with the most useful posts. No spam; unsubscribe in one click.

Comments

15

Sign in to join the conversation.

Sign in to comment
Sam Whitaker@segfault_sam#1 in Meme Driven Development
ranking grid place-items at cry level 0 is the most optimistic thing ive read this year
7 net votes
Kelechi Nnaji@abeg_compile#3 in Meme Driven Development
allow me this one joy sam. Safari has taken everything else from me
4 net votes
Sayan Mukherjee@bong_bugs#4 in Meme Driven Development
as QA i tested this on safari. method 1 works. method 5 works. abeg's confidence: 404 🐟
5 net votes
Kelechi Nnaji@abeg_compile#3 in Meme Driven Development
you this QA man, na you dey always find wahala 😂
7 net votes
Oluwatobi Adeyemi@tobi_freelance6 rep in Meme Driven Development
Kelechi you this boy. The client WhatsApp at 11pm part is too real. Mine sends voice notes 😂
10 net votes
Kelechi Nnaji@abeg_compile#3 in Meme Driven Development
voice note is violence. sorry oga
4 net votes
Frank Ostrowski@frank_cpp98#2 in Meme Driven Development
Five methods to put a box in the middle of another box. In 1994 we had one method. It was called arithmetic. Regards.
10 net votes
Kelechi Nnaji@abeg_compile#3 in Meme Driven Development
sir in 1994 the screen had one size. my client has 4 phones, a tablet and a smart fridge
4 net votes
Sam Whitaker@segfault_sam#1 in Meme Driven Development
frank the day someone asks you to center text on a 320px android with the font size set to large by their grandma you will find religion
3 net votes
Frank Ostrowski@frank_cpp98#2 in Meme Driven Development
I have found nothing except that I was right. Regards.
5 net votes
Giulia Bellandi@giulia_designs2 rep in Meme Driven Development
Designer here, thank you for the "parent has no height" section. Half the "can you center this" tickets that come back to me are exactly that. And please use min-height, not height, or the content overflows on small screens.
9 net votes
Karthik Subramaniam@karthi_rustacean6 rep in Meme Driven Development
Nice post. Grid place-items is what I also use for simple cases. The column axis swap catches backend people like me only.
3 net votes
Kuya Migs@kuya_migs_codes#8 in Meme Driven Development
Solid 'to lods 🔥 yung flex-direction column na nag-swap ang justify at align is THE number one question sa comments ng videos ko. Sharing this sa mga students ko
8 net votes
Rizal Pratama@rgb_rizal2 rep in Meme Driven Development
the bonus margin auto in flex is underrated. I use it to push the last nav item to the right also: margin-left: auto
9 net votes
Kelechi Nnaji@abeg_compile#3 in Meme Driven Development
yes!! that one is magic. one property and the logout button flies to the corner
5 net votes