I want to know exact length in pixels of String i.e. "Hello World".
It should not be length of my container.
Any Help???
I want to know exact length in pixels of String i.e. "Hello World".
It should not be length of my container.
Any Help???
Put your text in a <span>
and use JavaScript to measure the width of your <span>
.
HTML:
<span id="text">Dummy text</span>
JavaScript:
var textWidth = document.getElementById("text").clientWidth;
jQuery:
$('#text').width();
You can create some inline element (span
, for example), set your string as element's content (innerHTML
), insert it into document (append to some element) and get it's width (clientWidth
).
There is no other way to measure length of string in pixels because it depends on font style.
Since the pixel length of the string will change depending on independently applied CSS attributes such as font and weight and size, I can't see this being possible in JS.