Development 6 min read

How to Add SVG to Your HTML Website: 4 Methods Compared

Published 2026-01-22 Updated 2026-02-20

You have a beautiful SVG file and you want to put it on your website. Simple enough, right? It turns out there are several ways to do it, and each one comes with its own set of trade-offs. Some give you full styling control, others are dead simple but more limited. Here is a practical comparison so you can pick the right approach for your situation.

Method 1: The <img> Tag

This is the simplest approach and it works exactly like embedding any other image:

<img src="icon.svg" alt="My icon" width="64" height="64">

Pros: Dead simple. The browser caches the file just like any other image. Lazy loading works. It is the least amount of code.

Cons: You cannot style the SVG with CSS. Want to change the fill color on hover? Tough luck. The SVG is treated as an opaque image, so you lose all the interactive and styling capabilities that make SVG special.

Best for: Decorative images where you do not need to change colors or animate parts. Think hero illustrations, content images, or simple icons where you do not need interactivity.

Method 2: Inline SVG

Paste the SVG code directly into your HTML:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
  <circle cx="32" cy="32" r="30" fill="#10b981"/>
</svg>

Pros: Full CSS control. You can target individual paths and shapes with selectors, change colors on hover, add transitions, and animate elements. No extra HTTP request since the SVG is part of the HTML document.

Cons: Your HTML gets longer and harder to read, especially for complex SVGs. The SVG content is not cached separately from the page, so if you use the same icon in multiple places, the code is duplicated.

Best for: Icons and graphics that need CSS styling, hover effects, or animations. This is the most popular method in modern web development for interactive SVGs.

Method 3: CSS Background Image

Use the SVG as a background in your stylesheet:

.icon {
  width: 64px;
  height: 64px;
  background-image: url('icon.svg');
  background-size: contain;
}

Pros: Keeps your HTML clean. Good for decorative elements that are part of the visual design rather than the content. The browser can cache the SVG file.

Cons: No CSS control over the SVG internals, same as the img tag. Screen readers cannot see background images, so this is a poor choice for meaningful graphics. Sizing can be fiddly.

Best for: Purely decorative patterns, textures, or background illustrations. Not for icons that convey meaning.

Method 4: The <object> Tag

<object type="image/svg+xml" data="icon.svg">
  Fallback text
</object>

Pros: The SVG runs in its own document context, so stylesheets inside the SVG file will work. You can interact with the SVG DOM through JavaScript. The file is cached.

Cons: External CSS from your page does not reach inside the object. It adds a layer of complexity. The fallback behavior is not as clean as a simple img tag.

Best for: Complex, self-contained SVG graphics that include their own styling and interactivity. Rare in day-to-day web development.

So Which One Should You Use?

For most web projects, the decision comes down to two options:

  • Use an <img> tag when you just need to display the SVG without styling its internals.
  • Use inline SVG when you need CSS control, hover effects, or animations.

The other two methods have their place, but they cover edge cases rather than everyday needs. When in doubt, start with inline SVG. It gives you the most flexibility, and you can always switch to an img tag later if you decide you do not need the extra control.

svg html web development css tutorial

Need SVG files for your project?

Browse our library of free, CC0-licensed SVG files or convert your own images to SVG in seconds.