What is SVG? A practical introduction

SVG is an XML-based format for 2D vector graphics. Learn how it works, the three ways to put it on a page, and when another format is the better choice.

SVG stands for Scalable Vector Graphics. It is an XML-based markup language for describing two-dimensional graphics, and it is an open standard developed by the W3C. Where a PNG or JPEG stores a grid of colored pixels, an SVG stores instructions: draw a circle here, draw a curve there, fill it with this color. The browser follows those instructions at whatever size it needs.

Shapes, not pixels

The SVG 2 specification describes graphic objects as vector shapes, images and text that can be grouped, styled, transformed and composited. A small file can be written by hand:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 120">
  <circle cx="60" cy="60" r="40" fill="#1f3fe0"/>
  <rect x="110" y="30" width="60" height="60" fill="#d4006a"/>
</svg>

Each element is a shape with attributes. Edit a number and the picture changes. Try it below: change r, fill or the rectangle’s x and watch the result.

Because a shape is described mathematically, it stays sharp when enlarged. MDN lists resolution independence as a headline feature, along with the fact that SVG is text: files can be searched, indexed, scripted and compressed, and edited in any text editor.

What SVG can draw

The basic shape elements are <rect>, <circle>, <ellipse>, <line>, <polyline> and <polygon>. MDN notes that every basic shape can also be written as a <path>, which is the general-purpose element for lines, arcs and curves. The paths guide covers it in detail.

Beyond shapes, the SVG 2 introduction lists:

  • Text, with the usual font and paint properties.
  • Embedded raster images.
  • Grouping with <g>, CSS styling, and nested transformations (see transforms).
  • Clipping paths, alpha masks and filter effects (see masks and clipping and filters).
  • Animation, declared in markup or driven by scripts (see animation).
  • Interactivity through the SVG DOM and event handlers.

Strokes, gradients and opacity are set with attributes or CSS. Here is a stroked, rounded shape with a dashed outline:

Three ways to put SVG on a page

How you include an SVG changes what it can do.

Inline. Paste the <svg> markup straight into your HTML. The shapes become part of the page’s DOM, so page CSS and JavaScript can reach them. This is the right choice for interactive graphics and for icons that need to change color with the surrounding text.

As an image. Reference a file with <img src="logo.svg" alt="..."> or a CSS background-image. This is simple and cacheable, but MDN lists restrictions in this mode: JavaScript is disabled, and external resources such as linked images and stylesheets are not loaded. Treat an SVG used this way as a picture, not an application.

With <object>, <iframe> or <embed>. MDN notes these contexts are not subject to the image restrictions, so a linked file can keep its own scripts and fetch resources. They are used less often today, and you give up easy styling from the host page.

A related detail from MDN: the xmlns="http://www.w3.org/2000/svg" attribute is required on the outermost <svg> of a standalone SVG file, or in HTML that is served as XML. Inside ordinary HTML it is not needed, though including it does no harm and makes the snippet portable.

Size and the viewBox

By default width and height on <svg> are auto, and they set the displayed size of the viewport, not the size of the coordinate system. The coordinate system comes from the viewBox attribute, which has no default value. Writing viewBox="0 0 200 120" says that the drawing uses a 200 by 120 grid, and the browser scales that grid to fit the element. Once you understand this, responsive graphics become straightforward. The viewBox guide explains it step by step.

Strengths

  • Sharp at any size. One file serves a favicon and a billboard.
  • Often small for simple graphics. Icons, logos and diagrams usually need few instructions. This is a tendency, not a rule: a complex illustration with thousands of path segments can outweigh an equivalent compressed bitmap.
  • Styleable and scriptable. When inline, shapes respond to CSS, including hover states and custom properties.
  • Accessible when authored well. Text in an SVG is real text, and you can add titles and descriptions. See accessibility.
  • Editable. Changing a color or a label is a text edit.

When not to use SVG

SVG is a poor fit for photographs. MDN’s image-format guidance says plainly that SVG is not suited to photographs or bitmap-style images, though bitmaps can be embedded. A photo traced into vector shapes either looks like a stylized illustration or produces an enormous file. Use JPEG, WebP or AVIF instead. The SVG vs PNG and JPEG guide compares them directly.

Other cases where a different tool is better:

  • Pixel-level drawing and per-frame effects, such as image filters on video or paint programs. A canvas suits these. See SVG vs Canvas.
  • 3D scenes and heavy GPU work. That is WebGL territory. See SVG vs WebGL.
  • Untrusted uploads. An SVG is a document that can contain scripts and references, so accepting SVG files from strangers needs care. See security.

Where to go next

If you are new to SVG, read viewBox first, because almost every layout surprise traces back to it. Then move on to paths for drawing and transforms for positioning. When you are ready to make things move or respond to input, animation and SVG with JavaScript pick up from here.

Every example on this site is live. Edit the markup in any playground and the picture updates, which is the fastest way to learn what each attribute does.

Sources

Checked against these references on 2026-11-21.