Design comparison
Solution retrospective
I found this easier compared to the previous project which was the product card. I am a bit confused though as to when to use certain units in CSS like em, rem, %, px, vh, vw. Can I get any inputs on the units I used, and if I used any of them wrong, which one should I use and why?
Community feedback
- @correlucasPosted about 2 years ago
👾Hello @blurridge, Congratulations on completing this challenge!
Great code and great solution! I’ve few suggestions for you that you can consider adding to your code:
Your solution is great and the code is working, but the HTML structure can be reduced by removing unnecessary divs, all you need is a single
<main>
or<div>
to keep all the content inside, and nothing more. The ideal structure is thediv
and only the image, heading, and paragraph.Here’s one example to show can be cleaner this HTML structure:
<body> <main> <img src="./images/image-qr-code.png" alt="QR Code Frontend Mentor" > <h1>Improve your front-end skills by building projects</h1> <p>Scan the QR code to visit Frontend Mentor and take your coding skills to the next level</p> </main> </body>
To reduce the CSS you can use the direct selector for each element instead of using
class
this way you have a code even cleaner, for example, you can select everything using the direct selector for (img, h1, and p, main).✌️ I hope this helps you and happy coding!
Marked as helpful0@blurridgePosted about 2 years ago@correlucas When do you usually use separate divs for each components? Always thought it was a rule of thumb to separate it.
1@correlucasPosted about 2 years ago@blurridge For a component will be a different block, but since the div is holding all the content you can keep it inside a single one.
0@blurridgePosted about 2 years ago@correlucas Thank you so much! Good luck completing all HTML and CSS challenges, Lucas!
0 - @M-lakshanPosted about 2 years ago
-
em, rem, %, vh, vw are produce dynamic sizes while em depends on the page's default font value, rem depends on the element u are styling & %, view-height, view-width are depends on the projecting resolution.
-
px, pc, pt will always produce static values. these are universal for sizing that beginning from
1px >= 0.1pc ( roughly 10px = 1pc ) & 16px = 12pt.
-
from things I've learned its better to use rem, em values only for fonts because if we consider a component with dynamic values may change the alignments since they are dynamic. its up to u to choose what considering the situation. however u can change the em's default value ( 1em = 16px standard default ) to any size you prefer by putting
html { font-size: 10px; }
in top of your stylesheet. according to that if u had usedmargin-top: 1em
for an element, it will bemargin-top: 10px;
because we reduce the default font size from 16px to 10px.
hope this helps 🙂
Marked as helpful0@blurridgePosted about 2 years ago@M-lakshan Thanks for this! That's what I realized too when I was experimenting with this project. Used % at first for width of my container but I realized it readjusts based on screen. So I made it static instead by using px.
0 -
- @vanzasetiaPosted about 2 years ago
Hello, Zach! 👋
Most of the time you want to avoid using
px
unit. Instead, userem
as the main unit for the stylesheet. For the other CSS units, you need to learn about those units to know when to use it. For example,em
is a unit relative to the closest font size of the parent element. So, if you want this behavior, you can useem
instead ofrem
.Let's say the HTML markup is the following.
<div class="container"> <p>text</p> </div>
The CSS is the following.
.container { font-size: 2rem; // => 32px } .container p { font-size: 1em; // => 2rem = 32px }
Some suggestions from me.
- To place the card in the center of the page, I recommend using flexbox or grid. These modern techniques are more robust than absolute positioning and require less code to implement.
- Set a
max-width
to the.container
instead ofwidth
. This way, the element is allowed to shrink if ever needed.
That's it! I hope this helps!
Marked as helpful0@blurridgePosted about 2 years ago@vanzasetia May I ask what makes px so bad? Honestly curious since I had some problems with shrinking elements with other units such as % and rem until I changed it px.
0@vanzasetiaPosted about 2 years ago@blurridge
px
unit is not 100% evil. But, you need to be aware thatpx
is a static unit. It means that20px
will always be20px
. It doesn't care when the user is increasing the browser's font size, the size will always be20px
.I recommend reading the "The Surprising Truth About Pixels and Accessibility: should I use pixels or rems?" article.
For me, after some practice, I can start choosing what unit I should use. So, I think after you have more experience with CSS, you can start choosing the appropriate CSS unit. 🙂
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