EN
JavaScript - measure text width
3 points
In this article, we would like to show you how to measure text width using JavaScript.
Quick solution:
xxxxxxxxxx
1
const canvas = document.createElement('canvas'); // helper object in memory
2
const context = canvas.getContext('2d'); // helper object in memory
3
4
context.font = `normal 12px Arial`;
5
6
const metrics = context.measureText('Hello World!');
7
8
console.log(`text width: ${metrics.width}px`); // text width: 65.1328125px
Canvas context object provides simple API to measure font metrics. It is necessary just to set specific font-family
, font-size
, font-weight
and use measureText()
method that returns metrics for indicated text (metrics like e.g. width
).
In this section, you can find simple tool that helps to measure text width using canvas object.
xxxxxxxxxx
1
const canvas = document.createElement('canvas'); // helper object in memory
2
const context = canvas.getContext('2d'); // helper object in memory
3
4
const createMeasurer = (fontFamily = 'Arial', fontSize = '12px', fontWeight = 'normal') => {
5
context.font = `${fontWeight} ${fontSize} ${fontFamily}`;
6
return (text) => {
7
const metrics = context.measureText(text);
8
return metrics.width;
9
};
10
};
11
12
13
// Usage example:
14
15
// font-family: Arial
16
// font-size: 30px
17
// font-weight: normal
18
19
const measureWidth = createMeasurer('Arial', '30px', 'normal');
20
const measuredWidth = measureWidth('Hello World!');
21
22
console.log(`text width: ${measuredWidth}px`); // text width: 162.83203125px
In this section, you can find solution that mounts invisible element with some text to measure text width. This solution is hack, but it works.
xxxxxxxxxx
1
const createMeasurer = (fontFamily = 'Arial', fontSize = '12px', fontWeight = 'normal') => {
2
const element = document.createElement('span');
3
element.style.padding = '0'; // we don't want to measure paddings if globally used
4
element.style.position = 'absolute'; // we don't want to impact on other body elements
5
element.style.right = '150%'; // we want to place element outside body area
6
element.style.bottom = '150%'; // we want to place element outside body area
7
element.style.visibility = 'hidden'; // we want to render invisible element
8
element.style.whiteSpace = 'nowrap'; // we want to render text without automatic break lines
9
element.style.fontFamily = fontWeight;
10
element.style.fontSize = fontSize;
11
element.style.fontWeight = fontWeight;
12
return (text) => {
13
element.textContent = text;
14
document.body.appendChild(element);
15
const width = element.offsetWidth;
16
document.body.removeChild(element);
17
return width;
18
};
19
};
20
21
22
// Usage example:
23
24
// font-family: Arial
25
// font-size: 30px
26
// font-weight: normal
27
28
const measureWidth = createMeasurer('Arial', '30px', 'normal');
29
const measuredWidth = measureWidth('Hello World!');
30
31
console.log(`text width: ${measuredWidth}px`); // text width: 158px
Note: this approach requires
<body>
element to be mount.