DEV Community

Cover image for Different ways to center a div
Zaid Hassan
Zaid Hassan

Posted on • Edited on

Different ways to center a div

Different ways to center a div demonstrates how we can center a div using different css properties. It discusses use of 'flex box', 'css grid' and a third way which is not very conventional, i.e by using 'position'.

To center a div using Flex.

Centering a div using flex box is really simple. Here is how.

<div class="flex-container">
    <div class="item"></div>
</div>
Enter fullscreen mode Exit fullscreen mode
.flex-container {
    display: flex;
    justify-content: center; //center item horizontally
    align-items: center; //center item vertically
    height: 100vh;
}
.item {
    height: 100px;
    width: 100px;
    background: black;
}
Enter fullscreen mode Exit fullscreen mode

Note: You need to give the container a height to center the item vertically.

To center a div using Grid.

Centering a div using grid is even simpler. Here is how.

<div class="grid-container">
    <div class="item"></div>
</div>
Enter fullscreen mode Exit fullscreen mode
.grid-container {
    display: flex;
    place-items: center; // center's item horizontally and vertically
    height: 100vh;
}
.item {
    height: 100px;
    width: 100px;
    background: black;
}
Enter fullscreen mode Exit fullscreen mode

To center a div using Position.

Centering a div using position is not something you will normally do. Here is how to center a div using `position: absolute;`.

<div class="container">
    <div class="item"></div>
</div>
Enter fullscreen mode Exit fullscreen mode
.container {
    position: relative;
    height: 100vh;
}
.item {
    position: absolute;
    height: 100px;
    width: 100px;
    background: black;
}
Enter fullscreen mode Exit fullscreen mode
const container = document.querySelector('.container');
const item = document.querySelector('.item');


// clientHeight/clientWidth gives us the height/width of the element in pixels

item.style.top = (container.clientHeight/2) - (item.clientHeight/2) + 'px';

item.style.left = (container.clientWidth/2) - (item.clientWidth/2) + 'px';
Enter fullscreen mode Exit fullscreen mode

Follow me on X to join me in my tech journey.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (4)

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo) • Edited

Hey! Pretty cool post!

Just a little tip, you can highlight your code by placing the name of the language after the initial 3 backticks for code blocks, e.g.

```css
/*Your CSS code here*/
```

Collapse
 
thezaidhassan profile image
Zaid Hassan

Hey! Thanks for pointing it out.

Collapse
 
devdufutur profile image
Rudy Nappée

You can center a div with absolute position in pure CSS :

.item {
/* ... */
top: 50%;
left: 50%;
translate: 50% 50%;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
thezaidhassan profile image
Zaid Hassan • Edited

Yes we can do that, but this one was more fun 😅, i am learning JavaScript, so i was just experimenting with the possibility.

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay