Design comparison
Solution retrospective
What is the best practice to center elements horizontally and vertically?
Community feedback
- @git-riteshPosted over 1 year ago
I often use
display: flex;
,align-items: center;
&justify-content: center;
to center things from both sides i.e. horizontally & vertically.Though if I have to center a
<div>
only on the main axis then I usemargin: 0 auto;
to center but it should have awidth
for that to be happen as by default any<div>
element isdisplay: block;
and it would take fullwidth
in the parent container.2 - @TANJIRWEBDEVPosted over 1 year ago
display: grid; place-content: center; thats it ! ez
2@git-riteshPosted over 1 year agoDoes this center the content both vertically & horizontally using a single property
place-content: center;
Really it's cool! @TANJIRWEBDEV0 - @Code-BeakerPosted over 1 year ago
Hi there!
Congratulations on completing this project! 🎉
Here is the code that's commonly used for centering content horizontally and vertically:
.element { display: flex; justify-content: center; align-items: center; }
Hope you find this helpful!
1 - @mohammed-sarhad-ahmedPosted over 1 year ago
display:flex; justify-content:center; align-items:center or display:grid; place-content:center;
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