Using ems to Specify px Text Sizes in CSS

We all know that internet explorer is the runt of the browsers. In yet another rebellion, it simply refuses to scale elements which have been specified using pixels. In this tutorial I will show you how to think in pixels but work with ems.

By Clinton Montague

Please note!! I have shamelessly stolen this article from my old and (even if I do say so myself) rather boring blog. So please ignore the terrible writing style! I’ll rewrite this article in the future, but until then, I present you with version 1.0

Not a very clear title, I know, but it’s difficult to put across exactly what I mean with only a few words. Basically, this is a simple way to allow for you to specify the exact pixel size of your text using ems. This is useful because internet explorer can’t resize text if it is set with a px height. This is bad not only for people with impaired vision, but also those who use huge screen resolutions and need to resize the font so that they can see the 8px font size which so nicely fits into your design.

It is quite a simple and extremely easy to use trick. The first thing to know is that 1em is set to the default font size for the document (or user’s settings). Well, technically it’s the size of a capital M (hence the name ‘em’), but it’s the same thing. So if you change the default body text size to 10px, you can very easily exactly specify other sizes while still allowing IE to resize them properly.

The second and last thing to know is that em is a percentage scale, so to increase the size from 10px (now 1em) to say, 20px you would use a size of 2em. Likewise, to change from 10px to 15px, you would use 1.5em. If find it hard to use percentages, the simple way to do it is to divide the size you want by 10 (notice how 20px changed to 2em and 15px changed to 1.5em

1
2
3
4
5
6
7
8
9
10
11
body
{
  font-size: 10px;
  /* now 1em = 10px */
}
 
p
{
  /* set paragraph font size to 12px using 1.2em*/
  font-size: 1.2em;
}
Tags: , ,

Me on the intertubes

It wouldn't be right to have a web site, be a nerd, and not take this opportunity for a bit of shameless pluggery.

If you like all these weird and magic internet page things, you should try Coaster Nerd, Oxford Bloggers or, if you'd really like some unexplainable sourcery, is it the weekend yet.

Also you should follow me on Twitter ;)

Where to find me

I can be found fairly regularly at Oxford Geek Nights, OxTuttle and other general nerding out activities in and around Oxford.

I am also often found in my natural habitat which is anywhere that you can find an adreneline machine. I frequent English theme parks more often than is healthy, and take trips abroad to sample foreign rides.

From the aviary

Was having a decidedly crap day of development. Just got much better. It's nice when things go your way. #win 1 day ago

Subscribe

Slightlymore Posts RSS feed

Comedy disclaimer

These posts were written at the time of writing and may not still be accurate or reflect my opinions any more. In fact, they may not even be correct or representative at the time of writing. You see, when I write, I just write. I don't do that thing which they always told you to do at school and plan what you want to write first. Because of this, there will almost certainly be a load of typos, grammar errors and incorrect facts and references. If this makes you feel queasy, I apologise that you had to get to the bottom of the page to read this, and hope that some day, you'll learn to forgive me.

What's this site?

Slightlymore started off all too formally for my liking. In the depths of the archives, you'll find tutorials. However if you read the more recent posts, you'll find them a lot more playful and about fun and interesting stuff.

Annoyingly difficult to use tag cloud