[jQuery] Re: How to get font size in pixels?

2007-08-02 Thread Sam Collett

On Aug 1, 3:42 pm, "george.gsgd" <[EMAIL PROTECTED]> wrote:
> Perhaps think around the problem. Can you get the height of the
> element? That'd be in pixels regardless.
>
> Or maybe use sIFR instead?
>

sIFR was used, but now a server-side method generates the image (i.e.
so Flash isn't required and the amount the user has to download is
less, plus the page is faster).



[jQuery] Re: How to get font size in pixels?

2007-08-02 Thread Klaus Hartl


Sam Collett wrote:

Another problem is that it wouldn't work if you use % for font size
(which is true in my case).
Getting the height may be a good option (although that also includes
padding).


You simply need to use a reference element that doesn't have padding and 
borders.



--Klaus


[jQuery] Re: How to get font size in pixels?

2007-08-02 Thread Sam Collett

On Aug 1, 11:13 pm, Luke Lutman <[EMAIL PROTECTED]> wrote:
> I've run into this before, and came up with pretty much the same
> solution as Klaus:
>
> http://jquery.lukelutman.com/plugins/px/jquery.px.js
>
> which you can call like so:
>
> $('#example').px('font-size');
>
> One known bug is that it won't work for elements that don't accept
> content, like  or .
>
> Cheers,
> Luke

Another problem is that it wouldn't work if you use % for font size
(which is true in my case).
Getting the height may be a good option (although that also includes
padding).



[jQuery] Re: How to get font size in pixels?

2007-08-01 Thread Luke Lutman

Klaus Hartl wrote:
  > Couldn't you add an element as direct child to the body, for which you
> define:
> 
> position: absolut;
> visibility: hidden;
> height: 1em;
> font-size: 100% !important;
> 
> Then you can just poll the height of that element, e.g. the offsetHeight 
> and get the current pixel value. That would even work if the default 
> size has been changed.
> 
> And it would not only work for the body element, it should work 
> whereever you append that element. The jqem plugin works in a similiar 
> way and I think I have seen something like that somewhere else already, 
> maybe in YUI.

I've run into this before, and came up with pretty much the same 
solution as Klaus:

http://jquery.lukelutman.com/plugins/px/jquery.px.js

which you can call like so:

$('#example').px('font-size');

One known bug is that it won't work for elements that don't accept 
content, like  or .

Cheers,
Luke



[jQuery] Re: How to get font size in pixels?

2007-08-01 Thread Scott Sauyet


seedy wrote:

Sam Collett wrote:
How can I then convert the %/em value into pixels? 


Short Answer : You can't really convert em to pixels as em is a relative
value.


Will this help?:

http://davecardwell.co.uk/javascript/jquery/plugins/jquery-em/

  -- Scott



[jQuery] Re: How to get font size in pixels?

2007-08-01 Thread Klaus Hartl


seedy wrote:



Sam Collett wrote:
How can I then convert the %/em value into pixels? 



Short Answer : You can't really convert em to pixels as em is a relative
value.

Long Answer:
The default font size for browsers is 16px = 1em.  So you could make that
assumption and use 16px to calculate your pixel value, but if the user has
changed their browsers font size it could look wrong.  
Also, when font sizes are rendered in em, it could be inheriting from
another element 
body{font-size:0.69em}

element{font-size:0.75em}
In this case, if you only looked at the em value for element (0,75em), again
your calculation would be wrong.
Percentages will work mostly in this same way.  There is another unit of
font measurement you may have to worry about called ex, but I am not to
familiar with how that works.

Sorry I don't have an answer, as a consolation prize here is a nifty tool
for em -> px calculations.
http://riddle.pl/emcalc/



Couldn't you add an element as direct child to the body, for which you 
define:


position: absolut;
visibility: hidden;
height: 1em;
font-size: 100% !important;

Then you can just poll the height of that element, e.g. the offsetHeight 
and get the current pixel value. That would even work if the default 
size has been changed.


And it would not only work for the body element, it should work 
whereever you append that element. The jqem plugin works in a similiar 
way and I think I have seen something like that somewhere else already, 
maybe in YUI.



--Klaus


[jQuery] Re: How to get font size in pixels?

2007-08-01 Thread george.gsgd

Perhaps think around the problem. Can you get the height of the
element? That'd be in pixels regardless.

Or maybe use sIFR instead?

On Aug 1, 9:47 am, Sam Collett <[EMAIL PROTECTED]> wrote:
> When I use $("h1").css("font-size") it differs across browsers.
> Firefox returns it the way I want (in pixels), but Internet Explorer
> it returns it as returned from the stylesheet (so it could be % or
> em). How can I then convert the %/em value into pixels? I am doing
> image replacement (byt getting the text and passing it onto a server
> side image generation web handler) and as a result the font size is
> 76px rather than 24px (due to using var fontSize =
> parseInt($this.css("font-size")))



[jQuery] Re: How to get font size in pixels?

2007-08-01 Thread seedy



Sam Collett wrote:
> 
> How can I then convert the %/em value into pixels? 
> 

Short Answer : You can't really convert em to pixels as em is a relative
value.

Long Answer:
The default font size for browsers is 16px = 1em.  So you could make that
assumption and use 16px to calculate your pixel value, but if the user has
changed their browsers font size it could look wrong.  
Also, when font sizes are rendered in em, it could be inheriting from
another element 
body{font-size:0.69em}
element{font-size:0.75em}
In this case, if you only looked at the em value for element (0,75em), again
your calculation would be wrong.
Percentages will work mostly in this same way.  There is another unit of
font measurement you may have to worry about called ex, but I am not to
familiar with how that works.

Sorry I don't have an answer, as a consolation prize here is a nifty tool
for em -> px calculations.
http://riddle.pl/emcalc/


-- 
View this message in context: 
http://www.nabble.com/How-to-get-font-size-in-pixels--tf4198790s15494.html#a11945561
Sent from the JQuery mailing list archive at Nabble.com.