Top 3 ways to center align a div using CSS

Prem Das
2 min readApr 27, 2021

--

Aligning a div to the center of its parent div might sound simple to many of us, but believe me, its a nightmare to so many people. So, I thought of writing an article to show how it is done. There are many ways to do it, but I will show you the top three ways.

Let’s get started. Let us suppose we have two divs — parent and child.

Let us assign some height, width and color to these divs.

After applying these styles, our end result will look something like this

Our job is align the yellow box at the center (both vertically and horizontally) of the violet box.

Let us try this in three different ways :-

Using Absolute positioning

Using Flexbox

Using Grid

All the above three ways positions the child div at the center of the parent div which looks like this :-

Yeah, it’s that simple.

I hope this won’t be a nightmare now. Thank you for reading this article. Do not forget to give it a clap.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Prem Das
Prem Das

No responses yet

Write a response