HTML Semantic, CSS

Solution retrospective
What is the best practice to center elements horizontally and vertically?
Please log in to post a comment
Log in with GitHubCommunity feedback
- @git-ritesh
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. - @TANJIRWEBDEV
display: grid; place-content: center; thats it ! ez
- @Code-Beaker
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!
- @mohammed-sarhad-ahmed
display:flex; justify-content:center; align-items:center or display:grid; place-content:center;
Join 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