Loading...

HTML essentialsMedia

SVG, canvas & iframes

7 min lesson

Next
  • SVG, canvas and iframes let you add graphics and embedded content that HTML alone cannot produce.
  • SVG draws vector graphics with tags, so shapes stay sharp at any size.
  • Canvas is a drawing surface controlled by JavaScript, great for animations and games.
  • An iframe embeds another web page inside your page, like a window into another site.
  • Think of SVG as a drawing on paper (sharp at any zoom), canvas as a whiteboard you redraw with code, and an iframe as a window to a neighbor's room.
  • This builds on Responsive images: all three adapt to layout in different ways.
  • The example shows an inline SVG circle, a small canvas drawing and an embedded iframe; you will see all three on screen.
  • Next, Favicons & app icons adds the small icons that represent a site.
Keep in mind
  • Use SVG for icons, logos and charts that must stay sharp.
  • Use canvas for animations and heavy drawing where performance matters.
  • Use iframe only for content you cannot build in the page, and always add title.
  • Keep SVG small; remove unneeded editor metadata.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>SVG, canvas & iframe</title>
</head>
<body>
  <!-- An inline SVG circle: sharp at any size -->
  <svg width="100" height="100" viewBox="0 0 100 100">
    <circle cx="50" cy="50" r="40" fill="steelblue"></circle>
  </svg>

  <!-- A canvas: drawing happens with JavaScript -->
  <canvas id="board" width="200" height="100"></canvas>
  <script>
    const ctx = document.getElementById('board').getContext('2d');
    ctx.fillStyle = 'orange';
    ctx.fillRect(20, 20, 160, 60);
  </script>

  <!-- An iframe embeds another page; title is required for accessibility -->
  <iframe src="https://example.com" title="Example site" width="300" height="200"></iframe>
</body>
</html>

What happens: A page with a blue circle (100 by 100), an orange rectangle on a small canvas (200 by 100), and an embedded page showing the content of example.com inside a 300 by 200 frame.

Breaking it down

svg

<svg width="100" height="100">

A vector drawing area. Shapes inside are described by tags and stay sharp when scaled.

circle

<circle cx="50" cy="50" r="40">

A circle centered at (50,50) with radius 40. cx and cy are coordinates in the viewBox.

canvas

<canvas id="board" width="200" height="100">

An empty drawing surface. Nothing appears until JavaScript draws on it.

canvas draw

ctx.fillRect(20, 20, 160, 60);

Draws an orange rectangle on the canvas. The coordinates are in pixels from the top left.

iframe

<iframe src="..." title="...">

Embeds another page. The title is announced by screen readers and should describe the content.

SVG vs canvas vs iframe

ToolBest for
SVGIcons, logos, charts
canvasAnimations, games, dense drawing
iframeEmbedding another page
viewBoxScaling an SVG cleanly

A helpful analogy

SVG is like a drawing made of shapes on paper: it stays sharp at any zoom. Canvas is a whiteboard you redraw with code. An iframe is a window into a neighbor's room; you see their space without stepping into it.

Nesting rules

  • 1.SVG can be embedded directly in HTML as its own tag.
  • 2.Always give iframe a title; without it, screen reader users hear only "frame".

Common mistakes

Using canvas for static images

Avoid

canvas drawing a logo that never changes

Use instead

<img src="logo.svg">

Canvas needs JavaScript and is not accessible. A static SVG or image is simpler and sharper.

Leaving the iframe title empty

Avoid

<iframe src="..."></iframe>

Use instead

<iframe src="..." title="Example site"></iframe>

Without a title, screen readers cannot say what the frame contains, and the page is harder to navigate.

Quick tips

  • •Use viewBox on SVG so it scales smoothly with CSS width.
  • •Never embed a page you do not trust in an iframe; it can run scripts and track visitors.
Previous
Next