Inline SVG and Canvas

Intermediate · 10 min

Compare inline SVG, svg in an img, and canvas: what CSS and scripts can reach, how each appears to assistive technology, and when to pick which.

Before thisIframes and Embedded ContentImages and Alt Text

CSSnippets · 10 min

Inline SVG and Canvas

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CSSnippetsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • SVG is markup for vector shapes. Placed inline it becomes part of your DOM, so CSS and scripts reach every shape. Loaded through `<img src>` it is a sealed picture.
  • In a test, a CSS rule coloured a `circle` in inline SVG blue, and `fill="currentColor"` took the parent's text colour. The same SVG in an `<img>` drew black and was not in `document`.
  • An inline `<svg>` is exposed as an unnamed image unless you name it. Meaningful icons need `role="img"` and a label or `<title>`. Decorative ones need `aria-hidden="true"`.
  • `<canvas>` is a bitmap you paint with JavaScript. It defaults to 300 by 150 pixels, and CSS only stretches those pixels. Its drawing is invisible to the DOM and the accessibility tree.
  • Use SVG for icons, logos, diagrams and charts you want to style, inspect and label. Use canvas for pixel-heavy or fast-changing drawing, with a text alternative inside it.

Two ways to draw in a page

An <img> shows a finished picture. Sometimes you need the page to draw: an icon that changes colour on hover, a chart built from data, a game. HTML has two drawing surfaces, and the difference between them is the most useful thing to know.

SVG (Scalable Vector Graphics) describes shapes as markup: a circle, a path, some text. The browser keeps those shapes as elements and can redraw them at any size. Canvas gives you an empty grid of pixels and a JavaScript API to paint on it. After painting, the browser remembers pixels and nothing else. The first is like a diagram made of labelled cut-outs that you can rearrange. The second is like a painting: you can add paint, but you can’t pick up one brushstroke.

SVG: inline, or in an img

You can use SVG two ways, and they behave differently. I tested both in headless Chromium 159:

<svg viewBox="0 0 10 10" width="100" height="100">
  <circle cx="5" cy="5" r="4" fill="currentColor"/>
</svg>

<img src="icon.svg" alt="">
Inline <svg> <img src="icon.svg">
In the DOM yes, circle was found by querySelector no, one img element
CSS rule on circle applied, fill became blue cannot reach it
currentColor took the parent’s red text colour drew black
Scripts reach shapes yes no, contentDocument was undefined

So inline SVG is part of your page, and an SVG in an img is a sealed picture, cached like any image file. Choose by need: styling and interaction, inline. A big illustration you just show, img. The video builds up the vocabulary for either: shapes and styles, the viewport and viewBox (the coordinate system that makes SVG scale), gradients, text and the path element with its d attribute. The CSS you’d use on inline SVG is ordinary CSS, and SVG-specific clipping is in clip-path, masks and blend modes.

SVG and accessibility

An inline <svg> shows up in the accessibility tree whether you want it or not, so name it or hide it. In a test:

  • <svg role="img" aria-label="Warning"> became img "Warning".
  • <svg role="img"><title>Titled icon</title>... became img "Titled icon".
  • <svg aria-hidden="true"> disappeared from the tree.
  • A plain <svg> with nothing became an unnamed img.
  • A <button> containing a hidden svg and a text span was named by the text, button "Close". A <button aria-label="Menu"> that held a plain svg got an extra unnamed img inside it.

The rule: a meaningful graphic gets role="img" and a name. A decorative one, such as an icon beside a text label, gets aria-hidden="true". The accessible names lesson goes deeper.

Canvas: a bitmap you script

<canvas id="c" width="300" height="150">Fallback: chart of monthly sales</canvas>
<script>
  const ctx = document.getElementById("c").getContext("2d");
  ctx.fillRect(10, 10, 50, 50);
</script>

Canvas needs JavaScript. Key facts, checked in the test:

  • The default size is 300 by 150 pixels. The width and height attributes set the bitmap, and CSS only scales it. A canvas with width=200 height=100 and CSS width: 600px measured 600 by 300 on screen while its bitmap stayed 200 by 100, so the picture is stretched and soft. For crisp output, set the attributes to the displayed size times devicePixelRatio.
  • What you draw isn’t in the DOM. After drawing, canvas.querySelectorAll("*") found nothing and the accessibility tree held nothing for it.
  • The content between the tags is fallback. In a test, text inside a canvas appeared in the tree as plain text, and role="img" with aria-label became a named image. A fallback <button> inside a canvas did not appear in my snapshot, so check interactive fallbacks in a real screen reader.

Choosing

If shapes should be styled, inspected, labelled or animated individually, with a few hundred elements at most, use SVG. If you’re painting thousands of moving objects, processing pixels or running a game loop, canvas is faster, because it holds no per-shape objects. The cost is that you rebuild accessibility yourself. Scripting the canvas API is a JavaScript topic and is outside this roadmap.

What the video covers

The embed is CSSnippets’ “SVG Basics to Advanced in 10 Minutes | Easy SVG Tutorial” (August 2025, about 10 minutes). Its chapters are introduction (0:00), SVG shapes and styles (0:46), the SVG viewport (3:03), the viewBox (3:48), gradients (6:53), text (8:25) and path (9:09). It covers SVG only: nothing on canvas, inline versus img, or accessibility. I could not read a transcript, so I describe only the chapters. The comparison table, the accessibility results and the canvas facts are this lesson’s own. The deeper link covers canvas, from a small channel with a brand-new 2026 series.

Where this fits

This closes the images and media group, drawing on images and alt text and iframes. The next group, tables, starts with table structure. Canvas and SVG meet accessibility again in the accessibility tree.

Check yourself

  1. 01You want an icon to take the colour of the button it sits in. Inline SVG or `<img src="icon.svg">`?

    Inline SVG with `fill="currentColor"`. In a test the inline version drew in the parent's red text colour, and the same file in an `<img>` drew black because the picture cannot see the page's CSS. Alternatively use a CSS mask, but inline is the simplest.

  2. 02An icon-only button contains an inline `<svg>` and has no text. How do you name the button?

    Give the button an `aria-label`, or add visually hidden text, and mark the `svg` `aria-hidden="true"` so it is not announced as a stray image. In a test a button with a hidden svg and a text span was named by the text, while a `<button aria-label>` holding a plain svg also exposed an unnamed `img` inside it.

  3. 03A canvas chart is 600px wide in CSS but `width="200"` in the attribute. What do you see?

    A blurry chart. The `width` and `height` attributes set the bitmap size, 200 by 100 in the test, and CSS only scales it up to 600 by 300. Set the attributes to the real pixel size, multiplied by the device pixel ratio for sharp lines.

  4. 04How does a screen reader user get the content of a `<canvas>` drawing?

    Only through what you provide: fallback text inside the element, or `role="img"` with `aria-label`. In a test the drawn pixels were not exposed, fallback text appeared in the tree, and `role="img" aria-label` became a named image. Anything interactive needs real elements as well.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark