Understanding SVG: The Scalable Vector Format

SVG is fundamentally different from every other image format you are likely to encounter. While JPG, PNG, and WebP store images as grids of colored pixels (raster graphics), SVG stores images as mathematical descriptions of shapes, lines, and curves. This means an SVG logo looks perfectly sharp whether it is 16 pixels or 16,000 pixels wide.

SVG stands for Scalable Vector Graphics. It is an XML-based open standard maintained by the W3C, the same organization that standardizes HTML and CSS. Every modern browser renders SVG natively, and because SVG files are plain text, they can be styled with CSS, animated with JavaScript, and indexed by search engines.

Vector vs Raster: The Core Difference

Raster images (JPG, PNG, WebP) are made of pixels. Zoom in far enough on any raster image and you will see a grid of colored squares. The image has a fixed resolution: a 1000x1000 pixel PNG contains exactly one million pixels, and scaling it up means each pixel becomes a bigger, blockier square.

Vector images (SVG) are made of mathematical instructions. A circle is stored as a center point and radius. A line is stored as two endpoints. A curve is stored as control points for a Bezier equation. There are no pixels, no fixed resolution, and no quality loss when scaling. The browser recalculates the shapes at whatever resolution is needed.

This distinction determines when each type is appropriate. Photographs and complex images with millions of unique colors are raster by nature. Logos, icons, diagrams, charts, and illustrations with defined shapes and limited colors are natural fits for vector.

SVG as XML: A Text-Based Image

An SVG file is a text file written in XML. You can open it in any text editor and read (or modify) its contents directly. A simple red circle might look like: .

This text-based nature has powerful implications. SVG files can be embedded directly in HTML without a separate file request. Search engines can index text content inside SVGs. Build tools can process and optimize SVGs as part of a standard text pipeline. And version control systems like Git can track changes to SVGs meaningfully, showing exactly which shapes changed between versions.

The XML structure also means SVGs can be self-documenting. Elements can have title and desc tags for accessibility, IDs and classes for CSS styling, and data attributes for JavaScript interaction.

The viewBox: SVG's Coordinate System

The viewBox attribute is the most misunderstood aspect of SVG. It defines the internal coordinate system of the graphic, separate from its display size. A viewBox of "0 0 100 100" means the SVG's internal space ranges from 0 to 100 on both axes, regardless of how large or small it appears on screen.

The width and height attributes (or CSS dimensions) control the display size. The viewBox controls how the internal coordinates map to that display area. If an SVG has viewBox="0 0 100 100" and is displayed at 500x500 pixels, every internal unit maps to 5 screen pixels. The graphic scales perfectly.

When an SVG has a viewBox but no explicit width and height, it becomes fully responsive by default. It will fill whatever container it is placed in while maintaining its aspect ratio. This is why SVG is ideal for responsive web design.

Styling SVG with CSS

Inline SVGs (embedded directly in HTML) can be styled with CSS just like any other HTML element. You can change fill colors, stroke widths, opacity, and transforms using CSS selectors. This enables powerful effects like color changes on hover, dark mode support, and themed icon sets from a single SVG source.

External SVG files (loaded via img tags or CSS background-image) cannot be styled with CSS. They are treated as opaque images by the browser. If you need CSS control over an SVG, embed it inline or use an SVG sprite sheet with a use element.

CSS custom properties (variables) work inside inline SVGs, which opens up dynamic theming. You can define a color in CSS and reference it in your SVG's fill attribute, allowing the same SVG to adapt to different color themes without duplicating the graphic.

SVG Animation: SMIL vs CSS vs JavaScript

SVG supports three approaches to animation, each with different strengths. SMIL (Synchronized Multimedia Integration Language) is SVG's native animation syntax, built directly into the SVG specification. CSS animations and transitions work on inline SVGs just like on HTML elements. JavaScript libraries like GSAP, Anime.js, and Framer Motion can animate SVG elements programmatically.

For simple animations (color changes, opacity fades, basic transforms), CSS is the cleanest approach. For complex path animations (drawing strokes, morphing shapes), SMIL or JavaScript is needed. For interactive animations responding to user input, JavaScript provides the most control.

One important consideration: animated SVGs can significantly impact performance. Complex SVGs with many animated elements can cause layout thrashing and high CPU usage. Profile your animations and simplify where possible.

SVG Accessibility

SVG has strong accessibility support when used correctly. Every SVG should include a title element (equivalent to an image alt attribute) and optionally a desc element for a longer description. The role="img" attribute tells screen readers to treat the SVG as an image, and aria-labelledby can reference the title and desc elements.

For decorative SVGs that convey no information (background patterns, separator lines), use aria-hidden="true" to hide them from screen readers. For interactive SVGs (clickable icons, chart elements), appropriate ARIA roles and labels make them fully accessible.

Text content inside SVGs is readable by screen readers and searchable by browsers. This makes SVG charts and diagrams inherently more accessible than raster image alternatives, as long as text is rendered as text elements rather than outlined paths.

SVG Security Concerns

Because SVG is XML that can contain embedded JavaScript, it poses unique security risks that raster formats do not. An SVG file can contain script tags, event handlers, and external resource references. A malicious SVG could execute JavaScript in a user's browser, make network requests, or attempt cross-site scripting (XSS) attacks.

When accepting user-uploaded SVGs, always sanitize them by removing script elements, event handler attributes (onclick, onload, etc.), and external references (xlink:href to remote URLs). Libraries like DOMPurify can sanitize SVG content. Alternatively, render user SVGs inside a sandboxed iframe or convert them to a raster format before displaying.

SVGs loaded via img tags or CSS background-image are sandboxed by the browser and cannot execute scripts. This is the safest way to display untrusted SVGs, at the cost of losing CSS styling and interactivity.

When SVG Is the Wrong Choice

SVG is not suitable for photographs or complex natural images. A photo of a landscape would require thousands of vector paths to approximate the detail, resulting in a file much larger than a JPG and with visible artifacts along color boundaries.

SVG also struggles with images that have complex textures, gradients between many colors, or noise. A watercolor painting, a satellite photo, or a medical scan should always be raster. As a rule of thumb: if the image was created by a camera or has photographic content, use a raster format.

Very complex illustrations with thousands of paths can also be problematic. An SVG map with extremely detailed coastlines or a technical diagram with thousands of components might render slowly and produce large files. In these cases, consider simplifying the paths or using a raster format at the required resolution.

Frequently Asked Questions

How does SVG file size compare to PNG?

It depends entirely on the image complexity. A simple logo might be 2KB as SVG and 50KB as PNG. A complex illustration with thousands of paths might be 500KB as SVG but only 200KB as PNG. SVG excels for simple shapes, icons, and logos. PNG wins for complex illustrations and anything with photographic content.

What tools can I use to create SVG files?

Adobe Illustrator and Affinity Designer are professional-grade vector editors that export SVG. Figma and Sketch export SVGs from their design tools. Inkscape is a free, open-source SVG editor. For code-first approaches, you can write SVG markup by hand or generate it programmatically with JavaScript.

Can I use SVG for website icons?

Yes, SVG is the preferred format for website icons in 2026. SVG icons scale perfectly to any size, can be styled with CSS (including color themes and dark mode), and are typically smaller than equivalent PNG icon sets. Icon libraries like Heroicons, Phosphor, and Lucide all distribute their icons as SVG.

Does SVG work in email?

SVG support in email clients is poor. Most major email clients either do not render SVG at all or render it inconsistently. Apple Mail and some webmail clients support SVG, but Outlook and Gmail have limited or no support. For email, always use PNG or JPG for images.

Convert SVG to PNG now

Convert SVG to JPG

Related Reading