Calculate the width of the text in JavaScript


There are multiple methods to calculate the width of the text in JavaScript. Here are some of them:

  • Using the offsetWidth property: The offsetWidth property returns the width of an element, including padding, border, and scrollbar (if any). We can create a temporary element, set its text content to the desired text, and then get its offsetWidth property to calculate the width of the text. Here's an example:
function getTextWidth(text, font) {
  // create a temporary span element
  var span = document.createElement('span');
  // set the font (optional)
  span.style.font = font;
  // set the text content
  span.textContent = text;
  // add the span to the document
  document.body.appendChild(span);
  // get the width of the span
  var width = span.offsetWidth;
  // remove the span from the document
  document.body.removeChild(span);
  // return the width
  return width;
}

// example usage
var text = 'Hello, world!';
var font = '20px Arial';
var width = getTextWidth(text, font);
console.log(width); // output: 116
  • Using the measureText() method: The measureText() method is a part of the Canvas API and returns a TextMetrics object that contains information about the measured text, such as its width. We can create a temporary canvas element, set its font, and then use the measureText() method to calculate the width of the text. Here's an example:
function getTextWidth(text, font) {
  // create a temporary canvas element
  var canvas = document.createElement('canvas');
  var context = canvas.getContext('2d');
  // set the font
  context.font = font;
  // measure the text
  var metrics = context.measureText(text);
  // return the width
  return metrics.width;
}

// example usage
var text = 'Hello, world!';
var font = '20px Arial';
var width = getTextWidth(text, font);
console.log(width); // output: 116
  • Using the getBoundingClientRect() method: The getBoundingClientRect() method returns a DOMRect object that contains information about the position and size of an element relative to the viewport. We can create a temporary element, set its text content and font, and then use the getBoundingClientRect() method to calculate the width of the text. Here's an example:
function getTextWidth(text, font) {
  // create a temporary span element
  var span = document.createElement('span');
  // set the font
  span.style.font = font;
  // set the text content
  span.textContent = text;
  // add the span to the document
  document.body.appendChild(span);
  // get the bounding rectangle of the span
  var rect = span.getBoundingClientRect();
  // remove the span from the document
  document.body.removeChild(span);
  // return the width
  return rect.width;
}

// example usage
var text = 'Hello, world!';
var font = '20px Arial';
var width = getTextWidth(text, font);
console.log(width); // output: 116


About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.