MC Emperor
MC Emperor

Reputation: 22977

How to get the maximum width of a single character in JavaScript?

How can I get the maximum width of a single character in JavaScript? I have a text painted in an unknown font, but I know what the text length and font size is.

Now I want the to get the largest character in that font, so I can make sure the parent element (a div for example) is not too narrow (it has an absolute width). The widest character is usually the M or W, but I'm not sure, perhaps there are fonts which paints wider characters.

Now how do I do that? Is there a fast method for?

Upvotes: 2

Views: 3488

Answers (1)

daiscog
daiscog

Reputation: 12057

Use the CSS width: 1em;. em is a relative measurement and is the width of the 'm' character

Upvotes: 5

Related Questions