How to Center a Div — Simplified Solutions for Developers

Anurag Joshi
4 min readApr 26, 2024

--

Hey there, fellow web developer!

Have you ever found yourself scratching your head over how to center a div?

Don’t worry, you’re not alone! In fact, it’s one of the most frequently asked questions in web development interviews.

Whether you’re just starting out or you’ve been coding for years, mastering the various methods of centering a div is key to creating beautifully balanced layouts.

In this guide, we’ll break down div centering into simple steps.

We’ll explore four different techniques, breaking down each method step by step. By the end of this journey, you’ll be equipped with the knowledge and confidence to tackle this common challenge with ease.

Let’s get started and make div centering simple!

Starter Code

Let’s begin with our starter code:

<section>
<div>
<p>Center Me!</p>
</div>
</section>
section {
width: 400px;
height: 400px;
border: 1px solid blue;
}

div {
max-width: fit-content;
background-color: pink;
border: 1px solid red;
padding: 20px;
}

This is what it looks like on the browser.

Currently, the div is aligned to the top-left corner of the section. Our objective is to center the div both horizontally and vertically within the section.

To achieve our goal, we’ll employ some CSS techniques to center the div within its parent section.

This is what we want to achieve.

Method 1 — Using TABLE and MARGIN

In this method, we utilize CSS tables to center the div both horizontally and vertically within its parent container.

We’ll add the following CSS to the starter code:

section {
display: table-cell;
vertical-align: middle;
}

div {
margin: 0 auto;
}

By setting the parent container (section) to display: table-cell, we establish a table layout context. Then, vertical-align: middle ensures vertical alignment. Additionally, by applying margin: 0 auto to the child div (div), we achieve horizontal centering.

This method is reliable and widely supported across browsers.

Method 2 — Using POSITION and TRANSFORM

This approach utilizes absolute positioning and the transform property to center the div.

We’ll add the following CSS to the starter code:

section {
position: relative;
}

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

By setting the parent container (section) to position: relative, we establish a positioning context for the child div (div). Then, by applying position: absolute, along with top: 50% and left: 50%, we move the div to the center of its parent. Finally, using transform: translate(-50%, -50%), we adjust the div's position to perfectly center it both horizontally and vertically.

Method 3 — Using FLEXBOX

One simple way to center the div is by using Flexbox. Flexbox provides a powerful and flexible way to layout elements within a container.

We’ll add the following CSS to the starter code:

section {
display: flex;
justify-content: center;
align-items: center;
}

By setting the parent container (section) to display: flex and applying justify-content: center and align-items: center, we effortlessly center the child div (div) both horizontally and vertically.

Flexbox is a modern approach to layout and is well-supported across modern browsers.

Method 4 — Using GRID

CSS Grid is another modern layout method that offers precise control over the positioning of elements.

We’ll add the following CSS to the starter code:

section {
display: grid;
place-content: center;
}

By setting the parent container (section) to display: grid and using the shorthand place-content: center, we center the child div (div) within its parent container.

Alternatively, you can achieve the same effect by using justify-content: center; and align-items: center; separately.

CSS Grid provides a robust and intuitive way to create complex layouts, making it a valuable tool in a developer's toolkit.

Conclusion

In wrapping up, we’ve covered four simple ways to center a div on a webpage. Whether you like the straightforwardness of Flexbox, the precise control of CSS Grid, or other methods, there’s an option that fits your style. By grasping these techniques, you’ll be better at crafting neat and balanced layouts for your websites.

Keep experimenting to find what works best for you. With a bit of practice, you’ll nail div centering like a pro!

Happy reading 🚀

--

--

Anurag Joshi

Digital marketer turned Front-End Developer sharing coding concepts, tips, and more with the community ❤️ #JavaScript #React #NextJs #CSS #HTML