views:

3031

answers:

10

I heard you should define sizes and distances in your stylesheet with em instead of in pixels. So the question is why should I use em instead of px when defining styles in css? Is there a good example that illustrates this?

+7  A: 

It's of use for everything which has to scale according to the font size.

It's especially useful on browsers which implement zooming by scaling the font size. So if you size all your elements using em they scale accordingly.

DR
+2  A: 

use px for precise placement of graphical elements. use em for measurements having to do positioning and spacing around text elements like line-height etc. px is pixel accurate, em can change dynamically with the font in use

Scott Evernden
+5  A: 

Because the em (http://en.wikipedia.org/wiki/Em_(typography)) is directly proportional to the font size currently in use. If the font size is, say, 16 points, one em is 16 points. If your font size is 16 pixels (note: not the same as points), one em is 16 pixels.

This leads to two (related) things:

  1. it's easy to keep proportions, if you choose to edit your font sizes in your CSS later on.
  2. Many browsers support custom font sizes, overriding your CSS. If you design everything in pixels, your layout might break in these cases. But, if you use ems, these overridings should mitigate these problems.
Henrik Paul
+6  A: 
flodin
You can also zoom text in Firefox without zooming images in View -> Zoom -> Zoom Text Only, then zooming as normal.
thomasrutter
I have absolutely no issues zooming in SO with Opera, except image scaing being terrible. Can you provide a screenshot or something?
strager
@strager: good idea
flodin
Err, why was I downvoted?
flodin
+1 good point. although regarding large resolutions, you could set your screen setting to show text at 120 dpi instead of the standard 96 resolution
Spoike
@Spoike: I would, but Firefox does not honor the system DPI setting. See https://bugs.launchpad.net/ubuntu/+source/firefox/+bug/19524
flodin
I've seen worse - at least the text stayed with the buttons/tabs. I've seen sites where the buttons go one way and the text over them go a different way. Still, they haven't allowed enough space for that resolution and font size particulrly around the top tabs.
thomasrutter
+29  A: 

It is wrong to say that one is a better choice than the other (or both wouldn't have been given their own purpose in the spec). It may even be worth noting that StackOverflow makes extensive use of px units. It is not the poor choice Spoike was told it was.

px relates to pixel size on-screen. If, for example, you are writing a print style sheet the argument could be made that you shouldn't use px units.

em should be used when you want to define something relative to the size of characters in the current font. Unless you have overridden font style (using px units for example), this will be affected by the choice of fonts in the user's browser or OS if they have made one, so it does not make sense to use em as a general unit of length unless you specifically want it to scale as the font size scales, which may often be the case.

px should be used when you want to define something relative to the size of a pixel on-screen. This can be useful when you want something to be on the same scale as a 1:1 image in the page, for example. If you have a 16x16px icon, you'll probably want to state its size in pixels (unless, I guess, in a print style sheet) and position its background or label using pixels so that they do not distort relative to the image when the font size or browser zoom changes (if the browser zoom scales images, it will scale px units equally).

% values can be useful when you want a length relative to a dimension of the parent element. They are a good alternative to px units for things like the total width of a design, if your design does not rely on specific pixel sizes to set its size.

thomasrutter
What to use for "width" property in CSS em or px? and why?
Prashant
In case I want to convert a PX stylesheet to em based then 1px = how many em's? I tried out this tool: http://riddle.pl/emcalc/ but when I am converting 990px to em, it returns 61.88em, but when I am adding this to stylesheet then my content area becomes small, than 990px
Prashant
There is no way to convert between ems and pixels, unless you know what the size of an 'em' is in pixels, in that context. That can depend on the inherited font size of that element, which can in turn depend on the font size of the document as a whole, which can depend on the font size settings in the user's browser and/or operating system. If you are aiming for a particular pixel size, then specify it in pixels. Ie, if you want 990px, then put 990px. If pixels is what you want, why not use them?
thomasrutter
+6  A: 

A very practical reason is that IE 6 doesn't let you resize the font if it's specified using px, whereas it does if you use a relative unit such as em or percentages. Not allowing the user to resize the font is very bad for accessibility. Although it's in decline, there are still a lot of IE 6 users out there.

John Topley
My web-developer friends wish IE6 never existed! :)
xk0der
At lot of us feel like that, but to be fair it was a very good browser when it first came out. It's just a shame Microsoft forgot about it for all those years!
John Topley
Note that IE7 and IE8 *still* can't resize text with a font size specified in pixels (through `Page > Text Size`). However, IE7 added page zoom (`Page > Zoom`) which zooms the entire page, including the text, obviously.
mercator
Better than Page > Zoon, try hitting [CTRL] + [-] or [+] keys on your keyboard in IE7+, Chrome and Firefox - full page zooming without most of the issues that one experiences trying to change font sizes.
bitcrazed
+1  A: 

The main reason for using em or percentages is to allow the user to change the text size without breaking the design. If you design with fonts specified in px, they do not change size (in IE 6 and others) if the user chooses text size - larger. This is very bad for users with visual handicaps.

For several examples of and articles on designs like this (there are a myriad to choose from), see the latest issue of A List Apart: Fluid Grids, the older article How to Size Text in CSS or Dan Cederholm's Bulletproof Web Design.

Your images should still be displayed with px sizes, but, in general, it is not considered good form to size your text with px.

As much as I personally despise IE6, it is currently the only browser approved for the bulk of the users in our Fortune 200 company.

Traingamer
+1 fluid grids was actually what I was looking for when I asked the question. (also, shouldn't your company be upgrading to IE7?)
Spoike
If you asked me, they should use Firefox, but they don't actually ask me. :-) (IE7 would be better than 6, but I'm not privy to the decision-making)
Traingamer
A: 

You probably want to use em for font sizes until IE6 is gone (from your site). Px will be alright when page zooming (as opposed to text zooming) becomes the standard behaviour.

Traingamer already provided the neccessary links.

A: 

The general consensus is to use percentages for font sizing, because it's more consistent across browsers/platforms.

It's funny though, I always used to use pt for font sizing and I assumed all sites used that. You don't normally use px sizes in other apps (eg Word). I guess it's because they're for printing - but the size is the same in a web browser as in Word...

DisgruntledGoat
+2  A: 

The reason I asked this question was because I forgot how to use em's as it was a while I was hacking happily in CSS. People didn't notice that I kept the question general as I wasn't talking about sizing fonts per se. I was more interested how to define styles on any given block element on the page.

As Henrik Paul and others pointed out em is proportional to the font-size used in the element. It's a common practice to define sizes on block elements in px however sizing up fonts in browsers usually breaks this design. Resizing fonts is commonly done with the shortcut keys Ctrl++ or Ctrl+-. So a good practice is to use em's instead.

Using px to define width

Here is an illustrating example. Say we have a div-tag that we want to turn into a stylish date box, we may have html-code that looks like this:

<div class="date-box">
    <p class="month">July</p>
    <p class="day">4</p>
</div>

A simple implementation would defining the width of the date-box class in px:

* { margin: 0; padding: 0; }

p.month { font-size: 10pt; }

p.day { font-size: 24pt; font-weight: bold; }

div.date-box {
    background-color: #DD2222;
    font-family: Arial, sans-serif;
    color: white;
    width: 50px;
}

The problem

However if we want to size the text up in our browser the design will break. The text will also bleed outside the box which is almost the same what happens with SO's design as flodin points out. This is because the box will remain the same size in width as it is locked to 50px.

Using em instead

A smarter way is to define the width in ems instead:

div.date-box {
    background-color: #DD2222;
    font-family: Arial, sans-serif;
    color: white;
    width: 2.5em;
}

* { margin: 0; padding: 0; font-size: 10pt; }

// Initial width of date-box = 10 pt x 2.5 em = 25 pt
// Will also work if you used px instead of pt

That way you have a fluid design on the date-box, i.e. the box will size up together with the text in proportion to the font-size defined for the date-box. In this example the font-size is defined in * as 10pt and will size up 2.5 times to that font size. So when you're sizing the fonts in the browser, the box will have 2.5 times the size of that font-size.

Spoike