So far, all of The Blue Cup's images are files that someone created outside the code: the photo of the front of the café, the product photos, the PNG logo. But HTML also lets you draw. With SVG you describe shapes (rectangles, circles, lines, curves) with tags, just as you describe paragraphs and lists, and the result looks sharp at any size. With canvas you get a blank drawing surface on which JavaScript paints pixel by pixel, ideal for charts generated from data. In this lesson, the last in the module, you will learn the basic SVG elements and how to make them accessible, you will draw a cup icon for The Blue Cup, you will get to know the canvas element and its 2D context, and you will turn the "Most ordered" list in the news into a bar chart. By the end you will know how to choose between SVG and canvas in each situation.

Contents

  1. Vector graphics and bitmap graphics
  2. Inline SVG versus img src=".svg"
  3. The coordinate system: svg, width, height and viewBox
  4. Basic shapes: rect, circle, line and text
  5. Fill and stroke: fill and stroke
  6. Free-form shapes with path
  7. Accessible SVG
  8. Example: a cup icon
  9. The canvas element
  10. The 2D context: drawing with JavaScript
  11. Example: a bar chart explained line by line
  12. Fallback content and canvas accessibility
  13. SVG or canvas: when to use each one
  14. Project: The Blue Cup's icon, divider and chart

Vector graphics and bitmap graphics

In Adding Images you met the JPEG, PNG, WebP and SVG formats. The fundamental difference between them is how they store the image:

Bitmap (JPEG, PNG, WebP, canvas) Vector (SVG)
What is stored A grid of pixels with their colour Instructions: "a circle with a radius of 10 at such-and-such a position"
When enlarged It becomes pixelated or blurry Always sharp
File size Depends on the size in pixels Depends on the number of shapes
Ideal for Photographs, complex images Logos, icons, simple illustrations, diagrams
Can be edited with a text editor No Yes: it's code

SVG (Scalable Vector Graphics) is a markup language, a cousin of HTML: it uses tags and attributes. That's why you can write it by hand and put it straight into your pages.

Inline SVG versus img src=".svg"

An SVG graphic can reach the page in two ways:

1. As an external file, with img (what you already do with the gallery divider):

<img src="img/bean-divider.svg" alt="" width="120" height="20">

2. Inline, by writing the SVG code inside the HTML:

<svg width="40" height="40" viewBox="0 0 40 40">
  <circle cx="20" cy="20" r="18" fill="#1f4e79" />
</svg>
img src=".svg" Inline SVG
Where the code is In a separate file Inside the HTML
Browser cache Yes: it's downloaded once and reused No: it's repeated on every page
Can be changed with the page's CSS No Yes (colours, on hover…)
Can be manipulated with JavaScript No Yes
Accessibility alt, like any image role, title and ARIA attributes
Ideal for Illustrations, images repeated across many pages, favicon Small icons you want to colour or animate, interactive graphics

When you save an SVG as a standalone file, the svg tag must declare its namespace: xmlns="http://www.w3.org/2000/svg". Inside an HTML document it isn't necessary, because the browser already knows that svg is SVG. We will include it in files and leave it out inline.

The coordinate system: svg, width, height and viewBox

Every SVG drawing lives inside an svg element, which works like a sheet of graph paper:

<svg width="200" height="100" viewBox="0 0 200 100">
  <!-- shapes -->
</svg>
  • width and height: the size at which it's displayed on the page, in pixels. As with images, it's a good idea always to include them.
  • viewBox="x y width height": the area of the "paper" that is visible. Here, from the point (0, 0) to 200 units wide and 100 high.
  • The origin (0, 0) is in the top left-hand corner. x increases to the right and y increases downwards (the opposite of graphs in maths).
graph LR
    A["(0, 0) top left-hand corner"] -->|"x increases to the right"| B["(200, 0)"]
    A -->|"y increases downwards"| C["(0, 100)"]

Why is viewBox so important? Because it separates the drawing units from the on-screen size. You can draw an icon on a 64×64-unit grid and display it at 32, 64 or 512 pixels: the browser scales it without any loss of sharpness.

<!-- The same drawing, at three sizes -->
<svg width="32" height="32" viewBox="0 0 64 64">…</svg>
<svg width="64" height="64" viewBox="0 0 64 64">…</svg>
<svg width="256" height="256" viewBox="0 0 64 64">…</svg>

Basic shapes: rect, circle, line and text

<svg width="300" height="120" viewBox="0 0 300 120">
  <rect x="10" y="10" width="80" height="60" rx="8" fill="#c8d3e0" />
  <circle cx="150" cy="40" r="30" fill="#1f4e79" />
  <line x1="200" y1="10" x2="290" y2="70" stroke="#1f4e79" stroke-width="4" />
  <text x="10" y="105" font-family="sans-serif" font-size="18" fill="#1f4e79">The Blue Cup</text>
</svg>
Element Main attributes What it draws
rect x, y (top left-hand corner), width, height, rx (corner rounding) Rectangle
circle cx, cy (centre), r (radius) Circle
ellipse cx, cy, rx, ry (horizontal and vertical radii) Ellipse
line x1, y1 (start), x2, y2 (end) Straight line (needs stroke to be visible)
text x, y (start of the text's baseline), font-size, font-family Text
path d (instructions) Any shape (you will see it shortly)

Important details:

  • SVG shapes are void elements: they are closed with />. In SVG, unlike HTML void elements, that slash is the usual way of writing them.
  • Numbers have no unit: they are viewBox units.
  • Order matters: whatever is written later is drawn on top.
  • SVG text is real text: it can be selected, search engines find it and screen readers read it.

Fill and stroke: fill and stroke

Attribute What it controls Example
fill Colour of the inside fill="#1f4e79", fill="none" (no fill)
stroke Colour of the outline stroke="#1f4e79"
stroke-width Thickness of the outline stroke-width="4"
stroke-linecap Shape of the line ends butt (flat), round (rounded), square
opacity Transparency (0 to 1) opacity="0.5"
  • By default, the fill is black and there is no stroke. That's why a line without a stroke is invisible.
  • Colours are written as in CSS: #1f4e79, names such as white, or rgb(31, 78, 121).
  • A particularly useful value is fill="currentColor": the drawing takes on the colour of the text around it. That way, an inline icon changes colour along with the text when you apply CSS in module 8.

Free-form shapes with path

The path element draws any shape using a sequence of commands in the d attribute. Each command is a letter followed by coordinates, as if you were guiding a pencil:

Command Meaning Example
M x y Move: moves the pencil without drawing M 10 10
L x y Line: straight line to that point L 50 10
H x / V y Horizontal / vertical line H 90, V 40
Q cx cy x y Curve: it bends towards the control point (cx, cy) and ends at (x, y) Q 20 30 10 50
Z Closes the shape by returning to the starting point Z
<svg width="100" height="60" viewBox="0 0 100 60">
  <path d="M 10 50 L 50 10 L 90 50 Z" fill="#c8d3e0" stroke="#1f4e79" stroke-width="2" />
</svg>
  • M 10 50: the pencil is placed at the bottom left.
  • L 50 10: a line up to the top centre.
  • L 90 50: a line down to the bottom right.
  • Z: back to the start, closing a triangle.

Complex paths (such as the outline of a logo) aren't written by hand: they are drawn in a vector design program (Inkscape, which is free, Figma, Illustrator…) and exported to SVG. But understanding the basic commands lets you read and tweak that code.

Accessible SVG

Inline SVG has no alt attribute. Its accessibility depends on whether it is informative or decorative, the same distinction you learnt with images.

Informative SVG (it conveys something that isn't in the nearby text):

<svg width="64" height="64" viewBox="0 0 64 64" role="img" aria-labelledby="cup-title">
  <title id="cup-title">Coffee cup, symbol of The Blue Cup</title>
  <!-- shapes -->
</svg>
  • role="img" tells assistive technologies to treat the whole thing as a single image, instead of announcing each shape separately.
  • The title element (SVG's, not the same as the title in the head) is the first child of the svg and contains its accessible name. It also appears as a tooltip on hover.
  • aria-labelledby points to the id of the title to make sure all screen readers announce it. (ARIA attributes are studied in depth in Using ARIA Roles and Attributes.)
  • If a longer explanation is needed, a desc element is added after the title.

Decorative SVG (an ornament, or an icon next to text that already says it all):

<svg width="24" height="24" viewBox="0 0 64 64" aria-hidden="true" focusable="false">
  <!-- shapes -->
</svg>
  • aria-hidden="true" hides it from screen readers, like the alt="" of a decorative image.
  • focusable="false" stops some old browsers from including it in keyboard navigation with the Tab key.

And if you use an SVG as a file with img, the rule is the usual one: a descriptive alt if it informs, alt="" if it decorates.

Example: a cup icon

Let's draw The Blue Cup's symbol on a 64×64-unit grid:

<svg width="64" height="64" viewBox="0 0 64 64" role="img" aria-labelledby="cup-title">
  <title id="cup-title">Coffee cup, symbol of The Blue Cup</title>
  <path d="M 24 4 Q 19 10 24 16" fill="none" stroke="#1f4e79" stroke-width="3" stroke-linecap="round" />
  <path d="M 34 4 Q 29 10 34 16" fill="none" stroke="#1f4e79" stroke-width="3" stroke-linecap="round" />
  <circle cx="46" cy="34" r="8" fill="none" stroke="#1f4e79" stroke-width="4" />
  <rect x="12" y="20" width="34" height="30" rx="6" fill="#1f4e79" />
  <line x1="6" y1="56" x2="58" y2="56" stroke="#1f4e79" stroke-width="4" stroke-linecap="round" />
</svg>

Shape by shape, in the order in which they are drawn:

  1. Two steam path elements: they start at the top (M 24 4), curve to the left passing close to (19, 10) and end at (24, 16). fill="none" because they are lines, not surfaces; stroke-linecap="round" rounds off the ends.
  2. The handle, a circle with no fill and a thick stroke, centred on the right-hand edge of the cup. It is drawn before the body.
  3. The body, a rect with rounded corners (rx="6"). Because it is drawn afterwards, it covers the left half of the circle, and only the outer arc of the handle remains visible.
  4. The saucer, a horizontal line with rounded ends, under the cup.

With five simple shapes you have a recognisable icon that weighs a few hundred bytes and looks perfect at 16 or at 500 pixels.

The canvas element

The canvas element creates a rectangular surface of pixels on the page, which is drawn on exclusively with JavaScript:

<canvas id="my-canvas" width="600" height="200">
  Fallback content for anyone who can't see the canvas.
</canvas>
  • width and height set the canvas resolution in pixels. If you leave them out, it measures 300×150. Don't change its size with CSS alone: you would stretch the pixels and the drawing would look blurry or distorted.
  • The content between the tags isn't displayed when the canvas works; it's there as a fallback (you will see this in the section on accessibility).
  • Without JavaScript, a canvas is an empty, transparent rectangle.

Unlike SVG, no "objects" remain in a canvas: when you draw a rectangle, some pixels are painted and the browser forgets that there was a rectangle. You can't select it, change its colour or know whether the mouse is over it; to modify anything, you have to repaint.

The 2D context: drawing with JavaScript

To draw, you first ask the canvas for its 2D drawing context: an object with all the tools (brushes, colours, text).

const canvas = document.getElementById("my-canvas");
const ctx = canvas.getContext("2d");

ctx.fillStyle = "#1f4e79";
ctx.fillRect(20, 20, 150, 80);

ctx.strokeStyle = "#c8d3e0";
ctx.lineWidth = 4;
ctx.strokeRect(200, 20, 150, 80);

ctx.font = "20px sans-serif";
ctx.fillText("The Blue Cup", 20, 150);
  1. The canvas is found by its id, as in previous lessons.
  2. getContext("2d") returns the 2D context. It is usually stored in a constant called ctx.
  3. fillStyle chooses the fill colour for whatever is drawn next.
  4. fillRect(x, y, width, height) paints a filled rectangle. The coordinates work as in SVG: origin at the top left, y downwards.
  5. strokeStyle and lineWidth choose the colour and thickness of the outline.
  6. strokeRect(...) draws only the outline of a rectangle.
  7. font defines the typeface with the same syntax as CSS, and fillText(text, x, y) writes the text.
Method or property What it does
fillStyle, strokeStyle Fill colour and outline colour
lineWidth Outline thickness
fillRect(x, y, w, h) Filled rectangle
strokeRect(x, y, w, h) Rectangle outline
clearRect(x, y, w, h) Clears an area (makes it transparent)
font Text typeface
textBaseline Vertical alignment of the text (top, middle, alphabetic…)
fillText(text, x, y) Writes text
beginPath(), moveTo(), lineTo(), arc(), fill(), stroke() Free-form paths (lines, arcs and circles)

It works like painting on paper: the order of the instructions determines what ends up on top, and changing fillStyle only affects what you paint afterwards.

Example: a bar chart explained line by line

Now for something useful: a horizontal bar chart drawn from some data. This is exactly the one you will use in the project.

<canvas id="most-ordered-chart" width="600" height="200">
  <!-- fallback content: we'll look at it in the next section -->
</canvas>

<script>
  const data = [
    { name: "Butter croissant", votes: 48 },
    { name: "Autumn seasonal coffee", votes: 35 },
    { name: "Lemon sponge cake", votes: 22 }
  ];

  const chart = document.getElementById("most-ordered-chart");
  const ctx = chart.getContext("2d");
  const pixelsPerVote = 6;

  ctx.font = "16px sans-serif";
  ctx.textBaseline = "middle";

  for (let i = 0; i < data.length; i++) {
    const y = 20 + i * 60;
    const barWidth = data[i].votes * pixelsPerVote;

    ctx.fillStyle = "#333333";
    ctx.fillText(data[i].name, 10, y + 18);

    ctx.fillStyle = "#1f4e79";
    ctx.fillRect(230, y, barWidth, 36);

    ctx.fillStyle = "#333333";
    ctx.fillText(data[i].votes + " votes", 230 + barWidth + 10, y + 18);
  }
</script>

Line by line:

  1. const data = [ … ] is a list (an array) with three items. Each item is an object with two properties, name and votes, inside braces. It's the same name-value pair syntax you saw in JSON-LD, but without compulsory quotes around the names, because this is JavaScript.
  2. chart and ctx: the canvas and its 2D context.
  3. pixelsPerVote = 6: the scale. Each vote becomes 6 pixels of bar; the maximum, 48 votes, gives 288 pixels, which fit on the canvas together with the text.
  4. ctx.font sets the typeface for all the text.
  5. ctx.textBaseline = "middle" makes the y of fillText indicate the vertical centre of the text, which makes it easier to align with the bar.
  6. for (let i = 0; i < data.length; i++) repeats the block once for each product: i is 0, then 1, then 2. data.length is the number of items in the list (3). data[i] is the product for that pass.
  7. const y = 20 + i * 60: the vertical position of each row. The first starts at 20, the second at 80, the third at 140: 60 pixels apart.
  8. barWidth: the votes multiplied by the scale.
  9. The name is written in dark grey 10 pixels from the left-hand edge, vertically centred in the row (y + 18, half of the bar's 36-pixel height).
  10. The bar is painted in the brand blue, starting at x = 230 to leave room for the names, with the calculated width and a height of 36 pixels.
  11. The number of votes is written to the right of the bar, 10 pixels after its end. The + joins the number to the text " votes".

If the data changes tomorrow, you only have to modify the data list: the chart redraws itself. That's the great advantage of drawing with code from data.

Fallback content and canvas accessibility

To a screen reader, a canvas is an empty box: it doesn't know there are bars or text inside, because there are only pixels. You have to provide the information another way:

  1. Fallback content inside the canvas: the content between the tags isn't displayed, but assistive technologies can access it. You can put text, a list or even a table with the data there.
  2. An accessible name: role="img" and aria-label with a summary, when the chart is an image with no interaction.
  3. The data in the visible text, which is best of all: if the information appears on the page (in a list or a table), everyone can access it, and the chart is a visual complement.
<figure>
  <canvas id="most-ordered-chart" width="600" height="200" role="img"
          aria-label="Bar chart: butter croissant 48 votes, autumn seasonal coffee 35 votes, lemon sponge cake 22 votes">
    <p>Butter croissant: 48 votes. Autumn seasonal coffee: 35 votes. Lemon sponge cake: 22 votes.</p>
  </canvas>
  <figcaption>Customer votes in October 2026.</figcaption>
</figure>
  • role="img" with aria-label gives the canvas a name that summarises the data.
  • The paragraph inside is the fallback content, useful if the canvas can't be displayed.
  • figure and figcaption give the chart context, as you did with images, videos and tables.

SVG or canvas: when to use each one

Criterion SVG canvas
Type of graphic Vector: shapes that are kept as elements Bitmap: painted pixels
Created with Markup (by hand, or exported from an editor) and optionally JavaScript JavaScript only
Sharpness when enlarged Perfect Pixelates if stretched
Each shape is an element Yes: CSS, mouse events and ARIA can be applied to it No: everything has to be handled by hand
Accessibility Good: title, real text, role Requires fallback content
Performance with a huge number of elements (thousands) Gets worse: each shape is a DOM node Excellent
Works without JavaScript Yes No
Ideal for Logos, icons, illustrations, simple maps, interactive charts with few elements Games, complex animations, photo editing, visualisations with thousands of points, charts generated on the fly

Rule of thumb: start with SVG for icons, logos and simple charts; turn to canvas when you need to draw lots of elements, animate at high speed or manipulate pixels. For our three-bar chart, SVG would have worked too (and would be more accessible); we use canvas so that you learn how it works, since it's the basis of many charting libraries you will come across in your career.

Project: The Blue Cup's icon, divider and chart

  1. The cup icon in the footer (every page)

In the first paragraph of the footer, add the inline cup icon as an ornament next to the name:

    <footer>
      <p>
        <svg width="24" height="24" viewBox="0 0 64 64" aria-hidden="true" focusable="false">
          <path d="M 24 4 Q 19 10 24 16" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" />
          <path d="M 34 4 Q 29 10 34 16" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" />
          <circle cx="46" cy="34" r="8" fill="none" stroke="currentColor" stroke-width="4" />
          <rect x="12" y="20" width="34" height="30" rx="6" fill="currentColor" />
          <line x1="6" y1="56" x2="58" y2="56" stroke="currentColor" stroke-width="4" stroke-linecap="round" />
        </svg>
        <strong>The Blue Cup</strong>, café and bakery
      </p>
      <!-- address, opening hours, privacy and copyright, unchanged -->
    </footer>
  • It is decorative: the name "The Blue Cup" is right next to it. That's why it has aria-hidden="true" and no title.
  • It uses currentColor instead of a fixed blue: it will take on the colour of the footer text, which you will define with CSS in module 8.
  • It is displayed at 24×24 pixels, even though it is drawn on a 64×64 grid, thanks to the viewBox.
  • The logo in the header is still img/logo.png: it's the brand image already used by Open Graph and the structured data.

  1. The SVG favicon

In Metadata you declared img/favicon.svg. Create it now with the same drawing, in the brand blue:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
  <path d="M 24 4 Q 19 10 24 16" fill="none" stroke="#1f4e79" stroke-width="3" stroke-linecap="round" />
  <path d="M 34 4 Q 29 10 34 16" fill="none" stroke="#1f4e79" stroke-width="3" stroke-linecap="round" />
  <circle cx="46" cy="34" r="8" fill="none" stroke="#1f4e79" stroke-width="4" />
  <rect x="12" y="20" width="34" height="30" rx="6" fill="#1f4e79" />
  <line x1="6" y1="56" x2="58" y2="56" stroke="#1f4e79" stroke-width="4" stroke-linecap="round" />
</svg>
  • As it's a standalone file, it has xmlns.
  • It has no width or height: the browser scales it to the size of the tab icon.
  • It uses the fixed colour #1f4e79, because in an external file currentColor has no text to take the colour from.

Open any page with Live Server: the cup will appear in the tab.

  1. The gallery divider

Since Adding Images, gallery.html has used <img src="img/bean-divider.svg" alt="">, but the file has never been created. Create it with three coffee beans on a thin line:

<svg xmlns="http://www.w3.org/2000/svg" width="300" height="24" viewBox="0 0 300 24">
  <line x1="0" y1="12" x2="300" y2="12" stroke="#c8d3e0" stroke-width="1" />
  <ellipse cx="130" cy="12" rx="6" ry="8" fill="#1f4e79" />
  <ellipse cx="150" cy="12" rx="6" ry="8" fill="#1f4e79" />
  <ellipse cx="170" cy="12" rx="6" ry="8" fill="#1f4e79" />
  <path d="M 130 5 Q 127 12 130 19 M 150 5 Q 147 12 150 19 M 170 5 Q 167 12 170 19" fill="none" stroke="#ffffff" stroke-width="1.5" />
</svg>
  • The line runs across the whole width, in the colour of the menu's borders.
  • Three ellipse elements form the beans, in the brand blue.
  • A single path with three strokes (each M starts a new one) draws the curved groove of each bean, in white.
  • As it's a file used with img, it stays in the cache and its alt="" marks it as decorative. It's the right choice for an ornament that might be repeated on several pages.

  1. The "Most ordered" chart in news.html

In article#most-ordered, add the votes to the list (so that the data is in the visible text) and, after it, the chart:

    <article id="most-ordered">
      <header>
        <h2>Most ordered in October</h2>
        <p>Published on <time datetime="2026-11-03">3 November 2026</time></p>
      </header>
      <p>These were your three favourites this month, from least to most voted:</p>
      <ol reversed>
        <li>Lemon sponge cake (22 votes)</li>
        <li>Autumn seasonal coffee (35 votes)</li>
        <li>Butter croissant (48 votes)</li>
      </ol>
      <figure>
        <canvas id="most-ordered-chart" width="600" height="200" role="img"
                aria-label="Bar chart: butter croissant 48 votes, autumn seasonal coffee 35 votes, lemon sponge cake 22 votes">
          <p>Butter croissant: 48 votes. Autumn seasonal coffee: 35 votes. Lemon sponge cake: 22 votes.</p>
        </canvas>
        <figcaption>Customer votes in October 2026.</figcaption>
      </figure>
    </article>

And at the end of the main element of news.html, the bar chart script from the section "Example: a bar chart explained line by line", unchanged.

  • The list is still the main content; the chart complements it visually.
  • The chart orders the products from most to least voted (the usual approach in a bar chart), while the list keeps its countdown order.
  • If in the future you publish "Most ordered in November", you will only have to change the text and the data list in the script.

  1. Checking

  1. Open news.html: the chart should show three blue bars with their names and votes.
  2. Open the DevTools console: there shouldn't be any errors. If the canvas appears empty, the JavaScript error will be there.
  3. Enlarge the page with the browser zoom (Ctrl and +): the footer icon stays sharp; the text in the canvas chart becomes slightly blurry. You have just seen the difference between vector and bitmap live.

Common Mistakes and Tips

  • Forgetting the viewBox: without it, the drawing doesn't scale and may be cropped.
  • Getting the direction of the y-axis wrong: in SVG and in canvas, y increases downwards.
  • Drawing a line without a stroke: nothing is visible.
  • Forgetting fill="none" on curved lines drawn with path: the browser fills the inside of the curve with black.
  • Informative inline SVG without role="img" or title: the screen reader doesn't know what it is. If it's decorative, aria-hidden="true".
  • Forgetting xmlns in an .svg file: the browser may not display it as an image.
  • Changing the size of a canvas with CSS alone: the drawing gets distorted or blurry. Use width and height.
  • Leaving a canvas without a fallback: to a screen reader, it's an empty box.
  • Writing the script before the canvas: if the script runs when the canvas doesn't exist yet, getElementById returns null and an error appears. Put it at the end of the main element, as in the project.
  • Using canvas for a logo or an icon: SVG is sharper, lighter and accessible.
  • Tip: SVGs exported from design editors often come with a lot of unnecessary code (metadata, comments, extremely long decimals). There are SVG optimisers that clean it up without changing the drawing.

Exercises

Exercise 1. Use inline SVG to draw an "open" sign: a blue rectangle with rounded corners, 160×50 units, with the text "OPEN" in white and a small green circle to the left of the text. It is informative: make it accessible.

Exercise 2. Modify the bar chart script so that the product names are written in the brand blue and the bars in #c8d3e0, and to add a fourth product: "Sourdough bread", with 30 votes. What other value do you have to change so that the fourth product fits?

Exercise 3. Decide whether you would use SVG or canvas in each case and briefly justify your choice:

  1. A simple floor plan of the café with the tables, where each table can be clicked to book it.
  2. A game in which customers "catch" coffee beans falling at full speed.
  3. The social media icons in the footer.
  4. An editor that lets customers crop a photo and apply filters to it before uploading it.

Solutions

Solution 1.

<svg width="160" height="50" viewBox="0 0 160 50" role="img" aria-labelledby="open-title">
  <title id="open-title">Open</title>
  <rect x="0" y="0" width="160" height="50" rx="10" fill="#1f4e79" />
  <circle cx="25" cy="25" r="7" fill="#3cb371" />
  <text x="42" y="33" font-family="sans-serif" font-size="22" fill="#ffffff">OPEN</text>
</svg>
  • The rectangle fills the whole viewBox and is drawn first, as the background.
  • The green circle and the text are drawn on top.
  • The y of the text is its baseline: 33 leaves it roughly centred in a height of 50.
  • As it's informative, it has role="img" and a title linked with aria-labelledby. Although SVG text is already readable, role="img" makes it be announced as a single image with a clear name.

Solution 2.

  const data = [
    { name: "Butter croissant", votes: 48 },
    { name: "Autumn seasonal coffee", votes: 35 },
    { name: "Sourdough bread", votes: 30 },
    { name: "Lemon sponge cake", votes: 22 }
  ];
  // …
    ctx.fillStyle = "#1f4e79";
    ctx.fillText(data[i].name, 10, y + 18);

    ctx.fillStyle = "#c8d3e0";
    ctx.fillRect(230, y, barWidth, 36);
  • The new product is added to the list in its position by number of votes (don't forget the comma between objects, and make sure there's no comma after the last one).
  • The colours are changed in the fillStyle lines that come before the name and the bar.
  • You have to change the height of the canvas: with four rows, the last one starts at y = 20 + 3 × 60 = 200 and ends at 236. The canvas height must be increased to, for example, 260. Remember to update the aria-label and the fallback content with the new data too.

Solution 3.

  1. SVG: few shapes, and each table is an element that can have click events, a title and accessibility attributes associated with it.
  2. canvas: lots of objects moving at high speed; everything is redrawn on every frame.
  3. SVG: icons that are sharp at any size, lightweight and can be coloured with currentColor.
  4. canvas: photo manipulation works pixel by pixel, which is exactly what the canvas offers.

Conclusion

With this lesson you complete the Advanced HTML Techniques module. You have learnt that SVG describes vector graphics with tags (svg with its viewBox, rect, circle, line, path, text) that look sharp at any size and are made accessible with role="img" and title, or hidden with aria-hidden when they are decorative; and that canvas is a pixel surface on which JavaScript draws with its 2D context, ideal for charts generated from data, always with fallback content. Over the course of the module, The Blue Cup has gained quantities and phone numbers that don't split thanks to &nbsp;; reviews with curly quotes; a map on the contact page and a video interview in the news, through secure, privacy-friendly iframe elements; a menu with data-* attributes and a "gluten-free" filter; a complete head with a description, canonical URL, icons and Open Graph; structured data for the business and the bread-making workshop; an SVG cup icon in the footer and as the favicon; and a "Most ordered" chart. The site's HTML is practically complete, but if you open it in the browser you will see that it still has the default look: black text on a white background, underlined blue links, the menu as a plain list, and the only styles, those of the menu table, placed in a provisional <style> in the head of menu.html. The time has come to give The Blue Cup the design it deserves. In the next module, Integrating HTML and CSS, you will start with the lesson Linking CSS to HTML, finally create the css/styles.css file and connect it to every page.

HTML Course

Module 1: Introduction to HTML

Module 2: Text Formatting in HTML

Module 3: Links and Media in HTML

Module 4: Tables in HTML

Module 5: Forms in HTML

Module 6: HTML5 Semantic Elements

Module 7: Advanced HTML Techniques

Module 8: Integrating HTML and CSS

Module 9: Responsive Web Design

Module 10: Best Practices and Accessibility

Module 11: Project: Building a Complete Website

© Copyright 2026. All rights reserved