Calculate the width of the text in JavaScript
Last Updated :
12 Jul, 2025
Calculating the width of text in JavaScript refers to determining the pixel width that a specific string of text will occupy when rendered in a browser. This is commonly done using the Canvas API or by creating a hidden element with similar styling to measure the text.
Here we have some common approaches to calculating the width of the text in JavaScript
Method 1: Create a new DOM Element and measure its width
A temporary span element is created using createElement(), styled with the necessary font properties, and added to the document. The clientWidth property measures its width in pixels, after which the element is removed using removeChild(), ensuring accurate measurement.
Example: In this example, we calculate the pixel width of the text "Hello World" with a 16px font size. A temporary span is created, styled, measured using clientWidth, and displayed, before being removed from the document.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Calculate text width
with JavaScript
</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
Calculate text width
with JavaScript
</b>
<p>
Text to calculate width is
"Hello World"
</p>
<p>Font size of the text is 16px</p>
<p>
Width of the text is:
<span class="output"></span>
</p>
<button onclick="getTextWidth()">
Calculate text width
</button>
<script type="text/javascript">
function getTextWidth() {
text = document.createElement("span");
document.body.appendChild(text);
text.style.font = "times new roman";
text.style.fontSize = 16 + "px";
text.style.height = 'auto';
text.style.width = 'auto';
text.style.position = 'absolute';
text.style.whiteSpace = 'no-wrap';
text.innerHTML = 'Hello World';
width = Math.ceil(text.clientWidth);
formattedWidth = width + "px";
document.querySelector('.output').textContent
= formattedWidth;
document.body.removeChild(text);
}
</script>
</body>
</html>
Output:
Calculate the width of the text in JavaScriptMethod 2: Using the canvas measureText() Method
A canvas element is created, and its 2D context is accessed using getContext("2d"). The font is specified, and the measureText() method calculates the text dimensions, returning a TextMetrics object. The width property provides the text width in pixels.
Example: In this example we calculate the width of the text "Geeks For Geeks" with a 16px font size using the Canvas API's measureText() method and displays the width in pixels on the page.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Calculate text width with JavaScript</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>Calculate text width with JavaScript</b>
<p>
Text to calculate width is
"Geeks For Geeks"
</p>
<p>Font size of the text is 16px</p>
<p>
Width of the text is:
<span class="output"></span>
</p>
<button onclick="getTextWidth()">
Calculate text width
</button>
<script type="text/javascript">
function getTextWidth() {
inputText = "Geeks For Geeks";
font = "16px times new roman";
canvas = document.createElement("canvas");
context = canvas.getContext("2d");
context.font = font;
width = context.measureText(inputText).width;
formattedWidth = Math.ceil(width) + "px";
document.querySelector('.output').textContent
= formattedWidth;
}
</script>
</body>
</html>
Output:
Calculate the width of the text in JavaScript
Similar Reads
How to get the width of device screen in JavaScript ? Given an HTML document that is running on a device and the task is to find the width of the working screen device using JavaScript. Example 1: This example uses window.innerWidth to get the width of the device screen. The innerWidth property is used to return the width of the device. HTML<!DOCTYP
2 min read
How to calculate Width and Height of the Window using JavaScript ? In this article, we will know to calculate the width & the height of the window using Javascript. Given an HTML document that is running on a Window & we need to find the height and width of the window. The Window innerWidth property is used for returning the width of a windowâs content area
2 min read
How to get the Element's Actual Width and Height in JavaScript? In JavaScript, it is often necessary to retrieve the actual width and height of an HTML element. This information can be useful in a variety of scenarios, such as setting the size of an element dynamically, centering an element on the page, or determining the size of an element for animation purpose
5 min read
How to Set the Distance between Lines in a Text with JavaScript ? In this article, we will see how to create a user-defined space between two consecutive lines to beautify the view using JavaScript. To set the distance between lines, we can use the line height Property. The line-height property is used to set or return the distance between lines in a text. Syntax:
3 min read
How to get the Width of Scroll bar using JavaScript? Given an HTML document, the task is to get the width of the scrollbar using JavaScript. Approach:Create an element (div) containing a scrollbar.OffsetWidth defines the width of an element + scrollbar width.ClientWidth defines the width of an element.So scrollbar can be defined as width = offsetWidth
3 min read
How to find the height of a text in HTML canvas using JavaScript ? In this article, we will find the height of the text canvas using JavaScript. We have two approaches to find the height of a text in HTML canvas using JavaScript which are described below: Approach 1: In the following example, the height attribute of the HTML canvas is used. First set the font in pt
2 min read