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?
-
2For what it's worth, here's the definition for the different units in CSS: http://www.w3.org/TR/css3-values/#lengths. – Ryan Jun 11 '13 at 14:23
15 Answers
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 Stack Overflow makes extensive use of px units. It is not the poor choice that the question suggests it is.
Definition of units
px is an absolute unit of measurement (like in, pt, or cm) that also happens to be 1/96 of an in unit (more on why later). Because it is an absolute measurement, it may be used any time you want to define something to be a particular size, rather than being proportional to something else like the size of the browser window or the font size.
Like all the other absolute units, px units don't scale according to the width of the browser window. Thus, if your entire page design uses absolute units such as px rather than %, it won't adapt to the width of the browser. This is not inherently good or bad, just a choice that the designer needs to make between adhering to an exact size and being inflexible versus stretching but in the process not adhering to an exact size. It would be typical for a site to have a mix of fixed-size and flexible-sized objects.
Fixed size elements often need to be incorporated into the page - such as advertising banners, logos or icons. This ensures you almost always need at least some px-based measurements in a design. Images, for example, will (by default) be scaled such that each pixel is 1px in size, so if you are designing around an image you'll need px units. It is also very useful for precise font sizing, and for border widths, where due to rounding it makes the most sense to use px units for the majority of screens.
All absolute measurements are rigidly related to each other; that is, 1in is always 96px, just as 1in is always 72pt. (Note that 1in is almost never actually a physical inch when talking about screen-based media). All absolute measurements assume a nominal screen resolution of 96ppi and a nominal viewing distance of a desktop monitor, and on such a screen one px will be equal to one physical pixel on the screen and one in will be equal to 96 physical pixels. On screens that differ significantly in either pixel density or viewing distance, or if the user has zoomed the page using the browser's zoom function, px will no longer necessarily relate to physical pixels.
em is not an absolute unit - it is a unit that is relative to the currently chosen font size. Unless you have overridden the font style by setting your font size with an absolute unit (such as px or pt), 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 except where you specifically want it to scale as the font size scales.
Use em when you specifically want the size of something to depend on the current font size.
rem is like em, but it's relative to the base font size of the document (specifically, of the root element), rather than the font size given to the current element.
% is also a relative unit, in this case, relative to either the height or width of a 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.
Using % units in your design allows your design to adapt to the width of the screen/device, whereas using an absolute unit such as px does not.
vh, vw, vmin, and vmax are relative units like %, but they are relative to the size of the viewport (size of the viewable area of the window) rather than of the parent element. Respectively, they are relative to the viewport's height, width, smaller of the two, and greater of the two.

- 114,488
- 30
- 148
- 167
-
-
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 – djmzfKnm Jun 30 '09 at 17:24
-
30There 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 Jul 29 '09 at 06:09
-
3you forgetting about the accessibility for people with sight problems. The most commun usecase is that people with this disability change the base font size of the browser. If they to. You got a problem with all your pixel sizes, because text is gonna overlap for example. i think flodin's example points this out very clearly. Change the base-font size in chrome, safari or any browser and have fun browsing websites with PX dimensions. – meo Jul 29 '11 at 12:11
-
7Huh? I didn't say anything contradicting that. I was saying what px units are used for and what em units are used for. You seem to imply that using px units is generally bad. If a site breaks when you adjust the browser's default font size, the fault is not px units, it's bad assumptions. Clearly the web designer has relied on the default font size (often a good idea), and yet created the rest of their layout based on false assumptions about this font size, such as trying to align fixed-size graphical elements with text, where the element size only matches the text at one font size. – thomasrutter Jul 30 '11 at 13:52
-
The `em` unit stands for the font size of the element, except in `font-size`, where it stands for the font size of the parent element. Hence, the wording “currently chosen font size” is oversimplified, if not just wrong. – Jukka K. Korpela Apr 24 '13 at 17:10
-
5It's not wrong, nor oversimplified. Obviously, if you use em in a line where you actually *declare* the font-size, then it's impossible for em in that line to be relative to that same element's font-size *after* any font-size declarations on that element, because it won't know that yet - so instead it has to be relative to the font-size the element would have had *before* the font-size property on that same element is declared. Since any other behaviour is literally impossible, I don't see any ambiguity there. – thomasrutter Apr 24 '13 at 18:31
-
what happens if we use `em` and the text needs to grow `10X`. Shouldn't the rest of the screen's layout be written in a way that it can adapt? – mfaani Aug 15 '18 at 19:34
-
1If you want the size of something to be relative to the size of the text in your document, then `em` is appropriate, if you don't want it to change if you make the text 10x larger, then `em` is inappropriate. Whether or not you want your design to be able to adapt to a 10x change in text size is up to your design choices. – thomasrutter Aug 16 '18 at 01:08
I have a small laptop with a high resolution and have to run Firefox in 120% text zoom to be able to read without squinting.
Many sites have problems with this. The layout becomes all garbled, text in buttons is cut in half or disappears entirely. Even stackoverflow.com suffers from it:
Note how the top buttons and the page tabs overlap. If they would have used em units instead of px, there would not have been a problem.

- 49,047
- 9
- 93
- 153

- 5,215
- 4
- 26
- 39
-
17You can also zoom text in Firefox without zooming images in View -> Zoom -> Zoom Text Only, then zooming as normal. – thomasrutter Mar 04 '09 at 07:51
-
I have absolutely no issues zooming in SO with Opera, except image scaing being terrible. Can you provide a screenshot or something? – strager Mar 04 '09 at 20:00
-
4+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 Mar 06 '09 at 07:46
-
5@Spoike: I would, but Firefox does not honor the system DPI setting. See https://bugs.launchpad.net/ubuntu/+source/firefox/+bug/19524 – flodin Mar 06 '09 at 08:02
-
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 Mar 07 '09 at 04:12
-
This is an excellent point regarding em and should be upvoted! No one has mentioned that using em makes it much easier to have a site that "just works" with various mobile devices (and their small screen sizes). – velshin Oct 15 '11 at 12:34
-
Webkit (Safari and Chrome) make great zooming without all those issues you see, all browser should zoom like they do – Juan Ignacio Feb 01 '12 at 17:50
-
7@Juan: That's because they use the simplest zoom method: scaling the entire page. But it means at 150% zoom, a 1000px wide page becomes 1500px wide. It's the easiest way to zoom for web & browser developers. Unfortunately, it's not very user-friendly. Which is why other browsers have text-zooming that magnifies the content only without changing the layout. But that means web developers need to basically design their layouts for a wide variety of font sizes, greatly limiting what you can do design-wise. It's quite difficult to make a site look good in 16pt font as well as 48pt font. – Lèse majesté Aug 03 '12 at 16:16
-
2@Lesemajeste isn't scaling the entire page what "zoom" actually should be. Doesn't there come a point where the developer must tell the niche users to cope or not visit? I would never build a single page with consideration for for 16pt and 48pt font. That is just stupid and as you pointed out, very limiting on layout. I think it is great that FF wants to make a special zoom for text only, but I am not going to design with that in mind. – user1934286 Jan 31 '13 at 07:37
-
1@fredsbend: True, you can't satisfy everyone all the time, but in terms of what "zoom" should be, you need to consider what it's actually there for. Zoom is an accessibility feature. I would say that most visually impaired users just want to increase the text size, not necessarily the entire layout. If you want to discard with text zooming support, I would suggest making sure that visually impaired users don't need it as much. They may still need to use zoom, but perhaps they only need to zoom to 125% instead of 200%, and can thus avoid horizontal scrolling. – Lèse majesté Jan 31 '13 at 21:04
-
1@Lesemajeste A good point for a blog, mostly text site. I agree in that respect, but many sites are artistic in nature or contain many media features. I made a player recently that showed a lecture video, the slides, thumbnails and buttons with various features. For this I would most definitely prefer px measurements and zoom to affect the whole page. I know if I were sight impaired I would buy a large monitor and lower the resolution. – user1934286 Feb 01 '13 at 04:52
-
I think text-only zoom support is the biggest reason to use em today. Pixel sizes still aren't adjusted with changing the text size (tried IE11 and Chome). – Matt Zeunert Aug 31 '15 at 10:06
-
what happens if we use `em` and the text needs to grow `10X`. Shouldn't the rest of the screen's layout be written in a way that it can adapt? – mfaani Aug 15 '18 at 19:33
-
1@Honey layout should adapt to the screen's physical size, not its resolution. Your MS Word documents don't have a different layout on a 300 DPI printer vs a 1200 DPI printer. This is the same principle. – flodin Aug 17 '18 at 08:13
-
pro-tip you can change the dpi setting of your display instead of trying to zoom on firefox/etc which throws the layouts of many sites off . Added benefit is your taskbars and menus and other elements on your screen will be comfortably sized as well. If you're on linux you can set it via `Xft.dpi` in _.Xresources_ file if you use Xresource for your config. Not sure of setting in osx/windows/desktop environments with their own config. – speakingcode Jul 16 '21 at 21:15
The reason I asked this question was that 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 in 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 the 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.

- 668
- 1
- 9
- 22

- 119,724
- 44
- 140
- 158
-
1I'm wondering: How will it adjusts to grid-systems using percentages for width? Will it break it if the contianing block is given a width of 100%? – hugo der hungrige Jul 12 '13 at 17:57
-
77This is not true nowadays. Pressing `Ctrl+` and `Ctrl-` in any modern browser will scale the pixel values as well. It has been like this for a while now. – YemSalat Mar 18 '14 at 13:35
-
1
-
@YemSalat *`any modern browser will scale the pixel values`* -- true, but there is no fixed relation between `em` and other "absolute measures", the formula (or a layout decision) has to take the font size into account. – Wolf Nov 25 '15 at 09:20
-
2@Wolf, yes, but that is a horrible way of doing layout that wins you nothing and only brings suffering. – YemSalat Nov 25 '15 at 22:16
-
4@spoike, Downvoting this as it's clearly not true anymore as stated by YemSalat'scomment – RayLoveless Jun 06 '16 at 16:29
-
1I understand that this is now a year old, but in my opinion @thomastutter's answer should be accepted as it has been upvoted way higher than this. – David Archibald Jan 09 '17 at 05:57
-
1@YemSalat To me, a more interesting question is whether browsers are _required_ to intelligently upscale `px` measurements. If they are not, then `em` is still the correct choice. For all I know, browser authors just snuck that change in to work around poorly designed stylesheets everywhere... – underscore_d Mar 05 '17 at 21:05
-
-
2How come @OliverDixon? I use relative unit CSS a lot nowadays. `rem`, `em`, `ch`, etc. – Spoike Nov 11 '20 at 14:22
The top voted answer here from thomasrutter is right in his response about the em. But is very very wrong about the size of a pixel. So even though it is old, I cannot let it be undebated.
A computer screen is normally NOT 96dpi! (Or ppi, if you want to be pedantic.)
A pixel does NOT have a fixed physical size.
(Yes, it is fixed within one screen only, but in the next screen a pixel is most likely bigger, or smaller, and certainly NOT 1/96 of an inch.)
Proof
Draw a line, 960 pixels long. Measure it with a physical ruler. Is it 10 inches? No..?
Connect your laptop to your TV. Is the line 10 inches now? Still not?
Show the line on your iPhone. Still same size? Why not?
Who the heck invented the 96dpi computer screen myth?
(Some religions operate with a 72dpi myth. But equally wrong.)

- 1,393
- 1
- 12
- 17
-
4> To get an idea of the appearance of a px, imagine a CRT computer monitor from the 1990s: the smallest dot it can display measures about 1/100th of an inch (0.25mm) or a little more. The px unit got its name from those screen pixels. -- Quote from W3C: http://www.w3.org/Style/Examples/007/units.en.html#units – Jay Aug 14 '14 at 11:02
-
4_All absolute measurements are rigidly related to each other; that is, 1in is always *96px*, just as 1in is always *72pt*. (Note that 1in is almost never actually a physical inch when talking about screen-based media)_ I do not think the top voted answer is talking about physical inch. The relations are among the different absolute sizing schemes like in and pt. For Eg. Just extending your 'proof' if I draw a line of 1_in_ will it measure 1 inch physically? – Saumitra R. Bhave Jan 26 '15 at 16:19
-
7-1 for incorrect, irrelevant rant. `px` is sized relative to a _reference pixel_, not the pixels on the screens you're on about here. You might not like that, but getting your ruler out isn't going to make it any less of a fact, nor does it contribute to answering the question. http://stackoverflow.com/questions/27382331/how-a-css-pixel-size-is-calculated `"The reference pixel is the visual angle of one pixel on a device with a pixel density of 96dpi and a distance from the reader of an arm's length. For a nominal arm's length of 28 inches, the visual angle is therefore about 0.0213 degrees."` – underscore_d Mar 05 '17 at 21:09
It's of use for everything that has to scale according to the font size.
It's especially useful on browsers which implement zoom by scaling the font size. So if you size all your elements using em
they scale accordingly.

- 4,118
- 6
- 38
- 56

- 71,375
- 57
- 251
- 329
-
108Nowadays, all modern browsers implement zooming by scaling all absolute units equally, rather than scaling just font sizes. Note that this answer was written in 2009 when this was less the case than it is now. – thomasrutter Jan 24 '13 at 00:10
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:
- it's easy to keep proportions, if you choose to edit your font sizes in your CSS later on.
- 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.

- 66,919
- 31
- 85
- 96
-
I was just searching for a calculation formula :-) maybe it was good to add a reverse example as well, like `2px` is `0.125em` for a 16 pixel font. – Wolf Nov 25 '15 at 08:48
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

- 39,136
- 15
- 78
- 84
-
it is but it does not correspond to the WCAG 2.0 Minimum Requirements: http://www.w3.org/WAI/WCAG20/quickref/Overview.php - Sufficient Techniques for 1.4.4 – meo Dec 15 '11 at 08:53
example:
Code: body{font-size:10px;} //keep at 10 all sizes below correct, change this value and the rest change to be e.g. 1.4 of this value
1{font-size:1.2em;} //12px
2{font-size:1.4em;} //14px
3{font-size:1.6em;} //16px
4{font-size:1.8em;} //18px
5{font-size:2em;} //20px
…
body
1
2
3
4
5
by changing the value in body the rest change automatically to be a kind of times the base value…
10×2=20 10×1.6=16 etc
you could have the base value as 8px… so 8×2=16 8×1.6=12.8 //may be rounded by browser

- 1
- 1

- 1,279
- 13
- 19
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.

- 113,588
- 46
- 195
- 237
-
1Note 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 Dec 19 '09 at 18:51
-
1Better 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. – Rich Turner Oct 23 '10 at 12:14
-
1
-
4@MichaelMay Hence why the answer and other comments were written in 2009... lol – CatDadCode Aug 07 '17 at 21:03
Pixel is an absolute unit whereas
rem
/em
are relative units. For more: https://drafts.csswg.org/css-values-3/
You should use the relative unit when you want the font-size to be adaptive according to the system's font size because the system provides the font-size
value to the root element which is the HTML element.
In this case, where the webpage is open in Google Chrome, the font-size
to the HTML element is set by Chrome, try changing it to see the effect on webpages with fonts of rem
/em
units.
If you use px
as the unit for fonts, the fonts will not resize whereas the fonts with rem
/em
unit will resize when you change the system's font size.
So use px
when you want the size to be fixed and use rem
/em
when you want the size to be adaptive/dynamic to the size of the system.
There is a simple solution if you want to use px to specify font size, but still want the usability that em's provide by placing this in your CSS file:
body {
font-size: 62.5%;
}
Now specify you p
(and other) tags like this:
p {
font-size: 0.8em; /* This is equal to 8px */
font-size: 1.0em; /* This is equal to 10px */
font-size: 1.2em; /* This is equal to 12px */
font-size: 2.0em; /* This is equal to 20px */
}
And so on.

- 411
- 4
- 2
-
2Isn't this highly dependent on that no-one touches their browser's custom css settings? – Spoike May 11 '11 at 07:52
-
4Yea, you should just set `font-size: 10px`. http://css-tricks.com/forums/discussion/1230/the-problem-with-bodyfont-size62.5-and-ems/p1 – m33lky Mar 24 '12 at 23:50
-
I prefer to leave font-size: 100% and use mixins to work out the em value – gunnx Jun 16 '15 at 11:58
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.

- 1,438
- 8
- 9
-
1+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 Mar 06 '09 at 07:50
-
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 Mar 06 '09 at 14:52
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.
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...

- 70,219
- 68
- 205
- 290
Avoid em or px use rem instead becuase its easier to find the computed value. But between em and px, px is better because em is hard to debug.

- 91
- 1
- 6
-
3Please add some further explanation to your answer such that others can learn from it - especially as this is a really old question which has already tons of upvoted answers – Nico Haase Apr 09 '20 at 12:40