The <canvas> element is a bitmap drawing surface. Use it with the Canvas 2D API or WebGL to draw shapes, text, images, charts, games, and animations.
Alone it shows a blank rectangle. JavaScript selects the element, calls getContext("2d") (or a WebGL context), then draws. Put fallback content between the tags for environments without canvas support. For scalable, DOM-based graphics, prefer <svg>.
Baseline · Widely available.
Not deprecated. Stable across browsers since about July 2015 (and earlier in many engines).
Non-standard.
Do not use moz-opaque. Prefer canvas.getContext("2d", { alpha: false }) when you need an opaque buffer.
Accessibility note.
Pixels are not exposed as semantic HTML. Label the canvas and offer a text or table alternative for meaningful charts or UI.
Both start and end tags are required (unlike <img>). Defaults are width="300" and height="150" if omitted. Interactive content inside is mostly restricted; keep fallback simple.
Write fallback between the tags. Supporting browsers hide it once canvas renders.
Keep the message useful — describe what users would have seen.
Still draw with script when canvas is available.
Add aria-label (or a nearby text summary) for accessibility beyond fallback alone.
Edge Cases & Pitfalls
Most canvas bugs come from sizing, missing script, or inaccessible pixels.
sizing
CSS size ≠ buffer size
Styling only width/height in CSS scales the bitmap and can distort drawings. Set attributes (or JS properties) for the real pixel grid.
script
No drawing without JavaScript
A bare <canvas> stays blank. Always call getContext and draw after the element exists in the DOM.
a11y
Pixels are not semantic
Screen readers do not read drawn shapes. Label the canvas and provide a text or table alternative for important data.
limits
Maximum canvas size
Browsers and devices cap dimensions (often thousands of pixels; iOS can be stricter). Exceeding the limit makes drawing fail silently.
choice
Prefer SVG when it fits
Icons, logos, and accessible diagrams are often better as <svg>. Use canvas for pixel-heavy or high-frequency redraws.
Compatibility
Browser Support
The <canvas> element is core HTML graphics in every modern browser. Drawing still needs JavaScript in the page.
✓ Baseline · Widely available
<canvas>
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the element and 2D context. Internet Explorer 9+ also supports canvas.
98%Global support (approx.)
Google ChromeAll
Full support
Mozilla FirefoxAll
Full support
Apple SafariAll
Full support
Microsoft EdgeAll
Full support
OperaAll
Full support
Samsung InternetAll
Full support
Internet Explorer9+
Supported
<canvas>Widely supported
Bottom line: Safe to use for graphics everywhere modern browsers run. Always include fallback content and do not rely on non-standard attributes like moz-opaque.
Remember
Key Takeaways
Role:<canvas> is a blank bitmap; JavaScript draws the pixels.
Size: set width/height as numbers (default 300×150); avoid CSS-only sizing for the buffer.
API:getContext("2d") unlocks fillRect, arc, fillText, and more.
A11y: label the canvas and provide a non-canvas alternative for meaningful content.
One line: a good <canvas> has clear pixel size, script that draws, and a path for users who cannot see the bitmap.
UA style
Default CSS Settings
User-agent styles treat canvas like an inline replaced element. Typical defaults:
default.css
canvas {
display: inline;
}
The HTML width/height attributes (or their IDL properties) define the bitmap. CSS can change the displayed box, but mismatched CSS vs attribute size scales and may blur or stretch the drawing.
Frequently Asked Questions
The canvas element provides a bitmap drawing surface for graphics, charts, games, and animations. You draw on it with JavaScript using the Canvas 2D API or WebGL.
No. The element is only a blank surface. Shapes, text, and images appear only after script calls getContext and drawing methods.
They set the drawing buffer size in CSS pixels (default 300×150). Use numbers, not percentages. Prefer HTML attributes (or JS) over CSS sizing so drawings are not stretched.
Text or markup placed between the opening and closing canvas tags. Browsers that support canvas hide it; older browsers or environments without canvas show it.
Prefer canvas for pixel-heavy work like games, filters, and many dynamic charts. Prefer SVG for scalable icons, logos, and diagrams that benefit from DOM accessibility.
No. Drawn pixels are not exposed as semantic HTML. Add aria-label (or a caption), provide a text or table alternative for meaningful data, and avoid relying on canvas alone for critical UI.