How to Embed SVG in HTML: Methods and Best Practices

In this guide, I’ll walk you through the most common ways to embed SVG graphics in HTML and explain the key considerations for each method.

What Is SVG?

SVG (Scalable Vector Graphics) is an XML-based markup language for describing two-dimensional vector graphics. It is an open web standard for images that remain sharp at any size without becoming pixelated.

SVG was designed to work with other web standards, including CSS, the DOM, and JavaScript. In essence, SVG is to graphics what HTML is to text.

The Main Ways to Add SVG

When building HTML pages, you can add SVG images in two main ways:

  1. As a link to a separate SVG file.
  2. By copying and pasting the contents of the SVG file directly into the HTML or using them as a CSS property value.

Linking to an SVG File

This method keeps your HTML clean and lets you take advantage of browser caching.

Using the HTML <img> tag:

<img src="logo.svg" alt="Logo Img">

Using the CSS background-image property:

.logo {
  width: 50px;
  height: 50px;

  background-image: url("logo.svg");
  background-repeat: no-repeat;
  background-size: contain;
}

Embedding SVG Markup Directly in HTML

This method gives you the most flexibility because you can style SVG elements (for example, change the color of paths) directly with your project’s CSS.

For this example, I’ll use a free Google icon.

If you open this file in any text editor, you’ll see the following code:

<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M480-40q-91 0-171.5-34.5t-140-94q-59.5-59.5-94-140T40-480q0-92 34.5-172t94-139.5q59.5-59.5 140-94T480-920q92 0 172 34.5t139.5 94Q851-732 885.5-652T920-480q0 91-34.5 171.5t-94 140Q732-109 652-74.5T480-40Zm0-440Zm-25 311q10 10 23 10t23-10l288-288q10-10 10-24t-10-24L501-793q-10-10-23-10t-23 10L167-505q-10 10-10 24t10 24l288 288ZM319-361v-160q0-18 11-29t29-11h166l-42-44 56-56 140 140-140 140-56-56 42-44H399v120h-80Zm161 241q151 0 255.5-104.5T840-480q0-151-104.5-255.5T480-840q-151 0-255.5 104.5T120-480q0 151 104.5 255.5T480-120Z"/></svg>

Inline SVG (directly in HTML)

You can place this code directly in your page’s HTML:

<div class="logo">
  <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M480-40q-91 0-171.5-34.5t-140-94q-59.5-59.5-94-140T40-480q0-92 34.5-172t94-139.5q59.5-59.5 140-94T480-920q92 0 172 34.5t139.5 94Q851-732 885.5-652T920-480q0 91-34.5 171.5t-94 140Q732-109 652-74.5T480-40Zm0-440Zm-25 311q10 10 23 10t23-10l288-288q10-10 10-24t-10-24L501-793q-10-10-23-10t-23 10L167-505q-10 10-10 24t10 24l288 288ZM319-361v-160q0-18 11-29t29-11h166l-42-44 56-56 140 140-140 140-56-56 42-44H399v120h-80Zm161 241q151 0 255.5-104.5T840-480q0-151-104.5-255.5T480-840q-151 0-255.5 104.5T120-480q0 151 104.5 255.5T480-120Z"/></svg>
</div>

SVG as a CSS Property Value (Data URI)

You can also use SVG markup in your stylesheet. This is useful for styling pseudo-elements such as ::before and ::after.

/* SVG with the background-image CSS property */

background-image: url("data:image/svg+xml;charset=UTF-8,<svg xmlns='http://www.w3.org/2000/svg' height='24' viewBox='0 -960 960 960' width='24'><path d='M480-40q-91 0-171.5-34.5t-140-94q-59.5-59.5-94-140T40-480q0-92 34.5-172t94-139.5q59.5-59.5 140-94T480-920q92 0 172 34.5t139.5 94Q851-732 885.5-652T920-480q0 91-34.5 171.5t-94 140Q732-109 652-74.5T480-40Zm0-440Zm-25 311q10 10 23 10t23-10l288-288q10-10 10-24t-10-24L501-793q-10-10-23-10t-23 10L167-505q-10 10-10 24t10 24l288 288ZM319-361v-160q0-18 11-29t29-11h166l-42-44 56-56 140 140-140 140-56-56 42-44H399v120h-80Zm161 241q151 0 255.5-104.5T840-480q0-151-104.5-255.5T480-840q-151 0-255.5 104.5T120-480q0 151 104.5 255.5T480-120Z'/></svg>");

/* SVG with the content CSS property */

content: url("data:image/svg+xml;charset=UTF-8,<svg xmlns='http://www.w3.org/2000/svg' height='24' viewBox='0 -960 960 960' width='24'><path d='M480-40q-91 0-171.5-34.5t-140-94q-59.5-59.5-94-140T40-480q0-92 34.5-172t94-139.5q59.5-59.5 140-94T480-920q92 0 172 34.5t139.5 94Q851-732 885.5-652T920-480q0 91-34.5 171.5t-94 140Q732-109 652-74.5T480-40Zm0-440Zm-25 311q10 10 23 10t23-10l288-288q10-10 10-24t-10-24L501-793q-10-10-23-10t-23 10L167-505q-10 10-10 24t10 24l288 288ZM319-361v-160q0-18 11-29t29-11h166l-42-44 56-56 140 140-140 140-56-56 42-44H399v120h-80Zm161 241q151 0 255.5-104.5T840-480q0-151-104.5-255.5T480-840q-151 0-255.5 104.5T120-480q0 151 104.5 255.5T480-120Z'/></svg>");

Important Technical Considerations When Using SVG in CSS

When embedding SVG through CSS, there are a few critical details to get right; otherwise, the image may not display.

Let’s take a closer look, because there are a few nuances. If you open an SVG file in an editor, copy its contents, and paste them as the value of the background-image or content property, you’ll need to make a few changes to the code.

  • Quotation marks. Do not use double quotes inside the url("...") value. Replace the SVG attribute quotes (for example, width="24") with single quotes (width='24').
  • URI syntax. The contents of url("...") must begin with the prefix data:image/svg+xml;charset=UTF-8,.
  • Required attributes. For the browser to interpret the image correctly, always specify image/svg+xml and xmlns='http://www.w3.org/2000/svg'.
  • Units of measurement. It’s best to specify the height and width attributes inside the <svg> tag as numbers without units. For example, height='24' and width='24'.
  • Working with color (#). The hash symbol (#) is a special character in URLs, so you can’t use it in the fill value as-is. Use %23 instead, or specify the color by name (black, red, purple).

Comparison and Recommendations

MethodAdvantagesDisadvantages
<img> tagSimple to use; browser caching.Cannot change the color with CSS.
Inline SVGFull control over styling and animation.Adds a large amount of markup to the HTML.
CSS backgroundClean HTML; convenient for decorative icons.Colors are difficult to change on the fly.

Tip: If you need an icon to change color on hover (:hover), use inline SVG. If the image is purely decorative and doesn’t need to be interactive, use a separate file.

Mykhailo Petrov
Mykhailo Petrov

Full-cycle WordPress website development, improvements, new features, bug fixes, maintenance, updates and migration to another domain or hosting.