I am trying to to calculate text width in px.
I will take a string from html, this string may contain some special characters as well (in this example just a hyphen), and make this string an html content of a div, and calculate string width.
But I am getting strange results.
If there is an better way to get text width, cross-browser!, please let me know.
I have made a fiddle:
Thank you!
Add
float: left;to#holder#holderwhich is the test container for width, is defaulting towidth: auto;. In other words, it is spanning 100% of the browser window and giving you the mixed results.You might consider adding
white-space: nowrap;as well to#holdershould it ever exceed the width of it’s container.Updated fiddle