Canvas - The Graphics Canvas Element - HTML - HyperText Markup Language - MDN
Canvas - The Graphics Canvas Element - HTML - HyperText Markup Language - MDN
Attributes
This element's attributes include the global attributes.
height
Lets the canvas know whether 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.
width
Usage notes
Alternative content
You 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.
Closing </canvas> tag
Unlike the <img> element, the <canvas> element requires the closing tag ( </canvas> ).
Sizing the canvas using CSS versus HTML
The displayed size of the canvas can be changed using CSS, but if you do this the image is scaled during rendering to fit the styled size,
which can make the final graphics rendering end up being distorted.
It is better to specify your canvas dimensions by setting the width and height attributes directly on the <canvas> elements, either directly
in the HTML or by using JavaScript.
Maximum canvas size
The exact maximum size of a <canvas> element depends on the browser and environment. While in most cases the maximum dimensions
exceed 10,000 x 10,000 pixels, notably iOS devices limit the canvas size to only 4,096 x 4,096 pixels. See canvas size limits in different
browsers and devices .
Note: Exceeding the maximum dimensions or area renders the canvas unusable — drawing commands will not work.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas 1/4
28/2/24, 11:32 <canvas>: The Graphics Canvas element - HTML: HyperText Markup Language | MDN
work, other UI elements of your web application will remain responsive even if you are running complex graphics on an offscreen canvas.
For more information, see the OffscreenCanvas API documentation.
Examples
HTML
This code snippet adds a canvas element to your HTML document. A fallback text is provided if a browser is unable to read or render the
canvas.
HTML Play
JavaScript
Then in the JavaScript code, call HTMLCanvasElement.getContext() to get a drawing context and start drawing onto the canvas:
JS Play
Result
Play
Accessibility concerns
Alternative content
The <canvas> element on its own is just a bitmap and does not provide information about any drawn objects. Canvas content is not
exposed to accessibility tools as semantic HTML is. In general, you should avoid using canvas in an accessible website or app. The
following guides can help to make it more accessible.
Canvas accessibility use cases
Canvas element accessibility issues
HTML Canvas Accessibility in Firefox 13 – by Steve Faulkner
Best practices for interactive canvas elements
Technical summary
Content Flow content, phrasing content, embedded content, palpable content.
categories
Permitted Transparent but with no interactive content descendants except for <a> elements, <button> elements, <input>
content elements whose type attribute is checkbox , radio , or button .
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas 2/4
28/2/24, 11:32 <canvas>: The Graphics Canvas element - HTML: HyperText Markup Language | MDN
Tag omission None, both the starting and ending tag are mandatory.
Permitted Any element that accepts phrasing content.
parents
Implicit ARIA No corresponding role
role
Permitted ARIA Any
roles
DOM interface HTMLCanvasElement
Specifications
Specification
HTML Standard
# the-canvas-element
Browser compatibility
Report problems with this compatibility data on GitHub
diordnA arepO
emorhC
xoferiF
arepO
irafaS
egdE
See also
Canvas API
Canvas tutorial
OffscreenCanvas
Canvas cheat sheet (2009)
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas 3/4
28/2/24, 11:32 <canvas>: The Graphics Canvas element - HTML: HyperText Markup Language | MDN
WebGL API
<img>
SVG
Using HTML audio and video
This page was last modified on Feb 22, 2024 by MDN contributors.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas 4/4