Link to home
Start Free TrialLog in
Avatar of Knowledge-Dimension
Knowledge-Dimension

asked on

calculating pixels width based on number of characters

I have a drop down menu and we are trying to calculate pixel positioning of elements on the menu.

we have the length of a word but this is not the same as the pixel width of the word.

Is there a way to calculate the number of pixels a word would take , based on its width ?
Avatar of ShaneJones
ShaneJones
Flag of United Kingdom of Great Britain and Northern Ireland image

i just use guess work. Seems to work fine...

Shane
Avatar of jessegivy
Uhhh guess work will only work fine if you've got a monospace font like courier.  Fonts like Times New Roman, and most others for that matter are NOT monospace, they are true type, which means that different characters take up a different amount of screen real estate.  For instance a little i takes up only a pixel or two while an uppercase G takes up at least seven?

If precision is something you'll need in your calculation DEFFINATLY go with a monospace font!

Cheers,

JI
ASKER CERTIFIED SOLUTION
Avatar of Esopo
Esopo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you are still dealing with this and don't want to go with monospaced fonts,

Do a print-screen, blow it up and print it out to paper. Then do measurments with a ruler to figure out the dimensions and ratios and when you create your menu give the width dimension a relative number based on the size of the text (CSS ems would do well).

If your menus are being created dynamically you can use something like the GD library of PHP to figure out the exact width of a word or phrase in pixels.

I would normally recommend to just use a reasonable size and code your menu in such a way that would look well if a line needs to wrap into two.