Lesson 47 of 51 · Module 7, lesson 7 of 11 · 3 min

Icons with inline SVG (optional)

Add sharp, small icons with SVG, and make them accessible or hide them when they are decoration.

What you'll learn

  • What SVG is
  • How to put a small SVG icon directly into your HTML
  • How to make an icon accessible — or hide it when it is decoration

This lesson is optional. Come back to it when you need icons.

Why this matters

Icons — a phone, a clock, a check mark — help people scan a page. SVG (Scalable Vector Graphics) draws them with code instead of pixels, so they stay sharp at any size and on any screen. Many icon sets give you SVG code to paste into your page.

An inline SVG

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Icons</title>
</head>
<body>
  <h1>Tomato soup</h1>
  <p>
    <svg width="24" height="24" viewBox="0 0 24 24" aria-hidden="true">
      <circle cx="12" cy="12" r="10" fill="none" stroke="#0474F9" stroke-width="2"/>
      <path d="M12 6 V12 L16 14" fill="none" stroke="#0474F9" stroke-width="2"/>
    </svg>
    30 minutes
  </p>
  <p>
    <svg width="24" height="24" viewBox="0 0 24 24" role="img" aria-label="Vegetarian">
      <circle cx="12" cy="12" r="10" fill="#16A34A"/>
      <path d="M7 12 L11 16 L17 8" fill="none" stroke="white" stroke-width="2"/>
    </svg>
  </p>
</body>
</html>

What you'll see: a small blue clock next to "30 minutes", and a green circle with a white check mark.

The page "Tomato soup" with a small blue clock icon next to the text "30 minutes", and below it a green circle with a white check mark.

How to read it

  • <svg> is the drawing. width and height set its size on the page.
  • viewBox="0 0 24 24" sets the drawing's own grid: 24 units wide and 24 high. Everything inside is drawn on that grid.
  • <circle> draws a circle: centre (cx, cy) and radius (r).
  • <path> draws lines. M moves the pen, L draws a line, V draws a vertical line.
  • fill is the inside colour; stroke is the outline.

You rarely write paths by hand. You copy them from an icon set, or export them from a drawing program.

Accessible icons

An icon is either decoration or information:

  • Decoration — the text next to it already says everything ("🕒 30 minutes"). Add aria-hidden="true" so that screen readers skip it.
  • Information — the icon alone carries meaning (a green check that means "vegetarian"). Add role="img" and aria-label="Vegetarian", so that a screen reader says "Vegetarian, image".

(aria-hidden, role and aria-label are ARIA attributes: extra information for assistive technology. Use them only in cases like this, where no plain HTML element does the job.)

SVG files

You can also save an SVG as a file and use it like any image:

html
<img src="images/logo.svg" alt="Home Kitchen" width="120" height="40">

This is simpler when you use the same icon on many pages. Inline SVG is useful when you want to change its colours with CSS later.

Try it yourself

  1. Select the example and choose Try it.
  2. Change fill="#16A34A" to another colour.
  3. Change width and height to 48. The icon doubles in size and stays sharp.

Common mistakes

  • No viewBox. The icon is cut off or badly scaled. Fix: keep the viewBox from the icon set.
  • An information icon with no name. Screen readers skip it, or read nothing useful. Fix: role="img" and aria-label.
  • A decorative icon that is read aloud. Fix: aria-hidden="true".

Check yourself

  1. Why does an SVG stay sharp when you make it bigger?
  2. An icon sits next to the words "30 minutes". Decoration or information?
  3. How do you give an information icon a name?

Answers

  1. It is drawn with shapes described in code, not with pixels.
  2. Decoration — hide it with aria-hidden="true".
  3. role="img" and aria-label="…" on the <svg>.

Next: your HTML is ready for styles. The next lesson connects it to CSS.

Chat with us on WhatsApp