HTML <canvas> Tag

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

Definition and Usage

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.

HTML Syntax

syntax.html
<canvas width=" 300" height="150">fallback</canvas>

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.

Attributes

Element-specific attributes on <canvas>:

AttributeDescriptionTry it
widthDrawing buffer width in CSS pixels. Default 300. Numbers only — not percentages.Try it
heightDrawing buffer height in CSS pixels. Default 150. Sets the coordinate grid with width.Try it
moz-opaqueNon-standard (Firefox). Opaque hint; use getContext("2d", { alpha: false }) instead.Avoid

Global Attributes

The <canvas> tag also supports the Global Attributes in HTML.

Live Preview

Change buffer size and fill color, then redraw a rectangle on a real <canvas>.

Attributes set the pixel buffer; script draws with fillRect.

Canvas

width="240" height="120" fillStyle="#2563eb"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Canvas and Rectangle

Create a canvas with pixel width/height, then fill a rectangle.

HTML
<canvas id="demo" width="300" height="150" aria-label="Light blue square">
  Canvas is not supported in this browser.
</canvas>
<script>
  const canvas = document.getElementById("demo");
  const ctx = canvas.getContext("2d");
  ctx.fillStyle = "lightblue";
  ctx.fillRect(50, 40, 100, 80);
</script>
Try It Yourself

How It Works

  1. Add <canvas>. Set numeric width and height for the pixel grid.
  2. Select the element in script (here with id="demo").
  3. Get a 2D context with getContext("2d").
  4. Draw. fillStyle picks the color; fillRect(x, y, w, h) paints pixels.

Example 2 — Rectangle and Text

Combine a filled rectangle with fillText for a simple label.

HTML
<canvas id="demo" width="320" height="160" aria-label="Blue box with Hello Canvas text"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(40, 40, 100, 70);
  ctx.font = "18px sans-serif";
  ctx.fillStyle = "#0f172a";
  ctx.fillText("Hello, Canvas!", 160, 85);
</script>
Try It Yourself

How It Works

  1. Draw the shape first with fillRect.
  2. Set font like CSS font shorthand (size + family).
  3. Change fillStyle again for text color.
  4. Call fillText(string, x, y) — y is roughly the text baseline.

Example 3 — Draw a Circle

Use a path with arc to draw a full circle, then fill.

HTML
<canvas id="demo" width="300" height="150" aria-label="Green circle"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.beginPath();
  ctx.arc(150, 75, 40, 0, 2 * Math.PI);
  ctx.fillStyle = "#16a34a";
  ctx.fill();
</script>
Try It Yourself

How It Works

  1. Start a path with beginPath().
  2. Define an arc: center (x, y), radius, start angle 0, end 2 * Math.PI (full circle).
  3. Set fillStyle for the fill color.
  4. Call fill() to paint the current path.

Example 4 — Simple Animation

Clear and redraw each frame with requestAnimationFrame.

HTML
<canvas id="demo" width="300" height="120" aria-label="Moving red square"></canvas>
<script>
  const canvas = document.getElementById("demo");
  const ctx = canvas.getContext("2d");
  let x = 0;
  function frame() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = "#dc2626";
    ctx.fillRect(x, 35, 40, 40);
    x += 2;
    if (x > canvas.width) x = -40;
    requestAnimationFrame(frame);
  }
  frame();
</script>
Try It Yourself

How It Works

  1. Clear the buffer each frame with clearRect(0, 0, width, height).
  2. Draw the square at the current x.
  3. Update position (x += 2) and wrap when it leaves the canvas.
  4. Schedule the next frame with requestAnimationFrame.

Example 5 — Fallback Content

Put alternative text inside <canvas> for unsupported browsers.

HTML
<canvas id="demo" width="300" height="150" aria-label="Demo chart">
  Your browser does not support the HTML canvas element.
</canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.fillStyle = "#bae6fd";
  ctx.fillRect(20, 20, 260, 110);
  ctx.fillStyle = "#0f172a";
  ctx.font = "18px sans-serif";
  ctx.fillText("Canvas works!", 90, 85);
</script>
Try It Yourself

How It Works

  1. Write fallback between the tags. Supporting browsers hide it once canvas renders.
  2. Keep the message useful — describe what users would have seen.
  3. Still draw with script when canvas is available.
  4. 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.

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 Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer 9+
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.

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.

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.

Next: caption

Add a title to a table with the HTML caption element.

caption tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful