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


var width = metrics.width;


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;


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

Browser compatibility

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


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: mdnwebdocs-bot, mfluehr, fscholz, mattwojo
Last updated by: mdnwebdocs-bot,