
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Calculate text width with JavaScript
To calculate text width, we can use the measureText() method in JavaScript. In canvas, the measureText() method is used to measure width of the text content. This means we can pass text to this canvas method and then call that method to measure the text. It will return an object that contains information about the measured text.
Sometimes, width may be a float value; hence the Math.ceil() method is used to round the value and returns an integer.
Syntax
Following is the syntax of the method used to calculate the text width -
const text = "Hello World!"; const canvas = document.createElement("canvas"); const ctx = canvas.getContext('2d'); let text = ctx.measureText(text); console.log(text.width);
Steps to Calculate Text Width
STEP 1 – Declare the text for that we want to calculate the width. Optionally we can display the text on the screen using the innerHTML property for better user understanding.
STEP 2 – Create a canvas element using the document.createElement() method.
STEP 3 – Get the context of the above-created canvas using canvas.getContext("2d").
STEP 4 – Measure the text width using context.measureText(text).width property.
STEP 5 – Calculate the final width using the Math.ceil(width).
STEP 6 – Finally display the final width of the text.
Example
Calculating the width of the text
In the example below, we calculate the width of the text. The text content is " Hello World!". You can change the text content and see the difference in the output.
<!DOCTYPE html> <html> <body> <h2>Calculating width of the text using canvas</h2> <p id="text" style="font-size : 20px"></p> <p>Width of the text is: <span class="output"></span> </p> <script> const text = "Hello World!"; document.getElementById("text").innerHTML = text; const canvas = document.createElement("canvas"); const context = canvas.getContext("2d"); const width = context.measureText(text).width; const finalWidth = Math.ceil(width) + "px"; document.querySelector('.output').textContent = finalWidth; </script> </body> </html>
In the above, we have created a canvas element and passed text to the canvas.measureText() method and that returns an object. From that we get the width of the selected text. Math.ceil() returns rounded value from the float value. The returned value is in pixels.
Example
Calculating the width of the HTML element containing text
We use the clientWidth and clientHeight properties to calculate the width of <p> element.
<!DOCTYPE html> <html> <body> <h1>Calculating width of the text</h1> <p id='text' style = "font-size:30px"> Text to calculate : "Hello World!" </p> <p>Width of the text is: <span class="output"></span> </p> <script> const text = document.getElementById("text"); const height = text.clientHeight const width = text.clientWidth document.querySelector('.output').textContent = width + " px"; </script> </body> </html>
Here, clientWidth property will give width of the text. We are getting text from p tag with an id "text".
In above, we saw how to calculate the width of the text using clientWidth and canvas.measureText() methods. Hope this article gives clarification on calculating the width of the text.
- Related Articles
- Python program to wrap a text into paragraph with width w
- How to toggle text with JavaScript?
- Align text and select boxes to the same width with HTML and CSS
- How to Match Width of Text to Width of Dynamically Sized Image/Title?
- How to set column width and column count with JavaScript?
- JAVA Program to Calculate Radius of Circle with Given Width and Height of Arc
- How to set the width of the right border with JavaScript?
- How to set the width of the left border with JavaScript?
- How to set the width of the bottom border with JavaScript?
- With JavaScript how to change the width of a table border?
- How to set the width of the top border with JavaScript?
- How to set the maximum width of an element with JavaScript?
- How to set the minimum width of an element with JavaScript?
- Replace HTML div into text element with JavaScript?
- How to set text in tag with JavaScript?
