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


readonly metrics.width;


Given this <canvas> element:

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

You can get a TextMetrics object using the following code:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var 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

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support4121.5993.1
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support2.1 ? ?4 Yes3.2 ?

See also

Document Tags and Contributors

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