Iconkeep › Blog

How to use SVG icons in HTML: 5 methods compared

· 7 min read

There are five common ways to use SVG icons in HTML: paste the <svg> inline, load the file with <img>, reference a symbol sprite with <use>, apply the file as a CSS mask, or set it as a background-image. For interface icons that should follow your text color, use inline SVG or a <use> sprite; use <img> or a background image only when the icon never needs to change color.

Below is how each method works, with copy-ready code, followed by a comparison table and the accessibility markup each one needs. The examples use Iconkeep icons, which are line icons on a 48px grid drawn with stroke="currentColor", but the same rules apply to any SVG.

Method 1: inline SVG

Paste the SVG code straight into the HTML. Copy SVG on any Iconkeep icon page gives you this code; here the heart is set to 24px and marked as decorative:

<button class="like" type="button">
  <svg width="24" height="24" viewBox="0 0 48 48" fill="none" stroke="currentColor"
       stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"
       aria-hidden="true" focusable="false">
    <path d="M42.21 10.39L41.23 9.41C37.44 5.62 31.23 5.62 27.44 9.41L24 12.85L20.56 9.41C16.77 5.62 10.56 5.62 6.77 9.41L5.79 10.39C2 14.18 2 20.38 5.79 24.17L9.23 27.62L24 42.38L38.77 27.62L42.21 24.17C46 20.38 46 14.18 42.21 10.39"/>
  </svg>
  Like
</button>

What you get: full control. The icon follows the button's CSS color, you can change stroke-width per icon, animate individual paths, and there's no extra request.

The cost: every copy repeats the path data, which makes the HTML larger and harder to read when the same icon appears dozens of times. And inline SVG is cached only as part of the page it sits in, as the >W3C design system notes.

Keep the width and height attributes even if your CSS sets the size. The W3C design system recommends them because they stop an SVG from expanding to fill the page if the CSS fails to load.

One warning from >Iconify's documentation: SVG can contain scripts and links to external resources, so only inline SVG from sources you trust.

Method 2: the img tag

Treat the icon like any other image:

<img src="/icons/heart.svg" width="24" height="24" alt="">

What you get: the simplest markup, a file the browser caches, and a clean HTML document.

The cost: you can't style the inside. MDN's guide to >SVG as an image lists the restrictions browsers may apply in image contexts: JavaScript is disabled, and external resources such as images and stylesheets can't be loaded. Your page's CSS, including its text color, doesn't reach the shapes inside the file either, so the icon keeps the color it was saved with. For hover states or dark mode you'd need a second file.

<img> is a good fit for icons that never change color, such as colored illustrations, or for places where you can only insert images. If you download from Iconkeep, pick the color first; the downloaded SVG and PNG keep it.

Method 3: an SVG sprite with symbol and use

A sprite stores each icon once as a <symbol>, then draws it wherever you need it with <use>. Put the sprite at the top of the <body>:

<svg width="0" height="0" style="position:absolute" aria-hidden="true">
  <symbol id="i-heart" viewBox="0 0 48 48">
    <path d="M42.21 10.39L41.23 9.41C37.44 5.62 …"/>
  </symbol>
  <symbol id="i-search" viewBox="0 0 48 48">
    <path d="…"/>
  </symbol>
</svg>

Then use the icons anywhere on the page:

<svg class="icon" width="24" height="24" fill="none" stroke="currentColor"
     stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"
     aria-hidden="true" focusable="false">
  <use href="#i-heart"></use>
</svg>

MDN describes the effect of ><use> as if the referenced nodes were deeply cloned into a non-exposed DOM and pasted in its place. That has two consequences:

  1. Styling works through inheritance, not selectors. Your stylesheet can't target the cloned <path>, but the stroke="currentColor", fill="none" and stroke-width set on the outer <svg> are inherited by it. Keep those attributes off the paths inside the <symbol>, then change the color with CSS color as usual.
  2. Use href, not xlink:href. MDN notes that xlink:href has been deprecated since SVG 2, and that href wins if both are present. You'll still see xlink:href in older tutorials.

Sprite in an external file

To cache the sprite, save the <svg> with the symbols as /icons/sprite.svg and point at the fragment:

<svg class="icon" width="24" height="24" …><use href="/icons/sprite.svg#i-heart"></use></svg>

Two rules apply here:

  • Same origin only. MDN says browsers may apply the same-origin policy to <use> and that there is currently no defined way to set a cross-origin policy for it. The >crossorigin attribute is valid only on <image>, <feImage> and <script>. Host the sprite on the same domain as your pages, not on a CDN domain.
  • No data: URLs. Loading data: URIs in a <use> href is deprecated, and Chrome >announced the removal of that support after the SVG spec dropped it. Use a same-origin file or an inline sprite instead.

Method 4: CSS mask

A mask uses the SVG file only as a shape. The color comes from the element's background-color, so it can follow currentColor while the file stays cached:

<span class="icon-mask icon-heart" aria-hidden="true"></span>
.icon-mask {
  display: inline-block;
  width: 24px; height: 24px;
  background-color: currentColor;
  -webkit-mask: var(--icon) center / contain no-repeat;
          mask: var(--icon) center / contain no-repeat;
}
.icon-heart { --icon: url(/icons/heart.svg); }

Write both the prefixed and the unprefixed line. This is the method Iconify suggests for one-color icons when you want external files, and our guide to changing SVG icon color with CSS covers it in more detail.

The cost: the icon is always one flat color, and you can't change its stroke width or animate its parts. Set the stroke width you want before you download the file.

Method 5: background-image

.search-field {
  background: url(/icons/search.svg) 12px center / 20px no-repeat;
  padding-left: 40px;
}

This is fine for purely decorative icons inside form fields or list bullets. The same image restrictions as <img> apply, the color is fixed, and a CSS background has no text alternative, so never use one for an icon that carries meaning.

Which method should you use?

Method Change color with CSS Cached separately Markup per icon Best for
Inline <svg> Yes, plus stroke width and animation No Full path data A handful of icons, animated icons
<img src> No Yes One tag Fixed-color icons, CMS and email-like contexts
Sprite with <use> Yes, via inheritance Yes, if external and same-origin One short <svg> Icon systems, icons repeated on many pages
CSS mask Yes, one color Yes One element + class CSS-driven UIs with cached one-color icons
background-image No Yes None Decorative icons in inputs and lists

A practical default: use inline SVG while a project has few icons, and move to a sprite once the same icons repeat across pages. Use <img> for icons that must stay a fixed color.

How do you make SVG icons accessible?

The markup depends on what the icon does, not on which method you choose. The >W3C design system uses these patterns:

  1. Decorative icon next to visible text (like the "Like" button above): hide it with aria-hidden="true" and focusable="false" on the <svg>. For <img>, use an empty alt="", as the >W3C decorative images tutorial recommends for an image that sits next to link text it doesn't add to.
  2. Icon-only button or link: keep the SVG hidden and give the control a name with visually hidden text:
<button type="button">
  <svg … aria-hidden="true" focusable="false"><use href="#i-search"></use></svg>
  <span class="visually-hidden">Search</span>
</button>
  1. An icon that carries meaning on its own: use role="img" on the <svg> with a <title> as its first child, or <img> with a real alt text.

Icon-only buttons also need a large enough tap area. Our icon size guidelines cover WCAG target sizes and which icon sizes to use.

Common problems

The icon is huge or fills the page. The SVG has a viewBox but no width and height. Add both attributes, or set a size in CSS.

The sprite icon shows nothing. Check that the id in href="#i-heart" matches the <symbol> exactly, that the symbol has a viewBox, and, for an external sprite, that it's served from the same origin as the page.

The sprite icon turns black or solid. The outer <svg> is missing fill="none" and stroke="currentColor", or the paths inside the symbol have their own fill that overrides the inherited value.

The line looks too thin at small sizes. Iconkeep's 1.5px stroke is drawn for 48px. At 24px it renders at about 0.75px, so raise stroke-width or download the icon with a thicker stroke.

Icons to start with

These common interface icons work well as a first sprite. Every one is free SVG and PNG with currentColor and a matching stroke; browse the full set in UI & interface.

For developer docs and code-related pages:

Technology icons · 308 free icons, SVG and PNG

Frequently asked questions

What is the best way to add SVG icons to HTML?

For most sites, inline SVG or a symbol sprite with the use element. Both let the icon take its color from CSS through currentColor. Inline SVG is the simplest; a sprite avoids repeating the same path code when an icon appears many times.

Can I change the color of an SVG in an img tag?

No. An SVG loaded through img is treated as a finished image, so page CSS can't reach its shapes. Inline the SVG, reference it with use, or apply the file as a CSS mask and set background-color instead.

Can the use element load an SVG sprite from another domain?

Not reliably. MDN notes that browsers may apply the same-origin policy to use and that there is no defined way to set a cross-origin policy for it. Host the sprite file on the same origin as the page, or put the sprite inline.

Should I use href or xlink:href in the use element?

Use href. Since SVG 2, xlink:href is deprecated in favor of href, and when both are present href wins.

Are inline SVG icons cached by the browser?

Only as part of the HTML page they sit in. To cache icons separately, keep them in an external file and load it with img, a CSS mask or a same-origin sprite referenced by use.

More from the blog

Iconkeep is free

Every static icon is free for personal and commercial work. If they save you time, you can pay what you want — $0 is a fair price too.

We only ask once. Prefer a tip? Ko-fi

Donate to Iconkeep

Payments are handled by Ko-fi. Open Ko-fi in a new tab