DEV Community

Discussion on: Centring an element on a page

Collapse
 
davey profile image
Davey

Thanks for that Jordan. I’ve been looking at Flexbox but it’s taking a while for me to fully understand it. What you’ve done certainly looks easier so I'll give it a try. Thanks :).

Collapse
 
jwkicklighter profile image
Jordan Kicklighter

No problem! Feel free to send me a message if you have any questions. Like I mentioned, that froggy game does an excellent job of showing how each piece works individually and then has you put them together into more complex layouts.

Collapse
 
trostcodes profile image
Alex Trost

I can’t agree with Jordan more. The absolute positioning methods explained here don’t allow for changes in screen or content size. Please use flex box and Grid to center things.

Thread Thread
 
davey profile image
Davey

Thanks for the feedback Alex. I’m fairly new to CSS so thanks for explaining :)

Collapse
 
davey profile image
Davey

Using FlexBox, a 3rd box can be centered on my example above with code like:

<div class="center-container">
    <div class="centered">I am a centered div using FlexBox</div>
</div>
.center-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.centered {
    width: 600px;
    height: 500px;
    background-color: aquamarine;
}

Thanks everyone for the advice.

Thread Thread
 
jwkicklighter profile image
Jordan Kicklighter

You bet, keep up the good work!