View Full Version : Calculating string width in pixels

10-13-2005, 07:36 PM
How would you go about calculating the width (in pixels) of a string of text, when the text could be any mix of font families, sizes or styles? The TextRange object in IE exposes a boundingWidth property that could prove useful, but as far as I can tell the TextRange object is not supported in FF or Safari and I need a cross browser solution. Any insight would be greatly appreciated. Thanks in advance.

10-14-2005, 02:45 AM
if you could give a little more background into what you need this for maybe someone can suggest a method that works?

*ie, if it is text within a span tag i can remember something about a element.scaleWidth ???

10-14-2005, 03:11 AM
Luckily, I discovered that I can simply copy a text string into a hidden span element and then use the span element's offsetWidth property to get the width of the string in pixles, which is exactly what I needed. Problem solved!

10-14-2005, 05:09 AM
offsetWidth........ dam, so close!