Design comparison
Solution retrospective
This was a very easy challenge as it only required HTML and CSS, that too very basic knowledge. However, I still struggled with centering the main div. I haven't overcome it properly but I did try a temporary solution. I took help from a few popular websites such as Stack Overflow and W3Schools. Other than that, it was fun coding this!
What specific areas of your project would you like help with?Centering the div. Whenever I use margin: auto or any other common solutions, it sits in the center but not exactly at the middle of the page. It's stuck to the top. That's what I need help with.
Community feedback
- @atefkhaled7Posted 8 months ago
Great job on completing your first project on Frontend Mentor! Itโs an exciting milestone, and I appreciate your effort. ๐
Now, letโs address the issue you faced with centering a <div>. Centering elements can be tricky, especially when youโre just starting out. Here are some suggestions to improve your code:
Centering Elements:
- Consider using Flexbox or Grid for more consistent results.
- For horizontal centering, use justify-content: center; (Flexbox) or place-items: center; (Grid).
- For vertical centering, use align-items: center; (Flexbox) or place-items: center; (Grid).
CSS Reset: Consider using a CSS reset to remove default browser styles. This ensures a consistent starting point for your styling. EX: *{ margin: 0; padding: 0; box-sizing: border-box; }
You should add it first thing in the CSS file using the universal selector *{ } OR add it to the body { }
Remember, practice makes perfect! Keep experimenting, learning, and building more projects. Happy coding! ๐
Marked as helpful1@meher-hexPosted 8 months ago@atefkhaled7 Thank you so much for your feedback! I've already used CSS Flexbox and added exactly what you suggested. Unfortunately, it never works for me or any other typical solutions. However, this is my first time hearing about CSS reset. Thank you for enlightening me on that. I'll make sure to use it on my next project.
1 - @danielmrz-devPosted 8 months ago
Hello, @meher-hex!
Your project is looking fantastic!
About your question:
Centering the div. Whenever I use margin: auto or any other common solutions, it sits in the center but not exactly at the middle of the page. It's stuck to the top. That's what I need help with.
- Using
margin
isn't always the most effective method for centering an element.
Here's a highly efficient approach to position an element at the center of the page both vertically and horizontally:
๐ Apply this CSS to the body (avoid using
position
ormargins
in order to work correctly):body { min-height: 100vh; display: flex; justify-content: center; align-items: center; }
I hope you find this helpful!
Keep up the excellent work!
Marked as helpful1@meher-hexPosted 8 months ago@danielmrz-dev Hey there! I have used the exact code you suggested except for the min-height property. I'll try using it in my next project. Thank you so much for your feedback!
1 - Using
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