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
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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
TextMetrics.width by Mozilla Contributors is licensed under CC-BY-SA 2.5.