Design comparison
Solution retrospective
I struggled to space the image & details 50/50 on a bigger screen. Struggled with centering my main on the page but figured it out (Would really like to know a better way to do it). How to center my main on the mobile device to get rid of the horizontal scrolling. Any advice on how to better my code, thanks.
Appreciate the feedback & help :).
Community feedback
- @apah-devPosted over 1 year ago
I've struggled with centering items myself for a while but now i know many options you could use. I'll share them here. You could try them all out on a test project and then apply them on real projects when you understand them.
container { margin-left: auto; margin-right: auto; width: 400px; }
If you want to use margin and make it work you have to specify width if not it doesn’t work *There’s no way to vertically align it to the centre. That’s the disadvantage of this option *
container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
- the -50%, -50% represent x and y axis *
Using Flexbox method:
main { display: flex; justify-content: center; align-items: center; height: 100vh; }
Using the grid method:
main { display: grid; align-items: center; height: 100vh; justify-content: center; }
this is far from exhaustive as the methods you can use are many. I do advice you try them out and figure out the one that's best for the particular project you're working on. also consider how it would work for the responsiveness of your page. Hope this helps
Marked as helpful0 - @orphandeityPosted over 1 year ago
you can center your content by giving the containing element (the body in your case) a height and some flex-box properties like so...
body { min-height: 100vh; display: flex; justify-content: center; align-items: center; }
you could use grid instead like this...
body { min-height: 100vh; display: grid; place-content: center; }
to space the content within main evenly you can also take advantage of flex-box...
main: { display: flex; } .img, .details { flex: 1; }
Marked as helpful0 - @wheels63Posted over 1 year ago
to center the main container horizontally and vertically smack in the middle:
main { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
some kind stranger told me about this. this will position the main container smack in the middle of the viewport.
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