TextMetrics.width

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

Syntax

JavaScript
<var>readonly metrics</var>.width;

Examples

Given this <canvas> element:

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

You can get a TextMetrics object using the following code:

js;highlight:[5]
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var text = ctx.measureText("foo"); // TextMetrics object
text.width; // 16;

Specifications

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

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4.0 1.5 (1.8) 9.0 9.0 3.1
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 1.0 (1.8) ? 10.0 3.2

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/textmetrics/width

API Canvas Property Reference Référence TextMetrics