Skip to main content

HTML Tags

HTML <svg> Tag

Written by Updated HTML <svg> Tag exercises

<svg> puts vector graphics directly into HTML. Unlike a PNG or JPEG, an SVG is made of shapes described in code, so it stays sharp at any size, can be styled with CSS, and can be changed with JavaScript. It is how nearly every icon on the modern web is drawn.

Try it

An icon and a chart, both inline. Run it, then change color: #0ea5e9 to another colour: the icon follows, because its fill is currentColor.

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Inline SVG</title>
  <style>
    .btn { display: inline-flex; align-items: center; gap: 6px; color: #0ea5e9; font: 16px system-ui; padding: 8px 12px; }
    body { padding: 20px; }
  </style>
</head>
<body>
<button class="btn">
  <svg width="20" height="20" viewBox="0 0 24 24" aria-hidden="true">
    <path d="M12 2 15 8.5 22 9.3 17 14l1.2 7L12 17.8 5.8 21 7 14 2 9.3l7-.8z" fill="currentColor"/>
  </svg>
  Favourite
</button>

<svg viewBox="0 0 200 80" width="300" role="img" aria-labelledby="chart-title">
  <title id="chart-title">Lessons completed per week: 3, 5, 8, 6</title>
  <rect x="10"  y="50" width="30" height="30" fill="#94a3b8"/>
  <rect x="55"  y="30" width="30" height="50" fill="#94a3b8"/>
  <rect x="100" y="0"  width="30" height="80" fill="#0ea5e9"/>
  <rect x="145" y="20" width="30" height="60" fill="#94a3b8"/>
</svg>
</body>
</html>

Inline or <img>?

You can also load an .svg file with <img>, as a CSS background-image, or with <object>. The choice comes down to one question: do you need to reach inside it?

Inline <svg><img src="file.svg">
Style parts with CSSYesNo: it is a sealed image
Change colour on hover / themeYes (currentColor)No, unless you swap files
Animate or script individual shapesYesNo
Browser caches itNo: repeated in every page's HTMLYes
Best forIcons, small illustrations, chartsLogos, large artwork, anything reused across pages

viewBox: the attribute that makes it scale

viewBox="0 0 24 24" declares the coordinate system the shapes were drawn in: a 24×24 box starting at the origin. width and height (or CSS) say how big to draw it on the page. The browser maps one to the other, so the same 24-unit path renders crisply at 16px or 400px. Without a viewBox, the drawing is stuck at its original pixel size and resizing the <svg> just crops it.

A realistic example: one icon set, used many times

Inlining the same 200-byte path fifty times bloats the page. Define each icon once in a hidden <symbol>, then reference it with <use> wherever it appears. Each <use> can still be coloured independently.

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>SVG sprite</title>
  <style>
    .checks { list-style: none; padding: 0; font: 16px system-ui; }
    .checks li { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
    .checks svg { width: 18px; height: 18px; }
    .ok { color: #15803d; }
    .no { color: #b91c1c; }
  </style>
</head>
<body>
<svg style="display:none" aria-hidden="true">
  <symbol id="i-check" viewBox="0 0 24 24">
    <path d="M20 6 9 17l-5-5" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round"/>
  </symbol>
  <symbol id="i-x" viewBox="0 0 24 24">
    <path d="M6 6l12 12M18 6 6 18" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round"/>
  </symbol>
</svg>

<ul class="checks">
  <li class="ok"><svg><use href="#i-check"/></svg> Runs in the browser</li>
  <li class="ok"><svg><use href="#i-check"/></svg> Checked automatically</li>
  <li class="no"><svg><use href="#i-x"/></svg> Requires a download</li>
</ul>
</body>
</html>

Accessibility

An SVG is either decoration or content. If it is decoration, an icon next to a word that already says what it is, hide it: aria-hidden="true". If it carries meaning on its own (an icon-only button, a chart) give it role="img" and a <title> as its first child, linked with aria-labelledby as in the first example. Never leave a meaningful SVG with no accessible name; screen readers will either skip it or read out the path data.

Common mistakes

1. No viewBox

The icon shows at the wrong size, or resizing it crops instead of scaling. Every <svg> you write by hand should have a viewBox; every export from Figma or Illustrator already does: do not delete it when you clean the file up.

2. Hard-coded fill colours

fill="#000000" straight from the design tool means the icon stays black on your dark theme and on your blue button. Replace it with fill="currentColor" and the icon takes the text colour of wherever you put it. Then .btn:hover { color: … } recolours the icon for free.

3. Pasting editor exports unedited

Exported SVG carries editor metadata, unused <defs>, ids like Layer_1 that collide when two files are inlined, and inline style attributes. Run it through an optimiser such as SVGO first; a 20KB export is routinely 1KB afterwards.

4. Sizing with attributes and CSS that disagree

If <svg> has width="24" and your CSS says svg { width: 100% }, CSS wins, but only if the selector matches. It is simplest to leave the attributes off and size with CSS, keeping just the viewBox.

5. Treating it as a raster

Photos do not belong in SVG. It can embed a bitmap with <image>, but the file is then bigger than the bitmap alone. SVG is for shapes, lines and text; use <picture> for photographs.

Browser support

Inline SVG has worked in every browser since IE9. <use> with href (rather than the old xlink:href) since 2017–2019 in all current browsers. currentColor is universal.

Related chapters

  • <img>, for SVG files you do not need to style
  • <canvas>: pixel drawing with JavaScript, the other graphics element
  • <picture>: responsive images
  • CSS filters, which also apply to inline SVG

HTML <svg> Tag exercises

Draw a circle

Draw an SVG that is 100 wide and 100 high with a circle: centre at 50, 50 and radius 40.

Result

Check your answer to see it here.