DEV Community

Cover image for 3 Ways to CENTER a div in CSS
Code Oz
Code Oz

Posted on

106 29

3 Ways to CENTER a div in CSS

3 ways to center a div in HTML/CSS!!

With Position

/* Using positions */

.parent {
    position: relative;
}
.child {
    left: 50%;
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
}
Enter fullscreen mode Exit fullscreen mode
<div
     class="parent"
     style="background: blue; width: 500px; height: 250px;"
     >
    <div
         class="child"
         style="color: white;"
         >
      I'm center  
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode

With Flexbox

/* Using flexbox */
.container-flexbox {
    align-items: center;
    display: flex;
    justify-content: center;
}
Enter fullscreen mode Exit fullscreen mode
<div
     class="container-flexbox"
     style="background: green; width: 500px; height: 250px;"
     >
    <div
         style="color: white;"
         >
      I'm center  
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode

With Grid

/* Using Grid */

.container-grid {
    display: grid;
    place-content: center;
}

Enter fullscreen mode Exit fullscreen mode
<div
     class="container-flexbox"
     style="background: orange; width: 500px; height: 250px;"
     >
    <div
         style="color: white;"
         >
      I'm center  
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode


I hope you like this reading!

🎁 You can get my new book Underrated skills in javascript, make the difference for FREE if you follow me on Twitter and send message to me 😁 and SAVE 19$ πŸ’΅πŸ’΅

Or get it HERE

πŸ‡«πŸ‡·πŸ₯– For french developper you can check my YoutubeChannel

🎁 MY NEWSLETTER

β˜•οΈ You can SUPPORT MY WORKS πŸ™

πŸƒβ€β™‚οΈ You can follow me on πŸ‘‡

πŸ•Š Twitter : https://twitter.com/code__oz

πŸ‘¨β€πŸ’» Github: https://github.com/Code-Oz

And you can mark πŸ”– this article!

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (17)

Collapse
 
unclecheap profile image
UncleCHEAP β€’ β€’ Edited

Let's not forget yet a 4th way. No need for "positioning."

.parent {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.child {
    display: inline-block;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
codeoz profile image
Code Oz β€’

Didn't know it! Thanks you for sharing it!

Collapse
 
undqurek profile image
undqurek β€’ β€’ Edited

Good job!
Some additional solutions:
dirask.com/posts/CSS-center-child-...
dirask.com/posts/CSS-center-child-...

And as last one, composition of 10 approaches (I like it mostly):
dirask.com/posts/CSS-center-elemen...

Collapse
 
codeoz profile image
Code Oz β€’

very nice sharing thanks!

Collapse
 
bias profile image
Tobias Nickel β€’

good post,

your last example for the grid example, still uses the flex class container-flexbox in the html

Collapse
 
codeoz profile image
Code Oz β€’

thank you bro

Collapse
 
ezraguy profile image
Guy β€’

Nice post!
If I can add one thing is that in the second solution the
flex-direction: column is not really needed to center the div

Collapse
 
codeoz profile image
Code Oz β€’

nice! I remove it from code thanks!

Collapse
 
captflys profile image
CaptFlys β€’

Nice!

Collapse
 
shadowruge profile image
izaias β€’

wonderful, simple and straightforward

Collapse
 
codeoz profile image
Code Oz β€’

thank you a lot Izaias

Collapse
 
yevgeniyadanila profile image
Yevgeniya Danila β€’

i am learning CSS, thank you for the article

Collapse
 
codeoz profile image
Code Oz β€’

happy to help you :)

Collapse
 
thanhrossi profile image
Tran Thanh β€’
Collapse
 
latze profile image
latze β€’

Hey thanks for this!

But you can Also just center a div with a text-align: center;

Collapse
 
unclecheap profile image
UncleCHEAP β€’

Left-to-right center yeah, but that doesn't affect the relation to its parent vertically.

Collapse
 
codeoz profile image
Code Oz β€’

Totally agree!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay