SVG (.svg) File Format
Scalable Vector Graphics — the web's XML-based, resolution-independent image format that scales to any size without losing quality.
On this page
What is SVG?
SVG stands for Scalable Vector Graphics. Unlike raster image formats such as PNG or JPEG, which store images as grids of pixels, SVG describes images using mathematical shapes — lines, curves, circles, and paths — defined in plain XML text. The result is an image that can be rendered at any size, from a 16×16 favicon to a 10,000-pixel billboard, without any loss of sharpness or quality.
SVG was developed by the World Wide Web Consortium (W3C) and first proposed in 1999. The SVG 1.1 specification became a W3C Recommendation in 2003 and remains the widely-deployed standard today. SVG 2.0 is an ongoing W3C specification that adds features including better text layout, new paint server types, and closer alignment with CSS.
Because SVG files are plain XML text, they are human-readable, editable in any text editor, crawlable by search engines, and accessible to screen readers — advantages that no raster format can match.
Technical Specifications
| Property | Value |
|---|---|
| Full Name | Scalable Vector Graphics |
| File Extensions | .svg (plain), .svgz (gzip-compressed) |
| MIME Type | image/svg+xml |
| File Structure | XML 1.0 document |
| Developed By | World Wide Web Consortium (W3C) |
| First Release | September 4, 2001 (SVG 1.0) |
| Current Standard | SVG 1.1 (2nd Edition, 2011); SVG 2.0 (draft) |
| Namespace URI | http://www.w3.org/2000/svg |
| Encoding | UTF-8 (recommended), UTF-16 |
| Compression | None (plain .svg) or gzip (.svgz); also compresses well via HTTP gzip/brotli |
| Color Model | RGB (sRGB by default); ICC color profiles supported |
| Transparency | Full alpha transparency via opacity, fill-opacity, RGBA |
| Animation | SMIL declarative animation, CSS animations, JavaScript |
| Interactivity | Full DOM access — event listeners, hover states, JavaScript manipulation |
| Accessibility | <title>, <desc>, ARIA attributes supported |
| Scripting | JavaScript via embedded <script> tags |
| Styling | Inline styles, internal <style>, or external CSS |
| License | Open W3C standard — royalty-free |
File Structure & Syntax
An SVG file is an XML document. At minimum it needs a root <svg> element with the W3C namespace declaration and a viewBox attribute. Everything inside is standard XML — tags are case-sensitive, all attributes must be quoted, and every opened tag must be closed.
<!-- Minimal valid SVG --> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100"> <!-- A blue circle --> <circle cx="50" cy="50" r="40" fill="#2563eb" stroke="#1e40af" stroke-width="2"/> <!-- Centered white label --> <text x="50" y="55" text-anchor="middle" font-family="sans-serif" font-size="14" fill="white">Hello</text> </svg>
<img src="logo.svg"> or CSS background-image. Inline SVGs allow full CSS and JavaScript control; external SVGs are simpler to cache but isolated from the page's styles.Common SVG Attributes
| Attribute | What it does |
|---|---|
viewBox | Defines the internal coordinate system: "min-x min-y width height". Makes the SVG scale correctly at any display size. |
width / height | The rendered size on screen. Use CSS or omit for fluid scaling. |
xmlns | Required namespace declaration: http://www.w3.org/2000/svg |
preserveAspectRatio | Controls how the content fits when the aspect ratio of viewBox and the container differ. |
fill | Fill color for shapes. Accepts named colors, hex, rgb(), or none. |
stroke | Border/outline color. Use with stroke-width. |
opacity | Overall element transparency (0–1). |
transform | Translate, rotate, scale, or skew any element: transform="rotate(45 50 50)". |
SVG Element Types
SVG provides a rich vocabulary of elements for drawing shapes, rendering text, grouping content, applying effects, and controlling reuse.
Basic Shapes
<rect>Rectangle. Set x, y, width, height. Add rx/ry for rounded corners.
<circle>Circle defined by center point (cx, cy) and radius (r).
<ellipse>Like circle but with separate x-radius (rx) and y-radius (ry).
<line>A straight line between two points (x1,y1) and (x2,y2).
<polyline>A series of connected line segments. Defined by a points list.
<polygon>Closed shape from a points list. Auto-closes the last point to the first.
Paths
The <path> element is the most powerful shape in SVG. Its d attribute accepts a mini-language of commands: M (move to), L (line to), C (cubic bezier), Q (quadratic bezier), A (arc), and Z (close path). Uppercase commands use absolute coordinates; lowercase use relative.
<!-- A simple arrow using path commands --> <path d="M 10 50 L 80 50 M 65 35 L 80 50 L 65 65" stroke="#16a34a" stroke-width="4" fill="none" stroke-linecap="round"/>
Text Elements
<text>Renders a text string at position x, y. Supports font-size, font-family, text-anchor, etc.
<tspan>Sub-element of text for inline style changes or relative positioning within a text run.
<textPath>Renders text that flows along the curve of a path element.
Structure & Reuse
<g>Group element. Applies transforms and styles to all children at once.
<defs>Container for reusable definitions (gradients, filters, symbols) that aren't rendered directly.
<symbol>Defines a reusable graphic with its own viewBox. Rendered via <use>.
<use>Stamps a copy of a <symbol> or any element at a new location.
<clipPath>Defines a clipping region. Only the parts of an element inside the clip are visible.
<mask>Like clipPath but uses luminance or alpha channel for soft-edged masking.
Paint Servers & Filters
<linearGradient>Defines a linear color gradient, referenced by fill or stroke.
<radialGradient>Defines a circular color gradient radiating from a focal point.
<pattern>Tiles a graphic as a repeating fill pattern.
<filter>Container for filter effects (blur, shadow, color matrix, composite, etc.).
<feGaussianBlur>Filter primitive that applies a Gaussian blur to the source graphic.
<feDropShadow>Shorthand filter for a drop shadow (dx, dy, stdDeviation, flood-color).
Animation Elements
<animate>Animates a single attribute value over time (SMIL-based).
<animateTransform>Animates a transform (translate, rotate, scale) on an element.
<animateMotion>Moves an element along a motion path over time.
<img> tags and email clients. CSS animation requires inline SVG inside HTML. For complex interactive animations, JavaScript (GSAP, Anime.js) offers the most control.Coordinate System & viewBox
SVG uses a Cartesian coordinate system where the origin (0, 0) is at the top-left corner. X increases to the right; Y increases downward. The viewBox attribute is what makes SVG truly scalable.
<!-- viewBox="min-x min-y width height" --> <!-- This SVG uses 0–200 internally but renders at any CSS size --> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 100" width="100%"> <!-- Shapes use 0-200 / 0-100 coordinates --> <rect x="10" y="10" width="180" height="80" rx="8" fill="#dcfce7" stroke="#22c55e"/> </svg>
Without viewBox, the SVG renders at the pixel dimensions specified by width and height and will not scale. With viewBox, the SVG can be set to width="100%" and will fill any container at perfect sharpness.
The preserveAspectRatio attribute controls what happens when the viewBox aspect ratio doesn't match the container. The default, xMidYMid meet, centers and scales uniformly (letterboxing). Use none to stretch, or xMidYMid slice to crop-fill like CSS background-size: cover.
SVG vs. PNG vs. WebP vs. GIF
Choosing the right format comes down to what kind of image you have and how it will be used. SVG is unmatched for graphics defined by shapes; raster formats win for photographs and camera imagery.
| Feature | SVG | PNG | WebP | GIF | JPEG |
|---|---|---|---|---|---|
| Scales to any size | Yes | No | No | No | No |
| Best for photos | No | OK | Yes | No | Yes |
| Best for logos/icons | Yes | OK | OK | No | No |
| Transparency (alpha) | Full | Full | Full | 1-bit | No |
| Animation | Yes (SMIL/CSS) | No | Via APNG-like spec | Yes (limited) | No |
| CSS styling | Full | No | No | No | No |
| JavaScript interaction | Full DOM | No | No | No | No |
| Editable as text | Yes (XML) | No | No | No | No |
| Search-engine indexable | Yes (inline) | No | No | No | No |
| File size (simple icon, 24px) | ~500 B | ~2 KB | ~1.5 KB | ~1 KB | ~3 KB |
| File size (photo, 1200px) | Very large | ~800 KB | ~120 KB | ~2 MB+ | ~180 KB |
| Accessibility support | title, desc, ARIA | alt text only | alt text only | alt text only | alt text only |
| Browser support | 97%+ (IE9+) | Universal | 95%+ (no IE) | Universal | Universal |
| Email client support | Poor | Excellent | Good | Excellent | Excellent |
When to Use SVG (and When Not To)
Browser & Platform Support
SVG 1.1 is supported in all modern browsers and has been since Internet Explorer 9 (released 2011). CSS animations and transitions work in all evergreen browsers. SMIL animation support dropped in Blink-based browsers (Chrome/Edge) for a period but has since been retained. JavaScript DOM manipulation of inline SVG works everywhere modern.
| SVG Feature | Chrome | Firefox | Safari | Edge |
|---|---|---|---|---|
| Basic shapes & paths | Full | Full | Full | Full |
| CSS animation | Full | Full | Full | Full |
| SMIL animation | Full | Full | Full | Full |
| Filters (blur, shadow) | Full | Full | Full | Full |
| Inline SVG in HTML5 | Full | Full | Full | Full |
| foreignObject | Full | Full | Partial | Full |
| SVG in CSS background | Full | Full | Full | Full |
| SVG fonts | Dropped | Dropped | Partial | Dropped |
<img src="file.svg">, the browser blocks scripts and external references. Use inline SVG to access the DOM, run JavaScript, or load external fonts inside the graphic.Creating & Editing SVG Files
SVG can be created by hand in any text editor, exported from a vector design application, or generated programmatically by code. Each approach has its place.
Design Tools (GUI)
| Tool | Type | Notes |
|---|---|---|
| Figma | Web app (free/paid) | Clean SVG export; use "Include id attribute" for JS targeting. Strips unnecessary metadata well. |
| Adobe Illustrator | Desktop (paid) | Industry standard. Use "SVG 1.1" profile on export. Verbose output — run SVGO after. |
| Inkscape | Desktop (free) | Open-source, full-featured. Native format is SVG. Good for hand-editing complex paths. |
| Affinity Designer | Desktop (paid) | Clean exports, good value alternative to Illustrator. |
| Sketch | Desktop (paid, macOS) | Good SVG export; popular for icon libraries. |
| Canva | Web app (free/paid) | Exports SVG for Pro users. Output tends to be verbose. |
Code & Libraries
| Library | Language | Best For |
|---|---|---|
| D3.js | JavaScript | Data-driven SVG charts and visualizations. The gold standard for complex SVG generation. |
| Snap.svg | JavaScript | SVG manipulation and animation library. Like jQuery for SVG. |
| GSAP (SVG plugin) | JavaScript | High-performance animation of SVG elements with fine timeline control. |
| SVG.js | JavaScript | Lightweight library for SVG creation and manipulation. |
| CairoSVG | Python | Converts SVG to PNG/PDF on the server side. |
| Sharp (via resvg) | Node.js | Fast server-side SVG rasterization to PNG/JPEG. |
Optimizing SVG
Design tools export SVG with editor metadata, unused definitions, verbose decimal precision, and attribute duplication that inflates file size. Always optimize before deploying.
| Tool | Type | Notes |
|---|---|---|
| SVGO | CLI / Node.js | The standard SVG optimizer. Dozens of configurable plugins. Typical savings: 20–60%. |
| SVGOMG | Web app | GUI wrapper around SVGO. Paste SVG and toggle plugins visually. Free at jakearchibald.github.io/svgomg/ |
| Vecta Nano | Web app | Alternative optimizer; sometimes smaller than SVGO on complex paths. |
Converting SVG Files
SVG to PNG / JPEG (Rasterizing)
Converting SVG to a raster format is called rasterizing. You'll want to do this when you need a fixed-size image for a context that doesn't support SVG — such as email, Open Graph (social share) images, app store screenshots, or Microsoft Office.
| Method | Quality | Notes |
|---|---|---|
| Browser screenshot | Good | Open SVG in browser, screenshot. Simple but no resolution control. |
| Inkscape CLI | Excellent | inkscape input.svg --export-filename out.png --export-width 1200 |
| Sharp (Node) | Excellent | Via resvg-js: high-fidelity, fast, runs server-side or in CI. |
| CairoSVG (Python) | Very good | cairosvg input.svg -o out.png --output-width 1200 |
| Figma / Illustrator | Excellent | Export at any resolution with full font/effect rendering. |
PNG / Illustrator / Other Formats to SVG (Vectorizing)
Converting a raster image to SVG is called vectorizing or auto-tracing. Results vary dramatically based on the complexity and content of the source image. Simple flat-color logos trace well; photographs generally do not.
| Tool | Notes |
|---|---|
| Inkscape (Trace Bitmap) | Free; uses potrace. Good for logos and silhouettes. Path > Trace Bitmap. |
| Adobe Illustrator (Image Trace) | Industry standard. Multiple presets for logos, photos, line art. |
| Vector Magic | Web/desktop app, paid. Among the best automated vectorization quality. |
| Vectorizer.io | Free web app. Fast AI-based vectorization for simple graphics. |
Frequently Asked Questions
What does SVG stand for?
SVG stands for Scalable Vector Graphics. "Scalable" refers to the fact that the image can be displayed at any size without losing quality. "Vector" means the image is described with mathematical shapes (lines, curves, polygons) rather than a grid of pixels. "Graphics" is simply the output format — a 2D image.
Why is my SVG blurry?
A blurry SVG is almost always caused by a missing or incorrect viewBox attribute. Without viewBox, the browser renders the SVG at fixed pixel dimensions and scales it up pixel-by-pixel like a raster image. Fix: add viewBox="0 0 W H" where W and H match your internal coordinate space. Then set width="100%" or a CSS size — the browser will re-render it at crisp resolution.
Can SVG files contain malware?
Yes — SVG files can contain embedded JavaScript via <script> tags, which is why many file upload systems block SVG by default. When an SVG is rendered inline in a webpage it shares the document's JavaScript execution context, so malicious scripts inside it could run. Mitigations: serve SVGs from a separate CDN domain, use <img> tags (which sandbox scripts), sanitize uploaded SVGs with a library like DOMPurify's SVG mode, or disallow SVG uploads in your CMS entirely.
What is the difference between .svg and .svgz?
An .svgz file is simply an SVG file that has been gzip-compressed. The contents are identical to a .svg file; the file is just smaller on disk. Servers must send the Content-Encoding: gzip header when serving .svgz so that browsers decompress it correctly. In practice, most servers now apply gzip or Brotli compression on the fly to .svg responses, making .svgz files largely unnecessary — a plain .svg served with HTTP compression achieves the same transfer size.
Can I use SVG in a CSS background-image?
Yes. Both external file references (background-image: url('icon.svg')) and inline data URIs (background-image: url("data:image/svg+xml,...")) work in all modern browsers. However, SVG used via CSS background-image is fully sandboxed — scripts won't run and external references (fonts, images) won't load. For decorative background shapes and patterns this is ideal.
How do I make an SVG responsive?
Three things are needed: (1) add a viewBox attribute to the root <svg> element; (2) remove any fixed width and height attributes, or set them to width="100%"; (3) optionally set max-width in CSS to cap the maximum display size. The SVG will then scale fluidly to fill its container. If you're using inline SVG, also make sure no outer wrapper has a fixed width that prevents the container from shrinking.
Does Google index text inside SVG files?
Google can index text content in inline SVG (embedded directly in HTML) and in .svg files that are linked from pages. However, text in SVG is not treated the same as body HTML — it doesn't carry the same ranking weight and may not appear in featured snippets. For SEO-critical text, always use actual HTML text alongside or instead of SVG text. SVG files can also carry <title> and <desc> elements that Google reads.
What is the best way to use SVG icons on a website?
The recommended approach in modern web development is an SVG sprite: define all icons as <symbol> elements inside a hidden <svg> block in the page (or a separate file loaded once), then reference each icon anywhere on the page with <use href="#icon-name">. This approach gives you one HTTP request for all icons, easy CSS color control via currentColor, and no duplication. The alternative — individual inline SVGs — works fine for small icon counts but becomes verbose at scale.
