Course contentsShow
← Web Development
Lesson 2037 of 2,98237. Image and Asset OptimizationFree lesson

SVG Basics and Inline vs External

Learn SVG syntax fundamentals and when to inline SVG vs use external files for icons and graphics.

SVG Basics and Inline vs External

You'll learn when to embed SVG code directly in HTML versus referencing external SVG files.

What is SVG?

SVG (Scalable Vector Graphics) is an XML-based format for describing graphics using mathematical shapes rather than pixels. Think of it like giving directions ("draw a circle at point X with radius Y") versus showing a photograph. This means SVGs scale infinitely without losing quality—perfect for icons, logos, and illustrations.

Basic SVG Syntax

An SVG element looks like this:

<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="40" fill="blue" />
</svg>

The xmlns declares the XML namespace (required for standalone SVGs), and shapes like circle, rect, and path define the visual content.

Inline SVG: Embedded in HTML

Inline SVG means placing the entire <svg> element directly in your HTML:

<button>
  <svg width="24" height="24">
    <path d="M3 6h18v2H3z"/>
  </svg>
  Menu
</button>

Benefits: Full CSS/JavaScript control, no HTTP request, can animate individual elements, immediate rendering.

Drawbacks: Increases HTML size, not cached separately, clutters markup for complex graphics.

External SVG: Referenced File

External SVGs work like other images:

<img src="logo.svg" alt="Company Logo" width="200">

Benefits: Cached by browsers, keeps HTML clean, reusable across pages, smaller HTML payload.

Drawbacks: Extra HTTP request (mitigated by caching), limited styling control, can't animate internal elements.

When to Use Each

Use inline for small icons you need to style/animate dynamically. Use external for larger graphics, repeated elements, or when caching matters more than manipulation.

Key Takeaway: Inline SVG for interactive icons needing CSS/JS control; external SVG for cacheable, reusable graphics where styling control isn't critical.