px to em

godly.asian
avatar
rank Regular

Regular


Posts: 13
Joined: 2007-07-27

is there a formula to convert px to em?

Deuce
Deuce's picture
rank Elder

Elder


Posts: 1653
Joined: 2005-11-20
Location: STL

in short, no. pt and px are

in short, no.

pt and px are absolute values, em and % are relative values.

Ed Seedhouse
Ed Seedhouse's picture
rank Guru

Guru


Posts: 2528
Joined: 2005-12-14
Location: Victoria British Columbia

Deuce is right, there is no

Deuce is right, there is no simple conversion. Roughly speaking an em is the size of the current font, whatever that happens to be. It is best to avoid defining font sizes in pixels on the web. The web is not paper and you shouldn't try to treat is as if it were.

I could change my plea to guilty,
but I don't think it would stick.

Ed Seedhouse

thepineapplehead
thepineapplehead's picture
rank Moderator

Moderator


Posts: 9203
Joined: 2004-06-30
Location: Milton Keynes

PX to EM

Somebody hit the lights
So we can rock it day and night
People gettin' down that's right
From PX to EM
Everybody lookin' like stars (like stars)
All the chicks and the fellas in the bars (in the bars)
All ya'll bumpin' this in ya cars (in ya cars)
From PX to EM

godly.asian
godly.asian's picture
rank Regular

Regular


Posts: 13
Joined: 2007-07-27

standard

lets say the font size was standard. would there still be any...and if there is, is there neway to not allow users to change the font size(through javascript or nething).

Hugo
Hugo's picture
rank Moderator

Moderator


Posts: 12314
Joined: 2004-06-06
Location: London

Wow bad paragraph Not sure

Wow bad paragraph Smiling

Not sure what you're asking but if it's whether there is a means to prevent font-resizing no and nor should one want to do this.

Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

godly.asian
godly.asian's picture
rank Regular

Regular


Posts: 13
Joined: 2007-07-27

hmm, let me rephrase what I

hmm, let me rephrase what I said.
Is there any formula to convert px to em, on a standard font.

gary.turner
gary.turner's picture
rank Moderator

Moderator


Posts: 5848
Joined: 2004-06-25
Location: Dallas

As stated earlier, an em is

As stated earlier, an em is the size of the current font. If the current font is 16px, that's one em.

If you have that 1em font and you want a child to be 12px, that would be .75em relative to its parent. Within the child element, that 12px font is 1em.

It will give you tired head while you're learning it, but it is worthwhile as you will be able to alter all fonts proportionally by simply modifying the top level font size, and you will have a font-size gestalt.

cheers,

gary

A pianist who has learned the wrong system of fingering cannot become a virtuoso until he has laboriously taught himself the proper method.

My site is slo-o-owly being reconstructed; visit anyway.

Zander78
Zander78's picture
rank newbie

newbie


Posts: 2
Joined: 2008-05-08

You can't use a formula

You can't use a formula, as said, since it's a relative unit and depends on what font-size the parent element (and the parent's parent ...) has. For example if the body element has 10px, 10px = 1em. If then a element inside of the body has 2em and its child 1em, the font-size is 20px.

If you're familiar with javascript, you can check out this:
http://chrisbk.de/post/33642727

Ed Seedhouse
Ed Seedhouse's picture
rank Guru

Guru


Posts: 2528
Joined: 2005-12-14
Location: Victoria British Columbia

godly.asian wrote:hmm, let

godly.asian wrote:
hmm, let me rephrase what I said.
Is there any formula to convert px to em, on a standard font.

You have to understand that THE WEB IS NOT PAPER!!

You seem to want to make it into paper, and it can't be done. It just isn't, and nothing you can do will make it into paper.

If you insist on trying you will only write very bad sites that will make a lot of people go away from them. Nothing you can do will make the web into paper. Understand this, and learn to adapt.

The web isn't even visual. A certain percentage of visitors will not look at your site, but they will still visit it. The most important non visual visitor is, of course, the Google spider, but some folks with no eyesite still have money they can spend. Why would you voluntarily throw away part of your possible income?

I could change my plea to guilty,
but I don't think it would stick.

Ed Seedhouse

Stomme poes
Stomme poes's picture
rank Leader

Leader


Posts: 607
Joined: 2008-02-04
Location: Netherlands

Blah

Actually, I found this px-to-em calculator to be very helpful: http://riddle.pl/emcalc/

Because as everyone has said above px are static and em are relative, what this page seems to do is use JS to determine what YOUR font size is (by default). Obviously, everyone else isn't going to have that same font size but what the tool is good for is when you are sizing up boxes (I was doing a crapload of forms and no, I could not eyeball it by imagining letter M and seeing how wide in em the box should be) next to boxes and boxes. I would measure what I had already with a ruler that came with the Web Developer Toolbar to get a px unit. Then I plugged it into the pxtoem calc, and took that number and used it as a basis to get a feel to how close it really was to what I needed.

I usually still had to adjust the amounts, of course, but a lot less adjusting since I was starting out in the ballpark already (test test test in all browsers). Now I have wonderful boxes who scale with the text! It doesn't matter how big people make the font, everything stays cool. : )

I'm no expert, but I can fake one on teh Interwebz

luwana
luwana's picture
rank newbie

newbie


Posts: 8
Joined: 2008-05-12
Location: las vegas

Out of all the estimates

Out of all the estimates i've seen I kept this in my notes...
for font size:
1em=12px
.8em=11px

If you set the font size to something in px then the em will be the size of an "M" in that font size - here is a better explanation: http://24ways.org/2005/an-explanation-of-ems

Ed Seedhouse
Ed Seedhouse's picture
rank Guru

Guru


Posts: 2528
Joined: 2005-12-14
Location: Victoria British Columbia

luwana wrote:Out of all the

luwana wrote:
Out of all the estimates i've seen I kept this in my notes...
for font size:
1em=12px
.8em=11px

Totally wrong. There is no such simple conversion.

I could change my plea to guilty,
but I don't think it would stick.

Ed Seedhouse

luwana
luwana's picture
rank newbie

newbie


Posts: 8
Joined: 2008-05-12
Location: las vegas

hmmm

Ed Seedhouse wrote:

Totally wrong. There is no such simple conversion.

Hmmm. I suppose we could use the calculator an figure that is the universal truth. Is that totally wrong too? Or might both of these be some sort of a reference point for all of us youngers?

gary.turner
gary.turner's picture
rank Moderator

Moderator


Posts: 5848
Joined: 2004-06-25
Location: Dallas

First, there is no universal

First, there is no universal conversion. Let me create an example.

We will accept the browser's default default value of 16px for the base text size.

body {font-size: 100%;}
p {font-size: 1em;} /*or, 16px*/
h3 {font-size: 1.25em;} /*or 20px*/

Now, in a certain section of the page, we want a smaller base size so we do this;

#special {font-size: .75em;} /*or 12px*/

Within #special, p's 1em will now be 12px, and h3's 1.25em will be 15px.

But we still want the larger font for h3, so

#special h3 {font-size: 1.667em;} /*uh huh, 20px*/
#special h3 span {font-size: 1em;} /*20px*/

cheers,

gary

A pianist who has learned the wrong system of fingering cannot become a virtuoso until he has laboriously taught himself the proper method.

My site is slo-o-owly being reconstructed; visit anyway.

Chris..S
Chris..S's picture
rank Moderator

Moderator


Posts: 5678
Joined: 2005-02-22

You do all realise this is a

You do all realise this is a pointless resurrection of a 10 month old thread. Eye-wink

gary.turner
gary.turner's picture
rank Moderator

Moderator


Posts: 5848
Joined: 2004-06-25
Location: Dallas

Oh, sure. Bring sanity to

Oh, sure. Bring sanity to the party.

cheers,

gary

A pianist who has learned the wrong system of fingering cannot become a virtuoso until he has laboriously taught himself the proper method.

My site is slo-o-owly being reconstructed; visit anyway.

Ed Seedhouse
Ed Seedhouse's picture
rank Guru

Guru


Posts: 2528
Joined: 2005-12-14
Location: Victoria British Columbia

luwana wrote:Ed Seedhouse

luwana wrote:
Ed Seedhouse wrote:

Totally wrong. There is no such simple conversion.

Hmmm. I suppose we could use the calculator an figure that is the universal truth. Is that totally wrong too? Or might both of these be some sort of a reference point for all of us youngers?

There is no "universal truth" and there is no defined relationship between pixel size and em size. There just isn't any, period. The web is not paper. Trying to make it into paper won't work.

Gary's calculations are fine if we know that every browser defaults to 16 pixels, but in fact we know that they don't! Macs default to 15. Lots of people override the defaults. I override Firefox to default to 20 pixels for 1em because I need that to read easily. A lot of people are over 40 these days and they have money to spend. The boomers won't all be dead any time soon and you ignore them at your peril. (I say them because I myself am not one, I'm too old.)

The web just isn't paper. Many people who browse the web don't see it at all because they are blind.

I could change my plea to guilty,
but I don't think it would stick.

Ed Seedhouse

Chris..S
Chris..S's picture
rank Moderator

Moderator


Posts: 5678
Joined: 2005-02-22

*sigh*. Gary, I'm not well

*sigh*.

Gary, I'm not well versed in forum lore, but isn't there some magic word you can include in a post to finally kill a thread?

Ed Seedhouse
Ed Seedhouse's picture
rank Guru

Guru


Posts: 2528
Joined: 2005-12-14
Location: Victoria British Columbia

Sorry - didn't realize it

Sorry - didn't realize it was supposedly dead and buried. With this incarnation of the system I just browse the most recent messages and if someone posts on an old one it never penetrates my skull that this is so.

I could change my plea to guilty,
but I don't think it would stick.

Ed Seedhouse

gary.turner
gary.turner's picture
rank Moderator

Moderator


Posts: 5848
Joined: 2004-06-25
Location: Dallas

Same with me, Ed. And, I

Same with me, Ed. And, I wasn't doing more than using the 16ppx starting point as a reference for the calculations. The point being that all em font metrics are relative to the element's parent size, and that 1em in one element could be a different size in another element.

And, that gives me the last word as I close comments. Smiling

cheers,

gary

A pianist who has learned the wrong system of fingering cannot become a virtuoso until he has laboriously taught himself the proper method.

My site is slo-o-owly being reconstructed; visit anyway.

Hugo
Hugo's picture
rank Moderator

Moderator


Posts: 12314
Joined: 2004-06-06
Location: London

Chris..S

Chris..S wrote:
*sigh*.

Gary, I'm not well versed in forum lore, but isn't there some magic word you can include in a post to finally kill a thread?

Hitler, Nazis.

Now it's closed. If anyone unsure of the meaning there is an old thread that can be resurrected on the subject Smiling

Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me