How to convert Pixel to Em - Why use ems for font size?


Following on from the last post about calculating beautifully proportioned text sizes, I thought it would be good to cover the "correct" unit of text measurement rather than just sticking with pixels.

The W3C recommends that you use ems for font sizes - why?
The "em" unit is the most scalable when it comes to on-screen display (read more from W3C here)

Basically it gives users the ability to scale up all the font in a web page using their browser settings and see them "grow" proportionally. This is for accessibility reasons.
Generally when you are in the design phase using pixels sizes is easier - so in the end you'll need to convert from pixels to ems.


So how to convert pixels to ems?

Most browsers have a default font size for "medium" text of 16px.
In this case 1em = 16px.
So if you want a font size of 20px you need to divide 20 by 16 which equals 1.25em - easy.

You can use the following widget to calculate em sizes.


Another trick that many web developers use is to scale the overall font size of the body to 62.5%.
CSS: body {font-size: 62.5%}
Why? This converts the overall default font size from 16px down to 10px (16 / 100 x 62.5 = 10). Therefore 1em = 10px and all other em calculations are easy to do in your head (20px = 2em, 12px = 1.2em, OMG so easy!)

WARNING! The only thing to look out for when using ems is nested elements. Using relative sizes (ems) rather than pixels means that the relative size will not override the parent element. It will inherit it from its parent.
For example, imagine a Div (font-size:2em;) with a P (font-size:0.8em;) inside.
The font size of the P inside will actually be 1.6em (2 x 0.8) so when using ems be aware of inheritance from parent elements.

Comments

Popular posts from this blog

PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies

3 Best Applications that Organize your Chat and Messaging Accounts

Japan Style: Architecture Interiors Design