Making the transition from px to em

Written by That Web Guy on 7th June 2009. 15 comments

Making the transition from px to em
  • Avant Innovations
  • Advertise Here

Like it or not, there are times when your readers will want to resize the on-screen text for what ever reason. I find myself doing it often because I run a high resolution monitor and some web designers still believe in tiny fonts. But that's another story.

So what should you be using to display your fonts? Em or Px? That depends who you ask, but first a brief lesson on the differences.

Em, pronounced exactly as it's spelled, is equal to the default height of the upper-case letter M. When used in CSS an em will be the default font size, or if it's a child, the proportional size of the parent element. Em's are only really intended to be used for font sizing and line height, not padding or margins. Confused? A few examples soon - I promise.

Px, is the exact height of the font measured in pixels, regardless if it's the child of a parent or not. Px is a fixed value no questions asked. The other thing to consider is that pixel values only have meaning on a computer, so when you print then it will be up to the hardware to 'best guess' what physical size you really meant.

Using pixel values also comes with an accessibility issue, because if the user has increased their default font size in the browser (I'm talking about in the browser preferences, not the zoom feature), then the size will remain at the pixel value specified in the style sheet.

But at the end of the day, 12px for example if nowhere near the size of 12em (try it out and see for yourself :-), and you'll want an easy way to style your documents without having to figure out what em value to use to make text 12px high. There's an easy solution to that, and it's called the 62.5% trick. Here's how it works.

The trick involves making em's "act" like px. In your CSS file do this:

body {
font-size:62.5%
}

This will make 1em be equal to 10px, because 16 (a browsers default font size) x 62.5% is equal to 10. With that in place, you can start using em values instead of px values. Some examples:

...and so on.

That means you can also use them for line-height. Again the 62.5% trick works, so line-height: 1.6em is actually 16 pixels for example.

Things start to get tricky

When I first made the jump to using em's I was caught off guard by the parent/child relationship "issue". Using a specific example, if I had a parent div with a font size of 2em, and then a child element with a font size of 1.5em, it's reasonable to assume that the parent font size would be 20px and the child 15px. No such luck, and you'll understand why in a second.

At the beginning of this article I eluded to child em's inheriting the size of their parent. With this in mind, if the text of my parent element was 2em and the child was 1.5em, the child would in fact be displayed at 3 (30 pixels in height), because 2 x 1.5 equals 3. Get it? It's a multiplier, not addition or subtraction.

Another example with a 2em parent: If I wanted the child to be half the size of parent, then the child needs to be .5em (because 2 x .5 = 1). If I wanted the child to be double the size of the parent, then the child would need to be 2em (because 2 x 2 = 4).

That can be very advantageous. Let's say you have a parent at 2em with a bunch of child elements and you wanted to see what they would all look like 50% bigger. All you need to do is increase the parent value to 3 (2 x 50% = 1, so just add 1). Too easy.

After all is said and done it's really basic maths. I've provided an example file for download at the end of the article.

This not only solves the accessibility and printing issues, but you've also made it easier to make the permanent transition from px to em. Good luck!

Something to consider

While all this will allow the user to change their default font size in the browser preferences and actually see a change (where pixels won't), the change they see will be smaller than expected.

This is still a more desirable outcome than being forced to a fixed pixel size, but purely from an accessibility point of view it's not perfect. If that's an issue for you, then remove 62.5% from your CSS - and compensate for the sudden increased size of your text if you have to. Thanks to Emily for the reminder.

using_ems.zip (1k)

Is this worth sharing?

That Web Guy

About That Web Guy

That Web Guy (Mikey to his friends) is a veteran web designer based in Perth, Western Australia, and currently Design Director at Perth Web Design. When he's not XHTML'ing or messing around in Photoshop, Mikey can usually be found preaching web standards evangelism onto unsuspecting victims.

Feel free to send That Web Guy a message some time, follow him on Twitter, or make a donation.

Comments

Not a Member

Katey

Nice trick. I've seen the 62.5% trick on some style sheets before but never understood why it was there. I'm going to stick with pixels for now though thanks!

Tuesday 9th June 2009 | 03:22 PM Reply Comment URL Back to top

@todayinart

@todayinart

So with the zoom feature becoming more standard in browsers, do you feel like the use of em will go away? I have used both, but liked pixels more. In either case, this trick looks cool and worth trying out. Thanks!

Wednesday 10th June 2009 | 08:05 AM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by @todayinart

I can't see it going away because of the accessibility consideration. A person with limited mobility for example doesn't want to be pressing CTRL and + on every site they visit. I actually have a few (past older) clients with visual impairments who have the default font size increased in the preferences for this very reason.

Em's take a little getting used to but aslong as you remember about the child element 'issue' you'll be fine :-)

Wednesday 10th June 2009 | 08:48 AM Reply Comment URL Profile Back to top

CSS Babe

CSS Babe

That's beautiful in its simplicity. From a CSS noob, you have my thanks!

Thursday 11th June 2009 | 07:09 AM Reply Comment URL Profile Back to top

Not a Member

Emma J

I can seriously see this doing my head in. I read it a few times and it seems to make sense and now I just have to try it out for myself. Kudos for the food for thought.

Thursday 11th June 2009 | 07:22 AM Reply Comment URL Back to top

Not a Member

Axcel

This is pretty much standard practice in web design these days.

Nice look and feel you've got going on here web guy. Mind if I snoop around?

Friday 12th June 2009 | 07:28 PM Reply Comment URL Back to top

Not a Member

Tim

Ding!! now I get it. Thanks Mikey. This makes sense.

Friday 12th June 2009 | 07:57 PM Reply Comment URL Back to top

Harun Smrkovic

Harun Smrkovic

Still don't get it.. Why should I use em instead of px ? just makes things more complicated ?

Friday 12th June 2009 | 08:40 PM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by Harun Smrkovic

It's better for accessibility and then there is the print reason already mentioned. You also get easier experimental control over your design. See 3rd last paragraph under "Things start to get tricky".

I'll admit it sounds complicated but it's really not once you start using it.

Saturday 13th June 2009 | 08:25 AM Reply Comment URL Profile Back to top

Kim OJ

Kim OJ

Okay, this looks like a day of fun transition issues, I'll have to try it on a day when I feel brave.

Tuesday 23rd June 2009 | 01:28 PM Reply Comment URL Profile Back to top

creanium

creanium

Responding to this comment by Katey

Sorry, but you should really consider switching to using EM (or percentages if you wish) instead of pixels when it comes to font sizes. There are still many browsers that will not resize text that's sized in pixels, thus making your website less accessible to someone unable to read smaller fonts.

Wednesday 24th June 2009 | 07:21 AM Reply Comment URL Profile Back to top

Kim OJ

Kim OJ

If we are really making a virtue out of accessibility and user control, shouldn't we just dump the 62.5% trick and do our multiplications / divisions of 16? Granted it is easier to do the math with 10, but this is presumably done on a computer anyway, so you do not have to do the math in your head if you feel it is a strain.

Sunday 28th June 2009 | 01:29 AM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by Kim OJ

Absolutely true Kim. This exercise is aimed at making the transition easier but if accessibility is the only concern then we shouldn't need this trick. I guess you could look at it as a compromise.

Another thing you could do is to remove the 62.5% from the CSS after you've finished designing, which would increase the size of everything (and that's not a bad thing for people with VI) and it would also keep everything proportional still.

Sunday 28th June 2009 | 09:42 PM Reply Comment URL Profile Back to top

Teegee

Teegee

Hi! I have been rebuilding my entire website yesterday, but unfortunatewly used the ratio 16px = 1em. and got really frustrated calculating all those values, finally I have build program to go through all lines of code (css and html, javascript calculations need to be done manualy, as you add +'em' to the result value.) looking for all numbers followed by specified two letter unit (ie. 200px) after that it calculates the em value and put it back into the code with new value and unit, it can calculate back from em to px, or any units you insert (however if you use .5em instead of 0.5em it will not work, it will just skip the '.' and take it as 5em, sorry, just realized now while reading this post) or if you like you may recalculate font size from 10em to 16em replacing original 16px = 1em with 10em=16em.
I have uploaded it on QDrive, however they do not accept .exe files, so when saving just add .exe to the filename. (btw. sorry it's so big 392KB, but it's written in Delphi, I do not know other desktop programming languages, if someone want's the code to build it in C or something different let me know, or if you know how to take value inserted into textarea as is so entire code with tabs, spaces enters etc. and send it to php and then echo everything back as code without parsing, I will build it as an online tool. However for now it's only that:
https://www.qdrive.net/download/sharelinkdownloader.php?id=15754&key=xzmX72qDsKp412nP4OX43AjP2Gx412nX46d

Thursday 16th July 2009 | 07:49 PM Reply Comment URL Profile Back to top

Teegee

Teegee

well, btw is it possible to somehow scale the background. Let's say I have a button with em sizes, and the button has image as background if somebody change the font size, the background becomes too small, or too big, I can't find anything like background strech. Do you know any way how to do it?

Thursday 16th July 2009 | 09:53 PM Reply Comment URL Profile Back to top

FYI: You are currently not logged in. It's cool though - you can still comment. But only members get a profile page, access to the download section and they can pimp their own web sites. Feel free to register or Login now!

Your name:

Your comments:

Note: HTML tags are automatically stripped from comments.

Are you human?
Turing

Sorry, I have to ask. So what sort of animal is this? (Hint: you don't have to be perfectly specific)

Back to top

Login to That Web Guy Blog

Login

Not registered? | Forgot your Password? Cancel Login