Centering Elements using Transform: Translate()
Design comparison
Solution retrospective
Hey!
Do you know a better way to center elements on a page than the one I used?
I explained how I did it in the README.md
file.
Thanks!
Community feedback
- @MelvinAguilarPosted almost 2 years ago
Hi there π. Good job on completing the challenge ! I have some feedback for you if you want to improve your code.
HTML:
- Use the
<main>
tag to wrap all the main content of the page instead of the<div>
tag. With this semantic element you can improve the accessibility of your page.
- The
alt
attribute should not contain the words "image", "photo", or "picture", because the image tag already conveys that information.
If you want to learn more about the
alt
attribute, you can read this article.CSS:
- Instead of using pixels in font-size, use relative units like
em
orrem
. The font-size in absolute units like pixels does not scale with the user's browser settings. This can cause accessibility issues for users who have set their browser to use a larger font size. You can read more about this here.
- Centering an element with
position: absolute
would make your element behave strangely on some screen sizes, "there's a chance the content will grow to overflow the parent". You can use Flexbox or Grid to center your element. You can read more about centering in CSS here.
Using flexbox layout:
body { min-height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; }
Using grid layout:
body { min-height: 100vh; display: grid; place-content: center; }
- You should use a CSS reset to remove the default browser styles and make your page look the same in all browsers.
Popular CSS resets:
I hope you find it useful! π Above all, the solution you submitted is great!
Happy coding and Happy New Year! πππ
Marked as helpful2@Pedro-CelestePosted almost 2 years ago@MelvinAguilar Hey!
I was planning to write reply after I went through all your recommendations but since I'm realizing it's going to take a lot of time to learn all of these things, it's best to write it right now haha.
Thank youso much for this insightful piece of feedback. The article on relative units you sent me is amazing!!
1 - Use the
- @ShuvalovrusPosted almost 2 years ago
In this case I would not bother, you can set body property display: flex; with justify-conent:center; and align-items: center;
html, body { min-height: 100vh; } body { background-color: hsl(212, 45%, 89%); font-family: "Outfit", 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; display: flex; justify-content: center; align-items: center; }
Read more here - How to center
1 - @HassiaiPosted almost 2 years ago
Replace <div class="main-container center"> with the main tag to fix the accessibility issue.
To center .main-container on the page, add min-height:100vh; display: flex; align-items: center: justify-content: center; or min-height:100vh; display: grid place-items: center to the body.
Give #qr-code a max-width of 100% instead a width a height value. There is no need to give .main-container a height value, replace the height value with a padding value for all the sides.
Give h1 and p the same margin-top value and the same padding-right and padding-left values. Give p a margin-bottom value.
Use rem or em as unit for the padding, margin, width and preferably rem for the font-size for more on CSS units click here
Hope am helpful.
Well done for completing this challenge. HAPPY CODING
1 - @nelsonleonePosted almost 2 years ago
Straight to the point, You can set a max-width max-height on it then set a margin-auto.
Or set a display flex on the body
0
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