Web/API/TextMetrics/width

From Get docs


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 StandardThe definition of 'TextMetrics.width' in that specification.

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet

width

Experimental'

Chrome

Full support 4

Edge

Full support 12

Firefox

Full support 1.5

IE

Full support 9

Opera

Full support 9

Safari

Full support 3.1

WebView Android

Full support Yes

Chrome Android

Full support Yes

Firefox Android

Full support 31

Opera Android

Full support Yes

Safari iOS

Full support 3.2

Samsung 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