Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Converting px to em

Hello guys,

What is the best practice for converting a stylesheet current using font-sizes in px's to em's?

Thanks

4 Answers

I was thinking exactly the same thing yesterday. I went through and used an online tool to convert the sizes and then manually put them into my CSS.

Would be great to know if there is an easier way :D

I used http://pxtoem.com/ which was good, but slow...

Hello John Steer-Fowler,

Thought this may benefit you as it benefited me. I found after research that if the body font size has been set to 16px, which is the browser default, 1em will equal to 16px.

So say if we want to convert 19px to em's, we do the following:

19 / 16 * 1 = 1.1875em

We can also set body size to 62.5% to set the browser's font size to 10px. This will make the calculation process easier e.g

19 / 10 * 1 = 1.9em

Thanks

Hey James,

They mention this in the video on em and px. I used the converter as it was slightly faster than doing the calculations.

There are also some nice conversion grids out there that make the process simpler. I hadn't really used em before, but i'm beginning to really like it :D

I would step away from using conversion grids as they are only going to give helpful output in certain contexts. I would say the best way to do it is to begin using one of the above's formulas and doing it that way :)