CHAPTER 48 · HTML

SVG graphics: describe shapes and a simple diagram

Read coordinates, shapes and a viewBox. Build original graphics that scale while keeping their meaning available in words.

Download example files (ZIP)

A drawing described by shapes

Imagine telling a friend to draw a circle at a certain place, rather than handing over a photograph made of tiny coloured squares. SVG means Scalable Vector Graphics. It can describe shapes and text using markup. Vector shapes can scale cleanly because the browser draws them from their descriptions.

Photographs are usually better supplied as image files such as JPEG or WebP. SVG is useful for icons and diagrams. An SVG can also contain pixel-based pictures, so do not assume every possible SVG stays sharp regardless of what it contains.

Two ways to place SVG on a page

You can load a separate SVG file with img and appropriate alt text, as we did with original lesson drawings. You can also write svg directly inside HTML, which is called inline SVG. The examples here use inline markup so each shape is visible in the source.

Inline SVG has its own elements and attributes. It is not a collection of ordinary divs. When writing a standalone .svg file, the SVG namespace declaration is needed; inside an HTML svg element the HTML parser handles the namespace switch. Our complete programs are HTML files, not standalone SVG files.

Coordinates: right and down

In the default coordinate system, x increases to the right and y increases downward. The top-left point is 0,0. A point 75,80 is 75 units from the left and 80 units from the top. These are drawing coordinates; scaling can map them to different display sizes.

For circle, cx and cy locate the centre and r gives the radius. Our circle uses centre 75,80 and radius 42. It fits inside the 320-by-160 drawing area. Check the centre plus and minus the radius if a shape is unexpectedly clipped.

Rectangles, outlines and fills

Rect uses x and y for its upper-left corner, with width and height for its size. Fill paints inside the shape. Stroke paints its outline, and stroke-width controls outline thickness. Rounded corners can use rx, as in the step boxes.

A line uses x1,y1 for its start and x2,y2 for its end. Our arrow adds a small polygon, whose points list the corners in coordinate pairs. A line alone does not automatically grow an arrowhead. SVG paths can describe more complex curves, but you do not need them to understand these examples.

ViewBox separates drawing coordinates from display size

ViewBox="0 0 320 160" describes a drawing rectangle starting at 0,0 with width 320 and height 160. Width and height on svg establish the intended display size. The browser maps the drawing rectangle into the available display area, normally preserving its aspect ratio.

Our CSS max-width:100% and height:auto allow the graphic to shrink with its container. The viewBox does not move each shape to a new logical coordinate; it maps the existing drawing. Attribute spelling matters: use viewBox with a capital B.

Text and reading meaning

The second graphic uses text at named coordinates. Text-anchor=middle centres the text around its x coordinate, while y locates its baseline rather than the top of its letters. Font-size sets the text size in this drawing. Keep words short enough to fit; ordinary SVG text does not automatically wrap like an HTML paragraph.

The boxes and arrow repeat a normal sentence explaining the order. Readers should not need colour or the arrow alone to understand that Save comes before Open.

Give useful graphics a name and explanation

Each example uses role=img, an internal title and desc, and aria-labelledby and aria-describedby pointing to their unique ids. Title names the image; desc explains what the drawing communicates. Support still needs testing with the tools readers use. The ordinary visible paragraph provides the meaning as well.

Decorative graphics need different handling: they can be hidden from assistive tools when they add no information. Do not hide a diagram that is the only explanation. Prefer real visible text where readers need to copy or translate details.

Keep SVG sources trustworthy

Inline SVG can contain much more than simple shapes, including features that can execute or reference other content. Treat unknown SVG as active markup rather than assuming every SVG upload is safe. These examples contain only authored shapes and text, without scripts or external references.

Save each complete HTML program and zoom the page. Then change one coordinate or fill and explain the result. Complex illustration tools can produce SVG too, but a large generated file is not automatically a clear beginner example.

Follow the idea

Read a vector drawing
  1. 1ViewBox: the drawing rectangle
  2. 2Coordinates: right and down
  3. 3Fill and stroke: inside and outline
  4. 4Text: describe the same meaning

Shapes remain document elements; provide a useful text explanation.

Complete program: svg-shapes.html

Save this as svg-shapes.html. Open it in your browser.

svg-shapes.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Describe Simple Vector Shapes</title>

    <style>
      svg {
        max-width: 100%;
        height: auto;
      }
    </style>
  </head>
  <body>
    <h1>Draw two shapes with SVG</h1>
    <main>
      <p>The drawing contains a blue circle and a pale rectangle with a dark border.</p>
      <svg
        width="320"
        height="160"
        viewBox="0 0 320 160"
        role="img"
        aria-labelledby="shapes-title"
        aria-describedby="shapes-desc"
      >
        <title id="shapes-title">Two practice shapes</title>
        <desc id="shapes-desc">
          A blue circle on the left and a pale rectangle on the right. The shapes do not represent
          different levels of importance.
        </desc>
        <circle cx="75" cy="80" r="42" fill="#2458a6"></circle>
        <rect
          x="155"
          y="38"
          width="125"
          height="84"
          fill="#e8f0ff"
          stroke="#202c3a"
          stroke-width="3"
        ></rect>
      </svg>
    </main>
  </body>
</html>
Browser previewDocument title: Describe Simple Vector Shapes

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: a blue circle beside a pale outlined rectangle. The visible sentence names both. Shrinking the page scales the graphic within its container; changing cx moves the circle horizontally.

Complete program: svg-steps.html

Save this as svg-steps.html. Open it in your browser.

svg-steps.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A Two-Step Vector Diagram</title>

    <style>
      svg {
        max-width: 100%;
        height: auto;
      }
    </style>
  </head>
  <body>
    <h1>Show the direction of a practice task</h1>
    <main>
      <p>
        First save the file, then open it in a browser. The diagram repeats that order visually.
      </p>
      <svg
        width="360"
        height="160"
        viewBox="0 0 360 160"
        role="img"
        aria-labelledby="steps-title"
        aria-describedby="steps-desc"
      >
        <title id="steps-title">Save, then open</title>
        <desc id="steps-desc">An arrow goes from a box labelled Save to a box labelled Open.</desc>
        <rect x="20" y="45" width="120" height="60" rx="8" fill="#e8f0ff" stroke="#2458a6"></rect>
        <rect x="220" y="45" width="120" height="60" rx="8" fill="#e8f0ff" stroke="#2458a6"></rect>
        <line x1="140" y1="75" x2="216" y2="75" stroke="#202c3a" stroke-width="3"></line>
        <polygon points="206,67 218,75 206,83" fill="#202c3a"></polygon>
        <text x="80" y="81" text-anchor="middle" font-size="18" fill="#202c3a">Save</text>
        <text x="280" y="81" text-anchor="middle" font-size="18" fill="#202c3a">Open</text>
      </svg>
    </main>
  </body>
</html>
Browser previewDocument title: A Two-Step Vector Diagram

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: two labelled boxes with an arrow from Save toward Open. A visible sentence and the SVG description state the same order. These are shapes in the document, not a canvas painted by JavaScript.

Problem and solution

Problem: part of a circle disappears beyond the drawing edge.

Solution: check its centre and radius against the viewBox bounds. Move the circle or adjust the drawing area intentionally. Changing only fill cannot repair clipped geometry.

Common mistakes and useful tips

  • Do not confuse a centre point with a rectangle corner.
  • Y increases downward in the default system.
  • Use the correct viewBox spelling.
  • Do not rely on colour or arrows as the only explanation.
  • Keep label ids unique.
  • Do not assume an unknown SVG contains only harmless shapes.

Practice questions

  1. What does SVG stand for?
  2. Which circle attribute controls radius?
  3. Which direction increases y by default?
  4. What do the last two viewBox numbers describe?
  5. Does SVG text wrap like an ordinary HTML paragraph?
  6. Save the shapes program and change cx from 75 to 90, then restore it. What moves?
Check your practice answers
  1. Scalable Vector Graphics.
  2. R.
  3. Downward.
  4. The drawing rectangle's width and height.
  5. Not automatically; these labels need to fit their intended area.
  6. The circle moves 15 drawing units to the right. Its radius and the rectangle remain unchanged.

Interview questions

How does inline SVG differ from img?

Inline SVG places the drawing elements in the document. Img loads a separate image resource and uses alt text to describe its purpose.

Why use viewBox?

It defines the drawing coordinate rectangle that is mapped into the available display area, helping the graphic scale predictably.

Why keep a visible diagram explanation?

Not every reader can see or interpret the drawing. Text also supports copying, translation and an unambiguous description of its meaning.

Is every SVG inherently safe or made entirely of vector shapes?

No. SVG can contain more than basic vector shapes, including pixel-based pictures and active or external content. Use trusted authored sources and appropriate handling.

Quick revision

  • Read coordinates and dimensions separately.
  • Use fill and stroke intentionally.
  • Give the drawing a clear text meaning.
  • Test scaling and keep sources trustworthy.