Confused About rem and em units in CSS?

Which is better to use in CSS 🙄

Let's understand both units ✨

thread🧵👇
em and rem both are known as relative units in CSS.

You've probably been using em and rem units now for a while already,

but still, you might be confused about which unit is best in which scenario.
1️⃣ What is em?

An em value is equal to the computed font size of the parent of that element.

example if font size of parent element is 20 px then 1em is equal to 20px.
if the font size is not specified in the parent element then it will look continues up until the root element.

Root element font size is provided by the browser and by default it is 16px.

in this case, 1em is equal to 16px.
2️⃣ compounding effect of em -

check the below challenge, and then jump on the answer and understand how em works.

We have three divs, container, parent, and child and respectively are specified font-size 1em, 3em, and 2em.

1. font-size of container is 1em eqaul to 16px.

2. font size of parent is 3em means 3 * 16px ( parent font-size) so it becomes 48px now.
3. font size of child is 2em means 2 * 48px ( size of parent element) so it becomes 96px now.

the final size of the child becomes 96px, 😅 power of compounding.

check live example 👇

codepen.io/ali6nx404/pen/…
if you still do not believe on my words then see the below example.

see the computed size by the browser.
3️⃣ What is rem?

While em is relative to the font size of its direct or nearest parent, rem is only relative to the Html (root) font size.

by default, it is 16px until we didn't specify in the Html element.
rem value is not much convenient when it comes to specifying such as

1rem = 16px

But what if we want to use 10px than we need to calculate the rem value which is equal to 0.625rem.
To solve this problem we have one trick, specify the Html element font size as 62.5%.

than our 1rem becomes 10px and now we are able to calculate size easily.
4️⃣ Which is better?

There’s no better unit really, and it all depends on your personal choice.

Some people like to use rem units for consistency and scalability,

while some like to use em units in places where the influence of nearby parent elements would make sense.
I personally use rem more than em but be careful with both,

em becomes a trouble when you don't understand the relation between parents and child,

and we already viewed the example of compounding effect in em

so I like to say use rem more often than em.
If you want to calculate the accurate size in rem and em,

then this website is pxtoem.com pretty useful.
And that's it.

I tried my best to explain both units if you have still doubts,

then contact me or experiment with both units.

If you like this thread, drop a like and retweet, means a lot to me 💛

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Mahesh 👽

Mahesh 👽 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Ali6nX404

6 Oct
Total 1k Views, 100+ reactions, and 30 comments on my first blog from various platforms. 😎😍

5 things I learned during the creation of that article.

thread🧵👇
1️⃣ Research before write:-

I chose gradient because I use gradient a lot in my CSS art creation and,

I noticed there are not many well-detailed blogs available on the internet.

So I decided to go with it 🤠
Key point -

1. always do research about your blog topic.

2. Nothing is 100% new but we can improve at least 1% for easy understanding of readers.

3. Don't fall into research hell.

how to avoid research hell thread by @akoskm
Read 10 tweets
2 Oct
Are you using solid color background?🙄

Let's upgrade it with beautiful CSS gradients ✨

Mega thread🧵👇
1️⃣ What is the gradient?

CSS gradient allows you to create anything from a smooth gradient between two colors, right up to impressive mixing and multiple gradients.

Gradient makes the background more attractive than a solid color.
We all know about Instagram but did you notice that the Instagram logo is also a gradient.

The genius is in its simplicity.✨
Read 29 tweets
2 Oct
Are you using solid color background?🙄

Let's upgrade it with beautiful CSS gradients ✨

Mega thread🧵👇
1️⃣ What is the gradient?

CSS gradient allows you to create anything from a smooth gradient between two colors, right up to impressive mixing and multiple gradients.

Gradient makes the background more attractive than a solid color.
We all know about Instagram but did you notice that the Instagram logo is also a gradient.

The genius is in its simplicity.✨
Read 20 tweets
30 Sep
We're shifting on web 3.0 or you are still using Div element all along.🙄

That's not fair🤦‍♂️🤷‍♂️

Let's upgrade Div with Semantic tags. 🤯✔

thread🧵👇 Image
1️⃣ Wtf is Semantic?

Semantics refers to the meaning of a piece of code:-

"what purpose or role does that HTML element have".

The semantic element itself conveys some information about the type of content contained between the opening and closing tags.

1/3
Semantic elements increase the:-

- Accessibility
- Improve Search Engine Optimization
- Maintainability

And this makes code more readable for machines and humans too.

2/3
Read 17 tweets
29 Sep
Best CSS animation libraries for Web Developers✨

thread🧵👇
1️⃣ animate - This is a cross-browser library of CSS animations.

It’s extremely easy to use and helps you create beautiful CSS animations for web projects.

animate.style
2️⃣ Hover - A collection of CSS3 powered hover effects to be applied to links, buttons, logos, SVG, featured images and so on

ianlunn.github.io/Hover/
Read 7 tweets
28 Aug
Are You a Frontend Developer?

Or want to boost your frontend skills?

Here are 6 awesome websites to boost your Frontend skills.

Thread 🧵
1. frontendmentor - Solve real-world HTML, CSS, and JavaScript challenges whilst working on professional designs. one of the best website to get inspiration for your next projoct.

frontendmentor.io
2. codewell - Improve your HTML and CSS skills by practicing on real design templates.

codewell.cc
Read 9 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(