Measuring length of string in pixel in javascript -


how find length of string in pixels in javascript , if know font-size , font-family?

the simplest solution create in memory canvas (i.e. 1 isn't added dom) , use measuretext function :

var canvas = document.createelement('canvas'); var ctx = canvas.getcontext("2d"); ctx.font = "11px arial";         var width = ctx.measuretext(str).width; 

Comments

Popular posts from this blog

matlab - How to equate a structure array to structure array -

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -