The best way I've found is to use the CSS rule "display:inline;" to bind only to the textNode given HTML element, and then use the offsetWidth JS method.
<div style="display:inline;" id="gotWidthInPixels" >Some variable amount of text.</div> <script> //either: var widthInPixels= $('#gotWidthInPixels')[0].offsetWidth; //or var widthInPixels= document.getElementById("gotWidthInPixels").offsetWidth; </script>
This will give you the exact width in pixels of any HTML element.
source share