What Is SVG? A Developer's Guide to Scalable Vector Graphics
By Álvaro G.V. · Updated
SVG (Scalable Vector Graphics) is an XML-based image format that describes images as mathematical shapes rather than pixels. Because vectors are resolution-independent, an SVG looks perfectly sharp at any size — from a 16px favicon to a 4K wallpaper — without any increase in file size.
In practice: use SVG for icons, logos, diagrams and charts, and a raster format (WebP, AVIF, JPEG) for photos. This guide covers how the format works, the viewBox attribute, the ways to put SVG on a page, path syntax, and the mistakes that cause invisible icons, broken data URLs and security holes.
Because SVG is plain XML, you can read and tidy it with the XML formatter, and inline it in HTML or CSS as a Base64 data URL with the image to Base64 converter.
How SVG Works
Unlike raster formats (PNG, JPEG, WebP) that store a grid of pixels, SVG stores geometric instructions. A circle is described as a center point and radius. A logo is a set of paths with coordinates. The browser renders these instructions at whatever size and resolution is needed.
<!-- A simple SVG icon — an arrow pointing right -->
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<line x1="5" y1="12" x2="19" y2="12"/>
<polyline points="12 5 19 12 12 19"/>
</svg>
<!-- This SVG is 200 bytes and renders perfectly at any size:
16px icon, 64px button icon, 200px illustration — all identical quality -->Because SVG is XML text, it can be:
- Embedded directly in HTML
- Styled with CSS (color, size, hover effects)
- Manipulated with JavaScript
- Compressed with gzip (text compresses very well)
- Edited in a text editor
SVG vs PNG vs WebP: When to Use Each
| Property | SVG | PNG | WebP |
|---|---|---|---|
| Type | Vector | Raster | Raster |
| Scales without quality loss | ✅ Yes | ❌ No | ❌ No |
| Transparency | ✅ Yes | ✅ Yes | ✅ Yes |
| Animation | ✅ Yes (CSS/JS) | ❌ No | ✅ Yes (animated) |
| CSS styleable | ✅ Yes (inline) | ❌ No | ❌ No |
| Best for photos | ❌ No | ⚠️ OK | ✅ Yes |
| Best for icons/logos | ✅ Yes | ⚠️ OK (fixed size) | ⚠️ OK (fixed size) |
| File size for icons | ✅ Very small | ⚠️ Medium | ⚠️ Small |
| File size for photos | ❌ Huge/impractical | ⚠️ Large | ✅ Small |
| Browser support | ✅ All modern browsers | ✅ All browsers | ✅ All modern browsers |
Rule of thumb: Use SVG for anything that was created in a vector tool (Figma, Illustrator, Sketch) — icons, logos, illustrations, charts, diagrams. Use WebP (or JPEG) for photographs and images with gradients or photographic complexity.
Anatomy of an SVG File
<!-- xmlns: required namespace (when the file is used standalone)
width/height: display size, can also be set via CSS
viewBox: coordinate system as minX minY width height
fill: default fill inherited by child elements -->
<svg
xmlns="http://www.w3.org/2000/svg"
width="100"
height="100"
viewBox="0 0 100 100"
fill="none"
>
<!-- Basic shapes -->
<circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="white"/>
<rect x="10" y="10" width="30" height="20" fill="red"/>
<line x1="0" y1="0" x2="100" y2="100" stroke="black"/>
<polyline points="20,80 40,20 60,80 80,20"/>
<polygon points="50,10 90,90 10,90"/>
<!-- Path — the most powerful SVG element -->
<!-- M=moveto, L=lineto, C=curveto, Z=closepath -->
<path d="M10,50 C10,20 90,20 90,50 S90,80 10,80 Z" fill="green"/>
<!-- Text -->
<text x="50" y="95" text-anchor="middle" font-size="12">Hello</text>
<!-- Group — apply transforms or styles to multiple elements -->
<g transform="rotate(45, 50, 50)" opacity="0.5">
<rect x="40" y="40" width="20" height="20" fill="purple"/>
</g>
</svg>The viewBox Attribute Explained
viewBox is the most important — and most misunderstood — SVG attribute. It defines the internal coordinate system, separate from the displayed size.
<!-- viewBox="minX minY width height" -->
<svg width="200" height="200" viewBox="0 0 100 100">
<!-- Coordinates inside the SVG go from 0,0 to 100,100 -->
<!-- The browser scales this to fill the 200x200 display area -->
<circle cx="50" cy="50" r="40"/>
<!-- This circle appears centered in the 200x200 area, scaled up 2x -->
</svg>
<!-- viewBox lets you scale SVG content independently of display size:
The same SVG can be used as a 16px icon or a 500px hero image
by just changing the width/height attributes or CSS -->Reading Path Data
Almost every icon is one or more <path> elements, and the d attribute is a compact drawing language. Uppercase commands use absolute coordinates; lowercase ones are relative to the current point.
| Command | Meaning | Parameters |
|---|---|---|
| M / m | Move to (start a new subpath, draws nothing) | x y |
| L / l | Line to | x y |
| H / h, V / v | Horizontal / vertical line | x, or y |
| C / c | Cubic Bézier curve | x1 y1 x2 y2 x y |
| S / s | Smooth cubic (first control point mirrored) | x2 y2 x y |
| Q / q, T / t | Quadratic Bézier, smooth quadratic | x1 y1 x y, or x y |
| A / a | Elliptical arc | rx ry rotation large-arc sweep x y |
| Z / z | Close path back to the subpath start | none |
So the arrow M5 12h14M12 5l7 7-7 7 reads: move to (5,12), draw 14 units right; move to (12,5), draw 7 right and 7 down to the tip, then 7 left and 7 down. Two open strokes, which is why the icon needs stroke and fill="none" to be visible.
Three Ways to Use SVG in HTML
1. Inline SVG (most flexible)
<!-- Paste SVG code directly into HTML -->
<button>
<svg width="20" height="20" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" aria-hidden="true">
<path d="M5 12h14M12 5l7 7-7 7"/>
</svg>
Submit
</button>
<!-- Pros: CSS styleable, JS accessible, no extra HTTP request
Cons: bloats HTML, can't be cached separately -->2. Image tag
<img src="/icons/arrow.svg" alt="Arrow right" width="24" height="24">
<!-- Pros: browser caches the file, simple to use
Cons: CSS can't style internals, JS can't access elements -->3. CSS background-image
.icon-arrow {
background-image: url('/icons/arrow.svg');
background-size: contain;
width: 24px;
height: 24px;
}
/* Or inline as a data URI (useful for small icons) */
.icon-check {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'...%3E");
}4. A sprite with <symbol> and <use>
For many icons, define each once as a <symbol> and reference it by id. The icons stay styleable with CSS like inline SVG, without repeating the path data on every use:
<svg style="display:none">
<symbol id="icon-arrow" viewBox="0 0 24 24">
<path d="M5 12h14M12 5l7 7-7 7" fill="none" stroke="currentColor" stroke-width="2"/>
</symbol>
</svg>
<button><svg width="20" height="20" aria-hidden="true"><use href="#icon-arrow"/></svg> Next</button>Which one to pick
| Method | CSS can recolour it | Cached separately | Scripts inside run |
|---|---|---|---|
| Inline <svg> | Yes | No (part of the HTML) | Yes |
| <img src> | No | Yes | No |
| CSS background-image | No | Yes (or inlined as data URL) | No |
| <symbol> + <use> | Yes, via currentColor and inherited properties | Only if the sprite is an external file | Yes (sprite is inline) |
Why SVG Files From Design Tools Are Bloated
Exporting an SVG from Figma, Adobe Illustrator, or Inkscape produces a file with significant overhead — metadata, comments, editor namespaces, and structural artifacts that aren't visible in the final image but add substantial file size:
<!-- Bloated SVG export from Illustrator (220 bytes of overhead) -->
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 28.0, SVG Export Plug-In -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" width="24px" height="24px"
viewBox="0 0 24 24" style="enable-background:new 0 0 24 24;"
xml:space="preserve">
<g id="Layer_2_00000000000000000">
<g id="icons">
<path class="st0" d="M5,12h14 M12,5l7,7-7,7"/>
</g>
</g>
</svg>
<!-- After optimization (SVGO) — same visual result, 85% smaller -->
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M5 12h14M12 5l7 7-7 7"/>
</svg>Common things an SVG optimizer (minification for vector files) removes: XML declarations, editor comments and metadata, unused namespaces (xmlns:xlink), unnecessary groups, redundant attributes, high-precision decimals in path coordinates (0.12345678 → 0.12), and invisible elements. The standard tool is SVGO: npx svgo icon.svg -o icon.min.svg.
Common SVG Mistakes
Missing xmlns in a standalone file
Inline SVG inside HTML works without xmlns, so it is easy to drop. The same markup saved as a .svg file and loaded through <img> or background-image then renders nothing, because the browser parses it as XML and does not recognise the elements. Keep xmlns="http://www.w3.org/2000/svg" on every file.
No viewBox, so it doesn't scale
With only width and height, resizing the element crops or pads the drawing instead of scaling it. Add a viewBox matching the original coordinates (an exported 24×24 icon gets viewBox="0 0 24 24") and let CSS set the size.
Base64 data URLs for SVG
SVG is text, so a URL-encoded data URL is usually smaller than Base64, which adds a third to the size. Use single quotes inside the SVG and encode at least # (as %23, otherwise it starts a URL fragment and cuts off the image), <, > and %:
.icon-dot {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Ccircle cx='4' cy='4' r='4' fill='%23ef4444'/%3E%3C/svg%3E");
}Colliding ids between inline SVGs
Gradients, clip paths and masks are referenced by id (fill="url(#grad)"). Ids are document-wide, so two inline icons that both define id="grad" share whichever comes first, and one of them renders with the wrong colours or disappears when the other is removed. Give ids a per-icon prefix (SVGO's prefixIds plugin does this).
No accessible name
A meaningful standalone image needs a name: alt on an <img>, or role="img" with aria-label (or a <title> as the first child) on inline SVG. Decorative icons next to a text label should be hidden from screen readers with aria-hidden="true".
Serving user-uploaded SVG
An SVG can contain <script>, event handlers and links. Loaded through <img> they never run, but if a user opens the uploaded file directly, it executes on your origin like any HTML page: stored XSS. Either sanitize uploads (for example DOMPurify with its SVG profile), serve them from a separate domain, or send Content-Security-Policy: script-src 'none' with them.
Need to inline an SVG in HTML or CSS? Open the Image to Base64 converter →
Frequently Asked Questions
Can I use SVG for photographs?▾
No — SVG is a vector format, which means it describes images as mathematical shapes (paths, circles, polygons). Photographs are raster images: a grid of pixels. An SVG 'photograph' would need to encode every pixel as a shape, producing a file that's much larger than JPEG or WebP with no quality benefit. Use JPEG or WebP for photographs, and SVG for illustrations, icons, logos, charts, and diagrams.
Why are SVG files from design tools so large?▾
Tools like Illustrator, Figma, and Inkscape add significant metadata to exported SVGs: editor-specific namespaces, comments, embedded font data, invisible layers, groups with no visual purpose, excessive decimal precision in path coordinates, and metadata like creation date and author. A professionally designed icon might export as 50 KB of SVG from Figma but compress to 3 KB after optimization — a 94% reduction. Always run exported SVGs through an optimizer before using them in production.
What is the difference between inline SVG and an SVG in an img tag?▾
Inline SVG (pasting the SVG code directly into HTML) allows CSS styling and JavaScript interaction with individual elements inside the SVG — you can change colors, animate paths, or respond to hover events on specific parts. An SVG in an <img> tag is treated as an opaque image — CSS and JavaScript cannot access its internals, and it can't reference external stylesheets. Use inline SVG when you need interactive or dynamic SVGs. Use <img> when the SVG is purely decorative or you don't need to style it with CSS.
Do SVGs work in all browsers?▾
Yes — SVG has been supported in all major browsers since 2013. Internet Explorer 9+ supported basic SVG. All modern browsers (Chrome, Firefox, Safari, Edge) have excellent SVG support including animations (CSS and SMIL), filters, clipping, and masking. The only considerations are: (1) some advanced SVG features have inconsistent support across browsers, (2) SMIL animations are deprecated in Chrome (use CSS or JavaScript animations instead), and (3) SVGs used as CSS background-images have some limitations on interactivity.
Can SVG be animated?▾
Yes, in three ways. CSS animations and transitions work on SVG elements just like HTML elements — you can animate fill, stroke, transform, and opacity. JavaScript can manipulate SVG DOM elements directly to create interactive animations. SMIL (Synchronized Multimedia Integration Language) is SVG's native animation format using <animate> elements, but it's deprecated in Chromium-based browsers. For production, use CSS animations or a JavaScript animation library like GreenSock (GSAP).
Related guides
What Is Code Minification and Why It Matters for Web Performance
Minification removes whitespace, comments, and redundant code to reduce file size. Learn how CSS and JavaScript minification work, how much size they save, and what tools to use.
What Is Base64 Encoding and How Does It Work?
Base64 converts binary data into ASCII text using 64 characters. Learn why it exists, how the encoding works step by step, and when to use it.
HEX vs RGB vs HSL: Which Color Format Should You Use in CSS?
HEX, RGB, and HSL all describe the same colors — but each has a different use case in CSS. Learn when to use each and how to convert between them.
JSON vs XML: Which Should You Use for Your API?
JSON and XML both represent structured data, but they have very different strengths. Learn when to use each format, how they compare in size and verbosity, and which is right for your use case.