Design comparison
Solution retrospective
Vertically center a div is really hard
Community feedback
- @quintin156Posted about 2 years ago
I know one simple way to center a div. Checkout https://web.dev/patterns/layout/ This website has taught me so many layouts just with simple css
(parent container)
.container { display: grid; place-items: center: height: 100vh; }
(children container)
.card { / properties for your card / }
Marked as helpful1 - @correlucasPosted about 2 years ago
👾Hi Sajeel Ahmad, congrats on completing this challenge!
The approach you've used to center this card vertically is not the best way, because using margins you don't have much control over the component when it scales. My suggestion is that you do this alignment with
flexbox
using the body as a reference for the container.The first thing you need to do is to remove the margins used to align it, then apply
min-height: 100vh
to make the body height size becomes 100% of the screen height, this way you make sure that whatever the situation the child element (the container) align the body withdisplay: flex
andalign-items: center
/justify-items: center
.body { min-height: 100vh; background-color: var(--light_gray); font-family: 'Nunito Sans', sans-serif; font-size: 15px; display: flex; justify-content: center; align-self: center; display: flex; align-items: center; } } .card { display: flex; border-radius: 20px; background-color: var(--white); flex-direction: column; padding: 1em; padding-bottom: 1.5em; align-items: center; /* position: absolute; */ /* top: 0; */ /* bottom: 0; */ /* left: 0; */ /* right: 0; */ /* margin: auto; */ width: 250px; height: 400px; }
✌️ I hope this helps you and happy coding!
1@correlucasPosted about 2 years ago@sajeell You're welcome Sajeel! Then say me if worked for you =)
0@sajeellPosted about 2 years ago@correlucas This really worked. You can check the code. Hurrah :fire:
0
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