Canvas & SVG in HTML

From the HTML cheat sheet ยท Media & Graphics ยท verified Jul 2026

Canvas & SVG

Graphics and drawing elements

css
<!-- Canvas for JavaScript drawing -->
<canvas id="myCanvas" width="400" height="300">
  Your browser doesn't support canvas.
</canvas>

<script>
  const canvas = document.getElementById('myCanvas');
  const ctx = canvas.getContext('2d');
  ctx.fillStyle = 'blue';
  ctx.fillRect(10, 10, 100, 100);
</script>

<!-- Inline SVG -->
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
  <circle cx="100" cy="100" r="80" fill="green" />
  <text x="100" y="100" text-anchor="middle" fill="white">SVG</text>
</svg>

<!-- SVG as image -->
<img src="graphic.svg" alt="SVG graphic">
html
<canvas id="canvas" width="200" height="100"></canvas>
<svg width="100" height="100">
  <rect x="10" y="10" width="80" height="80" fill="red"/>
</svg>
๐Ÿ’ก Canvas for dynamic/interactive graphics
๐Ÿ“Œ SVG for scalable vector graphics
โšก SVG better for icons, Canvas for games
๐ŸŸข Essential for data visualization
๐Ÿ”— Related: WebGL for 3D graphics
canvassvggraphics

More HTML tasks

Back to the full HTML cheat sheet