Reading history

SVG in HTML: Shapes, Icons and Charts

6 min read 0 views

Learn inline SVG: viewBox and coordinates, rect, circle, line, polygon and text, path icons with currentColor, CSS styling and accessible charts.

SVG (Scalable Vector Graphics) describes pictures as shapes instead of pixels: “a circle here, a line there”. Because the browser draws the shapes, an SVG stays perfectly sharp at any size, from a 16-pixel icon to a full-screen banner, and the file is usually tiny. SVG code can be written straight into an HTML page, where CSS can colour and animate it. This lesson covers the basic shapes, the viewBox, icons that follow the text colour, and a simple bar chart.

In this lesson you will learn

  • The SVG coordinate system and the viewBox attribute
  • Basic shapes: rect, circle, ellipse, line, polyline, polygon and text
  • Paths and icons that use currentColor
  • Styling SVG shapes with CSS
  • Making SVGs accessible

You should know HTML elements and attributes and CSS selectors.

Basic Shapes

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>SVG shapes</title>
  <style>
    body { font-family: sans-serif; margin: 12px; }
    svg { border: 1px dashed #b0bec5; }
  </style>
</head>
<body>
  <!-- viewBox sets the drawing's own coordinate system: 0,0 is the top-left corner -->
  <svg width="420" height="160" viewBox="0 0 420 160">
    <rect x="10" y="20" width="100" height="60" rx="8" fill="#42a5f5" />
    <circle cx="170" cy="50" r="30" fill="#ef5350" stroke="#b71c1c" stroke-width="4" />
    <ellipse cx="260" cy="50" rx="45" ry="25" fill="#ffca28" />
    <line x1="320" y1="20" x2="410" y2="80" stroke="#2e7d32" stroke-width="5" stroke-linecap="round" />
    <polyline points="10,140 60,110 110,130 160,100 210,120" fill="none" stroke="#6a1b9a" stroke-width="3" />
    <polygon points="270,95 300,150 240,150" fill="#26a69a" />
    <text x="320" y="135" font-size="18" fill="#37474f">SVG text</text>
  </svg>
</body>
</html>

Result

  • width and height set the size on the page. viewBox="0 0 420 160" sets the drawing’s own coordinates: x runs from 0 to 420 and y from 0 to 160. If you change the width, the drawing scales to match.
  • The origin (0, 0) is the top-left corner, and y grows downwards, unlike graphs in maths class.
  • rect uses x, y, width and height; rx rounds its corners. circle uses a centre (cx, cy) and radius r, and ellipse has two radii.
  • line joins two points. polyline joins a list of points into an open line, and polygon closes the shape.
  • fill paints the inside and stroke the outline. For open lines, set fill="none" so the area under the line is not filled.
  • text draws real, selectable text at the given position.

Icons, Paths and CSS

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>SVG icons and a chart</title>
  <style>
    body { font-family: sans-serif; margin: 12px; }
    .btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border: 0;
           border-radius: 6px; font-size: 15px; background: #1565c0; color: white; margin-right: 6px; }
    .btn.alt { background: #e3f2fd; color: #0d47a1; }
    .icon { width: 18px; height: 18px; }
    .chart rect { fill: #66bb6a; transition: fill .2s; }
    .chart rect:hover { fill: #2e7d32; }      /* SVG shapes can be styled with CSS */
    .chart text { font-size: 12px; fill: #37474f; }
  </style>
</head>
<body>
  <!-- currentColor makes the icon use the button's text colour -->
  <button class="btn">
    <svg class="icon" viewBox="0 0 24 24" aria-hidden="true">
      <path d="M5 12l5 5L20 7" fill="none" stroke="currentColor" stroke-width="3" />
    </svg>
    Save
  </button>
  <button class="btn alt">
    <svg class="icon" viewBox="0 0 24 24" aria-hidden="true">
      <path d="M5 12l5 5L20 7" fill="none" stroke="currentColor" stroke-width="3" />
    </svg>
    Save draft
  </button>

  <svg class="chart" width="300" height="140" viewBox="0 0 300 140" role="img" aria-label="Marks: Rina 92, Arif 70, Tania 85">
    <rect x="20"  y="20" width="60" height="92" />
    <rect x="110" y="42" width="60" height="70" />
    <rect x="200" y="27" width="60" height="85" />
    <text x="50"  y="130" text-anchor="middle">Rina</text>
    <text x="140" y="130" text-anchor="middle">Arif</text>
    <text x="230" y="130" text-anchor="middle">Tania</text>
  </svg>
</body>
</html>

Result

  • <path d="..."> can draw any shape. M5 12 moves the pen to (5, 12), and l5 5 draws a line 5 units right and 5 down from there. Icon libraries and design tools generate these paths for you.
  • stroke="currentColor" makes the icon use the CSS color of its parent. The same icon is white in the blue button and dark blue in the light button, with no change to the SVG.
  • Decorative icons next to a text label get aria-hidden="true" so that screen readers do not announce them.
  • The chart is made of three rect bars. CSS rules such as .chart rect:hover work on SVG shapes, and fill can be set from CSS and transitioned.
  • A meaningful graphic gets role="img" and an aria-label that describes it, so screen-reader users get the same information.

Ways to Put SVG on a Page

MethodStyle with page CSS?Best for
Inline <svg> in the HTMLYesIcons, charts, anything you want to colour or animate
<img src="logo.svg" alt="...">NoLogos and illustrations that never change colour
CSS background-image: url(shape.svg)NoDecorative patterns

Common Mistakes

  1. Leaving out viewBox, so the drawing does not scale when you change its size.
  2. Forgetting fill="none" on lines and outline icons, which fills them with black.
  3. Pasting huge SVGs from design tools without removing editor data. Tools such as SVGO can shrink them.
  4. Mixing up width and viewBox: width is the displayed size, viewBox is the drawing’s coordinate range.

Practice Exercises

Exercise 1. Draw a 100 × 100 SVG with a green circle filling most of it and the letter “A” in white in the middle.

Show answer
<svg width="100" height="100" viewBox="0 0 100 100">
  <circle cx="50" cy="50" r="45" fill="#2e7d32" />
  <text x="50" y="62" font-size="36" fill="white" text-anchor="middle">A</text>
</svg>

text-anchor="middle" centers the text horizontally on x = 50.

Exercise 2. Make the check icon from the example red inside a .error message.

Show answer
.error { color: #c62828; }

Because the icon uses currentColor, changing the text colour is enough.

Frequently Asked Questions

When should I use SVG instead of PNG or JPEG?

Use SVG for logos, icons, diagrams and charts: anything made of shapes and flat colours. Use JPEG, PNG or WebP for photos, which would be enormous as vector shapes.

Can SVG be animated?

Yes. Inline SVG shapes accept CSS transitions and animations, such as changing fill on hover or rotating an icon with transform. See CSS transitions and animations.

Summary

  • SVG draws shapes that stay sharp at any size.
  • viewBox sets the coordinate system; y grows downwards.
  • fill paints inside, stroke paints outlines; currentColor follows the text colour.
  • Inline SVG can be styled with CSS; label meaningful graphics for screen readers.

Next: Styling HTML Forms with CSS.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *