[thelist] Is there a way to calculate what the width of a displayed word?

Peter-Paul Koch pp.koch at gmail.com
Mon Oct 17 13:02:47 CDT 2005

> I have a list of words, any of which can be displayed within a bordered div.
> I want the bordered div to stay the same width no matter which word is in
> it, short or long.
> I imagine I can make the bordered div be an absolute width the size of the
> longest word in the list (unfortunately the font I am dealing with isnt
> monotype). How can I calculate what the widths of the longest word is?

Try putting a word in a span and read out the offsetWidth of this
span. Then try the next word, remember the highest value and set your
div's width to this value.

Not sure if this would work, but it's the only way I can think of.

ppk, freelance web developer

More information about the thelist mailing list