Using tansform, padding, justify and align
Design comparison
Solution retrospective
Just starting to code, trying to center everything was kind of difficult.
Community feedback
- @devsotterPosted over 1 year ago
Hi, to center a div, follow these steps: Within the semantic tag 'main', create a parent div called '.background' or any other desired name. Then, set the background of this div as the container for your box, with the dimensions of the screen. Use 'height: 100vh' to obtain the total height of the viewport and allow space for the content to be vertically centered. Use 'width: 100%' to obtain the total width of the screen.
<main> <div class="background"> <div class="box"> </div> </div>After that, apply the flexbox properties in the class .background to center the content: 'display: flex; justify-content: center; align-items: center;'.
Finally, remove the 'height: 100%' property from the '.box' class to prevent it from inheriting the total viewport size. With these changes, your content will be perfectly centered. There is also the option to center content using grids, so I recommend doing some further research on that topic.
Marked as helpful0
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