Converting Font-Size To Pixels

Discussion in 'Javascript' started by Ram, May 1, 2007.

  1. Ram

    Ram Guest

    Hey,
    I've got the following code:
    <font font-size="7">Hello</font>

    How can I calculate the font-size int the actual size in pixels?

    Thanks ahead!

    --sternr
    Ram, May 1, 2007
    #1
    1. Advertising

  2. Ram

    Ram Guest

    On May 1, 11:38 pm, Good Man <> wrote:
    > Ram <> wrote in news:1178029544.763823.300580
    > @c35g2000hsg.googlegroups.com:
    >
    > > Hey,
    > > I've got the following code:
    > > <font font-size="7">Hello</font>

    >
    > > How can I calculate the font-size int the actual size in pixels?

    >
    > > Thanks ahead!

    >
    > that code is insanely bad. you can't do it.
    >
    > you should be setting font sizes, and everything else related to display,
    > via CSS instead of in tags themselves.


    Hey,
    I know this is bad, but it's not my code - long story short - I cant
    change this html...
    isn't there a way of doing that?

    Thanks again

    --sternr
    Ram, May 2, 2007
    #2
    1. Advertising

  3. Ram

    marss Guest

    Ram wrote:
    >>Hey,
    >>I've got the following code:
    >><font font-size="7">Hello</font>


    I think here is a typo. The font element has no attribute "font-size".
    Correct is "size":
    <font size="7">Hello</font>

    >>How can I calculate the font-size int the actual size in pixels?


    >>Thanks ahead!


    >>--sternr

    You can try next approach.
    Create in runtime a font element, set it font size to known value in
    pixels and compare this element with the font element which size you
    want to define. Increase the size in series and at some point of time
    you will get required value.
    Short example:

    <html>
    <script type="text/javascript">
    function checkSize()
    {
    var fonts = document.getElementsByTagName("font");
    var checkedElem = fonts[0];
    var height = checkedElem.offsetHeight;
    for(var i = 0; i < 100; i++)
    {
    var tempFont = document.createElement("font");
    tempFont.style.fontSize = i + "px";
    tempFont.innerHTML = checkedElem.innerHTML;

    document.body.appendChild(tempFont);
    var tempHeight = tempFont.offsetHeight;
    document.body.removeChild(tempFont);

    if (tempHeight >= height)
    {
    alert("Font size = " + i + "px");
    break;
    }
    }
    }
    </script>
    <body onload="checkSize()">
    <font size="7">Hello</font>
    </body>
    </html>
    marss, May 4, 2007
    #3
  4. Ram

    töff Guest

    You would have to know the visitor's monitor resolution to know how
    many pixels relate to point size.
    töff, May 4, 2007
    #4
  5. Ram

    töff Guest

    Perhaps to you but not to many others.
    töff, May 5, 2007
    #5
  6. Ram

    töff Guest

    Who said I can't do "it"? What is "it" that I can't do? WTF are you
    talking about Randy.
    töff, May 5, 2007
    #6
  7. Ram

    töff Guest

    Oh you're the asshole I said I'd ignore, my bad, sorry. Nevermind and
    please continue to be a dick.
    töff, May 5, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dave richards
    Replies:
    2
    Views:
    609
    Paul Furman
    Feb 19, 2004
  2. Ram
    Replies:
    6
    Views:
    398
  3. mttc
    Replies:
    2
    Views:
    2,386
    Roedy Green
    Jul 3, 2009
  4. nutso fasst

    get font size in pixels - IE

    nutso fasst, Nov 28, 2004, in forum: Javascript
    Replies:
    3
    Views:
    98
    Martin Bialasinski
    Nov 28, 2004
  5. Cartoper

    getting the font size in pixels

    Cartoper, Sep 8, 2007, in forum: Javascript
    Replies:
    4
    Views:
    129
    Thomas 'PointedEars' Lahn
    Sep 8, 2007
Loading...

Share This Page