Design comparison
Solution retrospective
Hi everyone! I am not convinced of my solution!
-
In order to be able to center the .container div both horizontally and vertically using Flexbox, I set min-height: 100vh on the body. It was a shot in the dark which seems to be working. Could someone remind me of the role of height in relation to flexbox? I've not fully grasped it.
-
I gave that same .container div margin: 100px auto as I also wanted free space at the top and bottom. Is that correct or is there another way?
-
I didn't set a width on the body as it didn't seem to make much difference. Should I have done?
Many thanks!
Community feedback
- @MarjanZivkovicPosted over 1 year ago
Hi @SLAKS97! Congrats on completing the project! Shots in the dark sometimes hit the target :) Just kidding!
Yes, that is one of the ways to center a container on the page, setting the body as a flex container.
align-items: center
will center your container vertically (by default). The height of your body is dictated by the height of your container. In this case, your container is high enough so you can even omitmin-height: 100vh
on the body, it'll be the same. You want to usemin-height: 100vh
on the body when your container is less high. In this way, it'll be perfectly centered vertically in the middle of the screen.Horizontally you'll use
justify-content: center
(by default) to center your container on the page. In your case, it's not necessary since you're havingmargin: 0 auto
on the container, which does the same thing.You can even remove
display: flex
andalign-items: center
andmin-height: 100vh
on body and you'll get the same result (try it out).As for the width, you don't have to set the
width: 100%
(especially not in px). By default, body, divs, and all the other block elements already have the width 100%, meaning all the available width of the screen.Hope some of this makes sense. Good luck!
Marked as helpful0@Slaks97Posted over 1 year agoHi @MarjanZivkovic!
It all makes perfect sense :)
I've applied all that you suggested and my code looks a lot cleaner for it, so thanks very much!
1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord