Design comparison
SolutionDesign
Solution retrospective
- How to decide which relative unit to select, "em" or "rem"? (I understand how they work. I just want to know when to use one or another)
- Any suggestions to maintain CSS would be helpful.
- Any other suggestions to improve would be wonderful.
Community feedback
- @seangray-devPosted about 2 years ago
Hey Anurag,
Here are some tips for your first question:
-
The em unit
- em are relative to their parent’s font size
- When used on anything else other than font-size it’s relative to the font-size of it’s own element
- font-sizes is an inherited property, so if you don’t declare it anywhere, it’s getting it from the body (or the default which is normally 16px)
- The problem with em: when we use em for font-size of an element, it can create a cascading effect.
-
The rem unit
- The rem unit is short for Root Em
- That means it’s always relative to the ‘root’ of our document
- The root of an HTML page is always the HTML element
-
How to decide which unit to use
- Pixels used to cause a lot of problems, as they were a fixed unit (1 dot on the screen)
- Now it follows the reference pixel.
General rule of thumb (not hard and fast rules) : - Font-size = rem - Padding and Margin = em - Widths = em or percentage
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