<canvas>...</canvas>

HTML4 HTML5

Acts as a container for dynamically drawing 2D graphics and 3D objects in real time using scripts (usually JavaScript). Allows creating charts, animations, games, or image manipulation.

Both opening and closing tags are required. Content inside the element is only displayed in browsers that do not support this tag.

Attributes:

width="..." - Width of the canvas area in pixels (default 300px).

height="..." - Height of the canvas area in pixels (default 150px).

Example

Code:

<canvas id="myCanvas" width="200" height="100" style="border: 1px solid #000; background-color: #f8f9fa;"> Your browser does not support the canvas element.
</canvas>
<script>
  const canvas = document.getElementById('myCanvas');
  const ctx = canvas.getContext('2d');
  // Draw blue rectangle
  ctx.fillStyle = '#007bff';
  ctx.fillRect(20, 20, 80, 60);
  // Draw green circle
  ctx.beginPath();
  ctx.arc(150, 50, 30, 0, 2 * Math.PI);
  ctx.fillStyle = '#28a745';
  ctx.fill();
</script>

The browser will interpret this code as follows:

Your browser does not support the canvas element.