Swastik Yadav

S Y

Navigate back to the homepage
Subscribe

Convert px to rem - An effective workflow

Swastik Yadav
April 24th, 2021 · 2 min read

Beginner CSS developers often use the pixel unit everywhere and that’s fine there is nothing wrong with it.

But in this post, I will try to convince you to use rem instead of px. You will see what are the benefits of converting px to rem.

Working with pixels

Let’s say you built a webpage where you used pixels for everything from font size to padding, margin, and all distances. Now to make this page responsive you will define media queries for all those sizes.

For instance.

CSS

1body {
2 padding: 30px;
3}
4
5.heading-primary {
6 margin-bottom: 60px;
7}
8
9.text-primary {
10 font-size: 60px;
11 letter-spacing: 35px;
12}
13
14.text-secondary {
15 font-size: 20px;
16 letter-spacing: 17px;
17}
18
19.btn {
20 padding: 15px 40px;
21 border-radius: 100px;
22 font-size: 16px;
23}

Media Query

1@media all and (max-width: 600px) {
2 body {
3 padding: 15px;
4 }
5
6 .heading-primary {
7 margin-bottom: 30px;
8 }
9
10 .text-primary {
11 font-size: 30px;
12 letter-spacing: 17px;
13 }
14
15 .text-secondary {
16 font-size: 15px;
17 letter-spacing: 8px;
18 }
19
20 .btn {
21 padding: 7px 20px;
22 border-radius: 100px;
23 font-size: 10px;
24 }
25}

You wrote a media query for each font size, padding, margin, and distance. Well, that’s fine, But I can show you a better way to make all size and length-related stuff adaptable and responsive with just one line of media query.

Working with rem

The rem unit is always related to the root font size. And the root font size is set in the HTML selector.

For example, the default root font size of most browser is 16px. So, 1rem is 16px, 2rem is 32px, 3rem is 48px, and so on and so forth.

So if we now set that root font size, we can and very easily change all the other measurements on our page to rem.

To keep our px to rem calculations easy, I will set the root font size to 10px. So, now 10px is 1rem, 20px is 2rem, 35px is 3.5rem.

Formula: rem = px / 10

Let’s change all px units to rem accordingly.

CSS

1html {
2 font-size: 10px;
3}
4
5body {
6 padding: 3rem;
7}
8
9.heading-primary {
10 margin-bottom: 6rem;
11}
12
13.text-primary {
14 font-size: 6rem;
15 letter-spacing: 3.5rem;
16}
17
18.text-secondary {
19 font-size: 2rem;
20 letter-spacing: 1.7rem;
21}
22
23.btn {
24 padding: 1.5rem 4rem;
25 border-radius: 10rem;
26 font-size: 1.6rem;
27}

Now to make the page responsive you don’t have to define media queries for all size and length-related stuff. Just define the root font size in the media query and boom, you have an adaptable and responsive page out of the box.

Media Query

1@media all and (max-width: 600px) {
2 html {
3 font-size: 6px;
4 }

Compare this media query with the previous one.

That’s it. All sizes and lengths will reduce accordingly below 600px because all your sizes and lengths depends on the root font-size.

Define root font size in %

It’s a bad practice to define the root font size in px. You should always define the root font size in %. Let me explain why.

Sometimes readers (users) might change the default font size of the browser for his / her reading convenience. So, if the reader increases the default font size to 20px, it won’t change the root font size defined by you which is 10px.

That’s why you should define the root font size in percentage. So, that it changes when the reader changes the browser’s default font size.

So, the root font size of 100% will be the browser’s default font size of 16px. But you want it to be 10px (to make your calculation easier). Let’s do the math.


16px = 100%
(16px / 16px ) * 10px = (100 / 16) * 10
10px = 62.5%

All right then, Let’s change the root font size from 10px to 62.5%.

CSS

1html {
2 font-size: 62.5%;
3 /* Default 100% is 16px. All rem will adapt accordingly if user changes browser font size. */
4}

So, that was it for this post. I hope I was able to convince you to change all your px to rem. Here is a live web page built with rem instead of px.

https://woyiv.csb.app

If you found this post useful, then please consider joining the newsletter below for more such stuff.

More articles from Swastik Yadav

I launched my first product and it failed

I launched my first product and it failed because I didn't knew about search intent.

April 12th, 2021 · 1 min read

MongoDB Introduction and CheatSheet

Introduction to MongoDB. Mongo is a No SQL database, Let's see some basic commands of mongoDB.

August 6th, 2021 · 1 min read

Become a better full-stack developer in 10 minutes a week.

Be the first to receive our latest content with the ability to opt-out at anytime. We promise to not spam your inbox or share your email with any third parties.



By subscribing, you agree with Revue’s Terms and Privacy Policy.
© 2021 Swastik Yadav
Link to $https://twitter.com/swastikJsDevLink to $https://github.com/SwastikyadavLink to $https://www.linkedin.com/in/swastik-yadav-6386b71b5/Link to $https://www.youtube.com/channel/UCHs73qDeimVFGTa-MzjtnLA