Contact

What is SVG?

Definition

SVG (Scalable Vector Graphics) is an XML-based W3C image format that describes two-dimensional graphics as shapes, paths and text rather than a grid of pixels. Because it is a set of drawing instructions, it stays sharp at any size and screen density, and for logos, icons, diagrams and simple illustrations it is usually smaller than raster formats. SVG can be embedded directly in HTML, styled with CSS and manipulated with JavaScript.

Also known as: Scalable Vector Graphics, vector graphics, .svg, image/svg+xml

Diagram showing a few lines of SVG markup becoming a vector graphic that stays sharp at any size and can be styled with CSS

Drawing instructions instead of pixels

A PNG or JPEG stores the colour of every pixel. An SVG stores instructions such as “a circle of this radius at these coordinates” or “a curve through these points”, and the browser draws them at whatever size the image ends up on screen. The file is plain text you can open in any editor:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
  <circle cx="12" cy="12" r="10" fill="none" stroke="currentColor" stroke-width="2"/>
  <path d="M8 12l3 3 5-6" fill="none" stroke="currentColor" stroke-width="2"/>
</svg>

The viewBox sets the drawing's internal coordinate system, here a 24×24 unit canvas, which scales proportionally whether the icon is rendered at 16 or 600 pixels. currentColor makes the stroke inherit the surrounding text colour, so the same icon adapts to light and dark themes without a second file.

On the standards side, the latest finished W3C Recommendation is SVG 1.1 Second Edition from 2011. SVG 2 has been a Candidate Recommendation since 2018, and browsers already implement a good part of it.

Ways to put SVG on a page

MethodStrengthLimitation
<img src="logo.svg">Cached as a separate file; scripts inside never runPage CSS cannot reach inside the graphic
Inline <svg>Individual parts can be styled and scripted; no extra requestAdds weight to the HTML and is resent with every page
CSS background-imageConvenient for decorative patternsCarries no accessible name; search engines do not treat it as a content image
<use href="sprite.svg#icon">Many icons from one cached fileSlightly more setup

When a vector is the wrong tool

Photographs cannot be expressed as vectors; tracing one produces either a huge file or a crude approximation. Very complex illustrations are also a trap: an SVG with thousands of paths may be small on the wire, but the browser recalculates those paths every time it paints, and scrolling or animation can stutter on low-end devices. For that kind of artwork, a raster format such as PNG or WebP is often lighter overall.

User-uploaded SVG is a security question

SVG is a document format, so a file can contain script elements, event-handler attributes or references to external resources. Scripts in an SVG displayed through img do not run. But if a user-uploaded SVG is opened directly from your own domain, its code can execute in your site's context, which turns it into a cross-site scripting hole. Systems that accept uploads should sanitise SVG files, convert them to a raster format, or serve them from a separate domain with a restrictive content security policy.

Optimisation and accessibility

  • SVGs exported from design tools carry editor metadata, redundant groups and over-precise decimals. Optimisers such as SVGO strip these and often shrink the file considerably.
  • Being text, SVG compresses very well with gzip or Brotli, so make sure your server compresses image/svg+xml responses too.
  • A meaningful SVG added through img needs alt text like any other image. Inline SVG uses role="img" together with a title element, while purely decorative icons get aria-hidden="true".
  • Google Images can index SVG files referenced from an img element. SVG also works as a favicon, though keeping a PNG or ICO fallback for older browsers is still common practice.

Related terms

← Back to the glossary