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.

Examples

Given this <canvas> element:

<canvas id="canvas"></canvas>

... you can get a TextMetrics object using the following code:

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

let text = ctx.measureText('foo'); // TextMetrics object
text.width; // 16;

Specifications

Specification
HTML Living Standard
The definition of 'TextMetrics.width' in that specification.

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
width
Experimental
Chrome Full support 4Edge Full support 12Firefox Full support 1.5IE Full support 9Opera Full support 9Safari Full support 3.1WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 31Opera Android Full support YesSafari iOS Full support 3.2Samsung Internet Android Full support Yes

Legend

Full support
Full support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.

See also