HTML Tags

HTML <canvas> Tag

The <canvas> tag in HTML is used to draw graphics dynamically via JavaScript, including shapes, charts, animations, and games.
It is a container for graphics but does not have any visible output by itself until you draw on it using JavaScript.

Syntax

<canvas id="canvasID" width="value" height="value">
  Fallback content if canvas is not supported.
</canvas>

Attributes

AttributeDescription
idSpecifies a unique identifier for the canvas.
widthWidth of the canvas in pixels (default is 300).
heightHeight of the canvas in pixels (default is 150).
classStandard class attribute for CSS styling.

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Canvas Tag Example</title>
  <style>
    canvas { border: 2px solid #29AB87; }
  </style>
</head>
<body>

  <h2>Drawing on Canvas</h2>
  <canvas id="myCanvas" width="400" height="200">
    Your browser does not support the HTML canvas tag.
  </canvas>

  <script>
    const canvas = document.getElementById('myCanvas');
    const ctx = canvas.getContext('2d');

    // Draw a rectangle
    ctx.fillStyle = "#FF0000";
    ctx.fillRect(50, 50, 150, 100);

    // Draw a circle
    ctx.beginPath();
    ctx.arc(300, 100, 50, 0, 2 * Math.PI);
    ctx.fillStyle = "#0000FF";
    ctx.fill();
  </script>

</body>
</html>

Output

Browser Output

Displays a red rectangle and a blue circle drawn dynamically on the canvas using JavaScript.
Use our Try It Editor to see it in action.

Browser Support

Chrome
Chrome
Firefox
Firefox
Edge
Edge
Safari
Safari
Opera
Opera
IE
IE9+
✅Yes✅Yes✅Yes✅Yes✅Yes✅Yes

Fully supported in modern browsers including Internet Explorer 9+.
Fallback content inside <canvas> is displayed if the browser does not support it.

Notes

  • The <canvas> tag cannot display content on its own — all graphics must be drawn with JavaScript.
  • Supports drawing 2D graphics via getContext('2d') and 3D graphics via WebGL (getContext('webgl')).
  • Useful for games, charts, visualizations, and dynamic graphics.
  • Always include fallback content for accessibility and unsupported browsers.
  • CSS can style the canvas border, background, or size but not its internal content — that must be drawn via JS.

Conclusion

The <canvas> tag is a versatile element for dynamic and interactive graphics in web pages.
It is fully supported across modern browsers, but requires JavaScript to render any visible content.

Sign In Form

User your email and password to singn in

Provide your email and we will send you a password reset link.

Don’t have an account, signup here : 

HTML5 & CSS3

Tools and Tutorials

Want a Website

Want a website for your business ?

Please fill out the details below, so we can reach out to you.

Registration Form

Signup to track your record and much more.

We have sent you an email with a registration link. Please click the link to verify your email address, or enter the 6-digit OTP provided in the email.

Didn't receive the OTP. Regenerate OTP Resend