You can get the width if you create a placeholder with all the same text formatting options and find its width.
For example, I will create a div with a .hidden
class that has the same attributes as the original div.
div.container { font-size: 16px; } div.hidden { font-size: 16px; display: none; }
Then, using jQuery, copy the contents of .container
to .hidden
and find the width of .hidden
:
$(function(){ $("div.container").each(function(){ $("body").append("<div class='hidden'>"+$(this).html()+"</div>"); var width = $("div.hidden").width(); $("div.width").html("Actual width: "+width+"px"); $("div.hidden").remove(); }); });
David source share