How to Use SVG Icons in HTML
Learn the three main methods for implementing SVG icons in your web projects, including inline SVG, image tags, and CSS backgrounds. Understand the pros and cons of each approach.
Method 1: Inline SVG (Recommended)
The most flexible method is to include the SVG code directly in your HTML. This allows for full control over styling and animations.
<svg class="w-6 h-6 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
</svg>
Method 2: As an Image
Simple to implement but with limited styling options. Good for static icons that don't need customization.
<img src="icon.svg" alt="Icon description" class="w-6 h-6">
Method 3: CSS Background
Useful for decorative elements where semantic meaning isn't important.
.icon {
background-image: url('icon.svg');
background-size: contain;
width: 24px;
height: 24px;
}