Наші волонтери ще не переклали цю статтю мовою: Українська. Приєднуйтесь до нас і допоможіть зробити це!
Ви також можете прочитати статтю мовою English (US).

Use the HTML <canvas> element with the canvas scripting 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.
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. 

If your canvas does not use transparency set the moz-opaque attribute on the canvas tag. This information can be used internally to optimize rendering. However, this attribute has not been standardized and only works in Mozilla-based rendering engines.

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


Specification Status Comment
HTML Living Standard
The definition of '<canvas>' in that specification.
Living Standard  
The definition of '<canvas>' in that specification.
Recommendation Initial definition

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic Support1 (Yes)1.51 2 39924
height1 (Yes)1.51 2 39924
moz-opaque No No3.5 No No No
width1 (Yes)1.51 2 39924
FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic Support ? ? (Yes)41 2 3 ? No1
height ? ? (Yes)41 2 3 ? No1
moz-opaque No No No4 No No No
width ? ? (Yes)41 2 3 ? No1

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

Мітки документа й учасники

 Востаннє оновлена: fscholz,