Design comparison
Solution retrospective
Hi! I'm happy with the result and I learned new things!
I want to know how I can use em and rem and not use px and still match the design. Does it matter if you don't want the component to change size?
Thanks!
Community feedback
- @RuanEsterhuysePosted over 2 years ago
Hi Louise,
Your solution looks great!
I will try to answer your question if I understand it correctly. You can use
rem
andem
units instead ofpx
and still match the design without affecting any changes in the size of the elements.Rem
is only relative to theHTML (root)
default browser font size. The default font size is16px
.Em
is relative to the font size of its parent element.So,
1rem
is equal to16px
,2rem
is equal to32px
,3rem
is equal to48px
and so on. For example, if you want to changepx
torem
for a font size of20px
, it'll be20/16=1.25rem.
If you want to use
em
, divide it by the parent element's font size. If the parent element's font-size is50px
, then1em = 50px
. So, for20px
it will be20/50 = 0.4em.
You can also specify a font size on the
HTML
element, which would make it easier to do the calculations.For example, you can set the font size to
10px
on theHTML
element. That will change the entire layout because it automatically changes all the lengths defined anywhere with therem
unit.1rem
will no longer be16 pixels
. It will be10 pixels
. So,20px
will be20/10 = 2rem
. However, you should not set it to a fixed value like10px
because the root font size will always be10px
. That will create usability problems for users who increase or decrease the font size of their browsers. Therefore, it is better to set it to a percentage of the default font size.10px
divided by16px = 62.5%
.html { font-size: 62.5%; }
Then,
10px
is equal to1rem
,20px
is equal to2rem
and so on.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