Calculating text width

react calculate text width
calculate text width online
react-native measure text width
get width of text on canvas
javascript measure text height
canvas string width
jquery calculate width of text
get width of filltext

I'm trying to calculate text width using jQuery. I'm not sure what, but I am definitely doing something wrong.

So, here is the code:

var c = $('.calltoaction');

var cTxt = c.text();

var cWidth =  cTxt.outerWidth();

c.css('width' , cWidth);

This worked better for me:

$.fn.textWidth = function(){
  var html_org = $(this).html();
  var html_calc = '<span>' + html_org + '</span>';
  var width = $(this).find('span:first').width();
  return width;

Calculate the width of the text in JavaScript, Calculate the width of the text in JavaScript. Method 1: Creating a new DOM Element and measuring its width: A new “span” element is created with the  The text to be measured is passed to this method. It returns a TextMetrics object that contains information about the measured text. The width property of this TextMetrics object is used to get the width of the text. The width may be a floating-point value, hence the Math.ceil() function is used to find the ceiling of the floating-point and

Here's a function that's better than others posted because

  1. it's shorter
  2. it works when passing an <input>, <span>, or "string".
  3. it's faster for frequent uses because it reuses an existing DOM element.


// Calculate width of text from DOM element or string. By Phil Freo <>
$.fn.textWidth = function(text, font) {
    if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
    $.fn.textWidth.fakeEl.text(text || this.val() || this.text()).css('font', font || this.css('font'));
    return $.fn.textWidth.fakeEl.width();

Calculate text width with JavaScript., To calculate text width, use the Canvas.measureText() method in JavaScript. You can try to run the following code to get the text width:Example  To calculate text width, use the Canvas.measureText() method in JavaScript. You can try to run the following code to get the text width:Example<!DOCTYPE html> <

My solution

$.fn.textWidth = function(){
    var self = $(this),
        children = self.children(),
        calculator = $('<span style="display: inline-block;" />'),

    width = children.parent().width(); // parent = the calculator wrapper
    return width;

Basically an improvement over Rune's, that doesn't use .html so lightly

TextMetrics.width, The read-only width property of the TextMetrics interface contains the text's advance width (the width of that inline box) in CSS pixels. Definition and Usage. The measureText() method returns an object that contains the width of the specified text, in pixels. Tip: Use this method if you need to know the width of a text, before writing it on the canvas.

The textWidth functions provided in the answers and that accept a string as an argument will not account for leading and trailing white spaces (those are not rendered in the dummy container). Also, they will not work if the text contains any html markup (a sub-string <br> won't produce any output and &nbsp; will return the length of one space).

This is only a problem for the textWidth functions which accept a string, because if a DOM element is given, and .html() is called upon the element, then there is probably no need to fix this for such use case.

But if, for example, you are calculating the width of the text to dynamically modify the width of an text input element as the user types (my current use case), you'll probably want to replace leading and trailing spaces with &nbsp; and encode the string to html.

I used philfreo's solution so here is a version of it that fixes this (with comments on additions):

$.fn.textWidth = function(text, font) {
    if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').appendTo(document.body);
    var htmlText = text || this.val() || this.text();
    htmlText = $.fn.textWidth.fakeEl.text(htmlText).html(); //encode to Html
    htmlText = htmlText.replace(/\s/g, "&nbsp;"); //replace trailing and leading spaces
    $.fn.textWidth.fakeEl.html(htmlText).css('font', font || this.css('font'));
    return $.fn.textWidth.fakeEl.width();

Looking for library to calculate text width in pixels given font and font , Has anyone used a library that calculate the pixel width of a text base on font and font-size? You can use CDC::GetTextExtent to calculate the width of text in a certain font. Use CWnd::GetDC to get the Device Context from the control displaying the text.

jQuery's width functions can be a bit shady when trying to determine the text width due to inconsistent box models. The sure way would be to inject div inside your element to determine the actual text width:

$.fn.textWidth = function(){
  var sensor = $('<div />').css({margin: 0, padding: 0});
  var width = sensor.width();
  return width;

To use this mini plugin, simply:


Calculate width of text from a DOM element or string. (Example), Calculate width of text from DOM element or string. By Phil Freo <http://philfreo.​com> $.fn.textWidth = function(text, font) { if (!$.fn.textWidth. If the width parameter is less than the actual width of the string, the returned Width component is truncated to a value representing the maximum number of characters that will fit within the specified width.

Pixie, <h3>Text Block Pixel Width</h3>. 5. </aside>. 6. <header>. 7. <label>. 8. <em>​font-size</em><br />. 9. <input class="size" type="text" value="calc(1em + 3vmin)"​  If you know the area and the length but not the width, you can also find the width by dividing the area by the width. For example, if you know you want a room to be 105 square feet and you know it will be 15 feet long, divide 105 by 15 to get 7 so the width of the room would be 7 feet.

Calculate text dynamic width Javascript - Need help, Hello, can someone show me how to change this into javascript to bubble element function. so I can use the published value to set dynamic  Ever wanted to quickly find the bytesize of a snippet of text? Now you can, as quickly and easily as possible. This is especially useful for front-end developers, since in JavaScript, HTML or CSS, bytesize really matters a lot.

How do I calculate text width in pixels, Like for instance: If i type in text object "HELLO WORLD" based on each character width its will automaticly calculate the entire width of a string  Set the width of an <input type="text"> element to 100px. However, when it gets focus, make it 250px wide: input [type=text] {. width: 100px; } input [type=text]:focus {. width: 250px; } Try it Yourself ».

  • So, in what way is that code not working? What does it need to do differently?
  • +1 - this truly measures the text, not just the containing block element like brainreavis' solution.
  • Slick. +1. It looks like you are missing a semi colon on the third line after '</span>', though it doesn't seem to make a difference (worked with or without it on FF9).
  • Be careful with this though... this method will unbind any events on child elements.
  • I have tried to use this function in a number of ways, but it keeps returning null. Could someone please post the syntax for how this function is actually used? Do I run the .textWidth() function on a jQuery object? Do I pass text to this function? Do I run the function as a jQuery function (ie $.textWidth(jqObject)? Because none of those options seems to work. Thanks...
  • @moosefetcher you would do $(selector).textWidth(); Look here…
  • Wow! Pure awesomeness!
  • Great solution, should be accepted answer, I think! Thanks, pal!
  • Fantastico! Simple and clean.
  • Whilst good, this doesn't handle spaces. See @edsioufi's answer for an enhancement to this solution.
  • If there's hidden text inside the element, then it will skew your width. To account for this, be sure to do remove the hidden nodes before calculating the text width. To safely remove the hidden nodes, you should probably make a clone first and do the removal there.