HTML <canvas> widthAttribute

HTML canvas 标签参考手册HTML <canvas> Tag

Example

A <canvas> element with a height and width of 200 pixels:

<canvas id="myCanvas" width="200" height="200" style="border:1px solid">

Try it Yourself »

Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

All major browsers support the width attribute.

Note:Internet Explorer 8 and earlier versions of IE do not support the <canvas> tag.


Definition and Usage

The width attribute returns the width of an ImageData object, in pixels.

Tip:Whenever the height or width of the canvas is reset, the canvas content is cleared (see the example at the bottom of the page).

Tip:Please refer to our HTML Canvastutorial to learn more about <canvas>.


Differences Between HTML 4.01 and HTML 5

<canvas> is a new tag in HTML5.


Syntax

<canvas width="pixels">

Attribute Values

Value Description
pixels Specifies the width of the canvas, in pixels (e.g., "100"). The default width is "300".


Examples

More Examples

Clear the Canvas
How to clear the canvas by resetting the width or height attribute (using JavaScript).


HTML canvas 标签参考手册HTML <canvas> Tag

Other Extensions