Use the <canvas> with either the canvas scripting API or the WebGL API to draw graphics and animations.

Content categories Flow content, phrasing content, embedded content, palpable content.
Permitted content Transparent but with no interactive content descendants except for <a> elements, <button> elements, <input> elements whose type attribute is checkbox, radio, or button.
Tag omission None, both the starting and ending tag are mandatory.
Permitted parents Any element that accepts phrasing content.
Permitted ARIA roles Any
DOM interface HTMLCanvasElement


This element's attributes include the global attributes.

The height of the coordinate space in CSS pixels. Defaults to 150.
Lets the canvas know whether or not translucency will be a factor. If the canvas knows there's no translucency, painting performance can be optimized. This is only supported by Mozilla-based browsers; use the standardized canvas.getContext('2d', { alpha: false }) instead.
The width of the coordinate space in CSS pixels. Defaults to 300.

Usage notes

Alternative content

You may (and should) provide alternate content inside the <canvas> block. That content will be rendered both on older browsers that don't support canvas and in browsers with JavaScript disabled.

Required </canvas> tag

Unlike the <img> element, the <canvas> element requires the closing tag (</canvas>).

Sizing the canvas

The displayed size of the canvas can be changed using a stylesheet. The image is scaled during rendering to fit the styled size. If your renderings seem distorted, try specifying your width and height attributes explicitly in the <canvas> attributes, and not using CSS.


This code snippet adds a canvas element to your HTML document. A fallback text is provided if a browser is unable to render the canvas, or if can't read a canvas. Providing a useful fallback text or sub DOM helps to make the the canvas more accessible.

<canvas id="canvas" width="300" height="300">
  An alternative text describing what your canvas displays. 

Then in the JavaScript code, call HTMLCanvasElement.getContext() to get a drawing context and start drawing onto the canvas:

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 100, 100);

Opaque canvas

If your canvas does not use transparency, you can tell the browser that your canvas is opaque, this will be used internally to optimize rendering. To do this, set alpha to false when getting the drawing context:

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d', { alpha: false });

Before the alpha option was standardized, you could use the moz-opaque attribute on the canvas tag. However, this only works in Mozilla-based rendering engines and should be avoided; check bug 878155 to track when this attribute will be removed.

<canvas id="myCanvas" moz-opaque></canvas>


Browser compatibility

Feature Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 1 Yes 1.51 2 3 9 9 24
height 1 Yes 1.51 2 3 9 9 24
moz-opaque No No 3.5 No No No
width 1 Yes 1.51 2 3 9 9 24
Feature Android webview Chrome for Android Edge mobile Firefox for Android IE mobile Opera Android iOS Safari
Basic support ? ? Yes 41 2 3 ? No 1
height ? ? Yes 41 2 3 ? No 1
moz-opaque No No No 4 No No No
width ? ? Yes 41 2 3 ? No 1

1. Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.

2. Prior to Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.

3. Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead.

4. Although early versions of Apple's Safari browser don't require the closing tag, the specification indicates that it is required, so you should be sure to include it for broadest compatibility. Versions of Safari prior to version 2 will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it.

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.