The read-only width property of the TextMetrics interface contains the text's advance width (the width of that inline box) in CSS pixels.

Syntax

var width = metrics.width;

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 Status Comment
HTML Living Standard
The definition of 'TextMetrics.width' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic support
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 2Chrome Android ? Edge Mobile ? Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support 3.2Samsung Internet Android ?

Legend

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

See also

Document Tags and Contributors

Contributors to this page: mfluehr, fscholz, mattwojo
Last updated by: mfluehr,