Design comparison
Solution retrospective
- It was somehow difficult for me to get the first white div box to the center.
- none
- I need to know a shotcuts on how to get less stylesheets css codes. That looks much for me.
Community feedback
- @francescomerighi1202Posted over 1 year ago
Hi there!
To perfectly center your .container set the following properties to body:
- 'display: flex'
- 'justify-content: center'
- 'align-items: center'
- 'height: 100vh'
Try and let me know!
Marked as helpful1 - @0xabdulkhaliqPosted over 1 year ago
Hello there 👋. Congratulations on successfully completing the challenge! 🎉
- I have other recommendations regarding your code that I believe will be of great interest to you.
HTML 🏷️:
- This solution lacks semantic markup, which causes lacking of landmark for a webpage and allows accessibility issues to screen readers, due to accessibility errors our website may not reach its intended audience, face legal consequences, and have poor search engine rankings, highlighting the importance of ensuring accessibility and avoiding errors.
- What is meant by landmark ?, They used to define major sections of your page instead of relying on generic elements like
<div>
or<span>
. They are use to provide a more precise detail of the structure of our webpage to the browser or screen readers
- For example:
- The
<main>
element should include all content directly related to the page's main idea, so there should only be one per page - The
<footer>
typically contains information about the author of the section, copyright data or links to related documents.
- The
- So fix it by replacing the
<div class="container">
element with the semantic element<main>
in yourindex.html
file to improve accessibility and organization of your page.
CSS 🎨:
- Let me explain, How you can easily center the component for better layout without usage of
absolute
positioning.
- We don't need to use
absolute
to center the component both horizontally & vertically. Because using `absolute' will not dynamical centers our component at all states
- To properly center the component in the page, you should use
Flexbox
orGrid
layout. You can read more about centering in CSS here 📚.
- For this demonstration we use css
Grid
to center the component
body { min-height: 100vh; display: grid; place-items: center; }
- Now remove these styles, after removing you can able to see the changes
.qr-main { position: absolute; top: 100px; right: 50px; left: 50px; margin: auto; }
- Now your component has been properly centered
.
I hope you find this helpful 😄 Above all, the solution you submitted is great !
Happy coding!
Marked as helpful0 - @Kmichael017Posted over 1 year ago
You could also use position: absolute; top: 50%; left: 50% transform: translate(-50% -50%);
0 - @taco-nekoPosted over 1 year ago
To center the div both vertically and horizontally, this is the absolute easiest method, in my opinion:
Add these properties to the body:
body { display: grid; place-content: center; }
Avoid using absolute positioning for this sort of stuff. In general, if you can achieve it without using absolute positioning, try to go with that route. Also, avoid setting a fixed height on elements unless they absolutely need it, because in a real-world scenario, if ever you add more text to the container, it will overflow at some point. If you leave it without a height, the container will adjust to the content you add.
Can you elaborate on what you mean by less stylesheets CSS codes? The only instance I can see of you writing a lot of code is where you used the absolute positioning to try and center the card: if you just replace it with the method I suggested, that'll remove the unnecessary lines of code. Otherwise, there's nothing that looks off about how much code you've written.
0@FeezlePosted over 1 year agoI have so many css codes like top, right, height etc. I think if there is a way to minimise having long content on one class and at the same time get the same result. thanks @taco-neko
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