views:

3490

answers:

5

Hi, I need to convert <font size="10"> to px.

Example only(not correct): <font size="10"> is equivalent to 12px.

Is there any formula or table conversion out there to convert <font size="10"> to px?

Thanks :)

Kind Regards, Mark PHP Developer from Philippines

A: 

In general you cannot rely on a fixed pixel size for fonts, the user may be scaling the screen and the defaults are not always the same (depends on DPI settings of the screen etc.).

Maybe have a look at this (pixel to point) and this link.

But of course you can set the font size to px, so that you do know how many pixels the font actually is. This may help if you really need a fixed layout, but this practice reduces accessibility of your web site.

Lucero
A: 

This cant be answered that easy. This depends on the font used and the points per inch (ppi). This should give an overview of the problem.

Mork0075
+5  A: 

According to W3:

This attribute sets the size of the font. Possible values:

  • An integer between 1 and 7. This sets the font to some fixed size, whose rendering depends on the user agent. Not all user agents may render all seven sizes.
  • A relative increase in font size. The value "+1" means one size larger. The value "-3" means three sizes smaller. All sizes belong to the scale of 1 to 7.

Hence, the conversion you're asking for is not possible. The browser is not required to use specific sizes with specific size attributes.

Also note that use of the font element is discouraged by W3 in favor of style sheets.

Tormod Fjeldskår
I see, thanks Tormod. I am using eed3si9n's solution and this converter http://pxtoem.com/.Now it works. Thanks to all of you who answered me :D
marknt15
+2  A: 
<font size=1>- font size 1</font><br>
<span style="font-size:0.63em">- font size: 0.63em</span><br>

<font size=2>- font size 2</font><br>
<span style="font-size: 0.82em">- font size: 0.82em</span><br>

<font size=3>- font size 3</font><br>
<span style="font-size: 1.0em">- font size: 1.0em</span><br>

<font size=4>- font size 4</font><br>
<span style="font-size: 1.13em">- font size: 1.13em</span><br>

<font size=5>- font size 5</font><br>
<span style="font-size: 1.5em">- font size: 1.5em</span><br>

<font size=6>- font size 6</font><br>
<span style="font-size: 2em">- font size: 2em</span><br>

<font size=7>- font size 7</font><br>
<span style="font-size: 3em">- font size: 3em</span><br>
eed3si9n
Where did you get the font-size: values? How do you make sure that they are correct in all browsers?
pts
@pts, I got them by looking at the rendering of the browser. It's up to the browser's implementation, so I don't think I have to make sure correctness for all browsers. It looks close enough in all browsers I tested zoomed max.
eed3si9n
It appears to be correct in Safari and Firefox.
Chuck
Hhmm I will test this in different browsers :)
marknt15
@pts: What if I have font-size 8 and above? How will I know the equivalent em? Thanks.
marknt15
@marknt15, 8 and above act as same as 7.
eed3si9n
+1  A: 

Using the data points from the accepted answer you can use polynomial interpolation to obtain a formula.

WolframAlpha Input: interpolating polynomial {{1,.63},{2,.82}, {3,1}, {4,1.13}, {5,1.5}, {6, 2}, {7,3}}

Formula: 0.00223611x^6 - 0.0530417x^5 + 0.496319x^4 - 2.30479x^3 + 5.51644x^2 - 6.16717x + 3.14

And use in Groovy code:

import java.math.*
def convert = {x -> (0.00223611*x**6 - 0.053042*x**5 + 0.49632*x**4 - 2.30479*x**3 + 5.5164*x**2 - 6.167*x + 3.14).setScale(2, RoundingMode.HALF_UP) }
(1..7).each { i -> println(convert(i)) }
Stephen Swensen