faqts : Computers : Programming : Languages : JavaScript : DHTML : DOM

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

61 of 88 people (69%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How can I convert 'em' to 'px'?

Feb 4th, 2003 02:45
Klaus Bolwin, Mark Filipak,


Hallo Mark,

first of all you have to retrieve the font-size of the body:

1. fz = document.body.fontSize;
or (if not available) 
2. you should use getComputedStyle (Mozilla)
or
3. getCurrentStyle (MSIE)

is the font-size given in pt, you can calculate the font-size in px by:

fspx = 1.3333*fspt;

e.g.
a font-size specified with: font-size:10pt;
will lead to a font-size of 13.3333px.

A font-size, specified with: font-size:2.5em;
is leading to a font-size, which is 2.5 times that of the bodies font-size.

e.g.
you specified the font-size of a div to 1.5 em, you can calculate its
pixel-value by:

fsdivpx = 1.3333*fsbodypt*fsdivem
or
fsdivpx = fsbodypx*fsdivem

in both cases is the result 20px