Introduction to SVG: Scalable Vector Graphics for the Web
Learn SVG fundamentals from coordinate systems to path commands. Understand when to use SVGs, how to optimize them, and best practices for web performance.
Introduction to SVG: Scalable Vector Graphics for the Web
The modern web is a visual medium, and the images we use to build it come in two fundamentally different flavors. Raster formats like PNG, JPEG, and WebP store images as grids of colored pixels — a mosaic that looks sharp at its native resolution but degrades the moment you scale it up. Scalable Vector Graphics, or SVG, takes an entirely different approach. Instead of describing pixels, SVG describes shapes. A circle is defined by a center point and a radius, a line by its start and end coordinates, a curve by a set of mathematical control points. Because these descriptions are resolution-independent, an SVG image looks perfectly crisp whether it is rendered on a 4K monitor, a mobile phone, or a billboard. This fundamental distinction makes SVG the format of choice for logos, icons, illustrations, data visualizations, and any graphic element that needs to look sharp at every size.
An XML-Based Markup Language
Unlike binary image formats, SVG is a plain-text markup language built on XML. An SVG file is something you can open in a text editor, read, and modify by hand. The root element is <svg>, and inside it you place elements that represent shapes, text, gradients, filters, and groups. Because SVG is XML, it follows strict syntax rules: every tag must be closed, attribute values must be quoted, and the document must be well-formed. This text-based nature has profound implications. It means SVGs can be versioned in Git, generated by server-side code, manipulated by JavaScript in the browser, and styled with CSS. It also means that SVGs can be compressed extremely well with gzip or brotli, often resulting in files far smaller than their raster equivalents for the same visual complexity.
A minimal SVG document might look like this: an <svg> element with a width, height, and xmlns attribute, containing a single <circle> element with cx, cy, and r attributes. That is enough for a browser to render a perfect circle at any resolution. Compare that to a PNG of a circle, which would need hundreds or thousands of anti-aliased pixels to approximate the same smoothness, and the elegance of the vector approach becomes clear.
Coordinate Systems and the viewBox
Every SVG operates within a coordinate system. By default, the origin sits at the top-left corner, with x increasing to the right and y increasing downward. The width and height attributes on the root <svg> element define the viewport — the visible area in the page layout. But the real power comes from the viewBox attribute, which defines the internal coordinate system. The viewBox takes four values: min-x, min-y, width, and height. By setting a viewBox of 0 0 100 100, you establish a 100-by-100 unit canvas regardless of the actual rendered size. This decoupling of the internal coordinate space from the display size is what makes SVGs truly responsive. You can set the rendered width to 50 pixels or 500 pixels, and the shapes inside will scale proportionally without any loss of quality.
The preserveAspectRatio attribute controls how the viewBox maps to the viewport when their aspect ratios differ. The default value, xMidYMid meet, centers the graphic and scales it to fit entirely within the viewport, similar to CSS object-fit: contain. Changing it to xMidYMid slice behaves like object-fit: cover, scaling the graphic to fill the viewport and clipping any overflow. Setting it to none stretches the graphic to fill the viewport without preserving proportions. Understanding these mechanics is essential for building responsive SVG layouts that behave predictably across screen sizes.
Basic Shapes and the Path Element
SVG provides a set of primitive shape elements that cover common geometric forms. The <rect> element draws rectangles and, with rx and ry attributes, rounded rectangles. The <circle> element draws circles, while <ellipse> handles ovals. The <line> element connects two points, and <polyline> and <polygon> connect multiple points into open or closed shapes. These primitives are convenient and readable, but the true workhorse of SVG is the <path> element.
The <path> element uses a compact command language in its d attribute to describe arbitrary shapes. The M command moves the pen to a starting point. The L command draws a straight line to a new point. The C command draws a cubic Bézier curve, defined by two control points and an endpoint, enabling the smooth, flowing curves you see in professional illustrations. The Q command draws a quadratic Bézier curve with a single control point. The A command draws an elliptical arc, which is useful for pie charts and rounded corners. The Z command closes the path by drawing a line back to the starting point. Lowercase versions of these commands use relative coordinates rather than absolute ones. Mastering the path data syntax unlocks the ability to create any shape imaginable, from simple icons to complex illustrations with thousands of curves.
Stroke, Fill, and Transforms
Every shape in SVG can be styled with two fundamental visual properties: fill and stroke. The fill property sets the interior color of a shape, while stroke sets the color of the outline. The stroke-width property controls the thickness of the outline, and additional properties like stroke-linecap, stroke-linejoin, and stroke-dasharray provide fine control over how lines are rendered. Setting fill to none creates an outline-only shape, which is the basis for most line-icon systems.
SVG also supports a powerful transform system. The transform attribute can apply translations, rotations, scaling, and skewing to any element or group. Transforms can be chained, so you might translate an element to a new position, rotate it around its center, and scale it, all in a single attribute. The <g> (group) element is particularly useful here: by wrapping related shapes in a group, you can apply a single transform to the entire collection. This hierarchical transform model is similar to how 2D game engines and animation tools work, making SVG a natural fit for interactive and animated graphics.
Inline SVG vs. External Files
There are two main ways to include SVGs on a web page. The first is as an external file, referenced via an <img> tag, a CSS background-image, or an <object> element. This approach is simple and cacheable, but it treats the SVG as an opaque image — you cannot style its internal elements with CSS or manipulate them with JavaScript. The second approach is inline SVG, where you paste the SVG markup directly into the HTML document. Inline SVGs become part of the DOM, which means you can target individual shapes with CSS selectors, attach event listeners, animate properties, and change attributes dynamically with JavaScript. The trade-off is that inline SVGs add to the HTML document size and cannot be cached separately.
For icon systems, a hybrid approach using SVG sprites has become popular. You define all your icons inside a single SVG with <symbol> elements, each identified by an id. Then you reference individual icons elsewhere in the page using <use href="#icon-id">. This gives you the DOM access benefits of inline SVG while keeping the markup tidy and allowing the browser to render the symbol definitions once and reuse them. This technique has largely replaced older icon font approaches, offering better accessibility, sharper rendering, and more flexible styling.
CSS Styling and Animation
Because inline SVGs are part of the DOM, they can be styled with CSS just like HTML elements. You can set fill, stroke, opacity, and even transform properties using CSS rules. This enables powerful theming: a single icon component can change color based on its parent's class, respond to hover states, or adapt to dark mode, all through CSS. Media queries work too, so an SVG illustration can simplify itself at small screen sizes by hiding or rearranging elements.
CSS animations and transitions work beautifully with SVG. You can animate the stroke-dashoffset property to create a line-drawing effect, transition fill colors on hover, or use keyframe animations to create pulsing, spinning, or morphing effects. For more complex animations, the SMIL animation elements (<animate>, <animateTransform>) are built directly into the SVG specification, though CSS animations have largely supplanted them in modern practice due to better tooling and broader developer familiarity. Libraries like GreenSock (GSAP) provide even more control, enabling timeline-based SVG animations with easing, staggering, and physics-based motion.
Accessibility
SVG accessibility is often overlooked, but it is straightforward to implement. The <title> element provides a human-readable name for the graphic, equivalent to alt text on an <img>. The <desc> element offers a longer description for complex graphics. Adding role="img" to the root <svg> element tells assistive technologies to treat it as a single image rather than traversing its internal structure. For decorative SVGs that convey no information, aria-hidden="true" removes them from the accessibility tree entirely. When SVGs are used as interactive controls, appropriate ARIA roles and labels ensure that keyboard and screen-reader users can interact with them effectively.
SVG Optimization
SVGs exported from design tools like Figma, Illustrator, or Inkscape often contain substantial bloat: editor metadata, unnecessary namespaces, default attribute values, excessive decimal precision in coordinates, empty groups, and redundant transforms. This extra data increases file size and can slow rendering. Optimization tools strip this cruft, simplify path data, merge compatible paths, and minify the markup. The results can be dramatic — it is common to see file size reductions of 30 to 60 percent after optimization, with no visible change in the rendered output.
The SVG Optimizer is designed for exactly this workflow. You can paste in raw SVG markup or upload an SVG file and receive a cleaned, minified version ready for production. For projects that involve converting vector graphics to pixel-based formats for specific use cases, the SVG to Raster tool renders SVGs to PNG or other raster formats at any resolution you specify. And when you need to go in the other direction — converting a photograph or raster graphic into a vector representation — the Image to SVG Tracer uses tracing algorithms to approximate raster imagery with SVG paths, which is useful for creating scalable versions of logos or simple illustrations that only exist in raster form.
When to Use SVG vs. Raster Formats
Choosing between SVG and raster formats is a decision that depends on the nature of the image. SVG excels for graphics that are geometric, have flat colors, and need to scale across different contexts: logos, icons, UI elements, diagrams, charts, and simple illustrations. Raster formats are better for photographs and complex imagery with continuous tones, gradients of millions of colors, and photographic detail. A photograph encoded as SVG paths would be enormously large and would not look as good as a well-compressed JPEG or WebP.
There is also a middle ground. Some illustrations combine flat vector areas with photographic textures or complex gradients. In these cases, you might use SVG for the structural elements and embed raster images within the SVG for the photographic parts, using the <image> element. Or you might export a complex illustration as a high-resolution PNG or WebP and use SVG only for the simpler graphics on your site. The key principle is to match the format to the content: vectors for shapes, pixels for photos.
Responsive SVG Patterns
Building truly responsive SVGs requires understanding the interplay between the viewport, viewBox, and CSS layout. The most common pattern is to omit the width and height attributes from the <svg> element and set only the viewBox. This makes the SVG intrinsically responsive — it will stretch to fill its container while maintaining the aspect ratio defined by the viewBox (assuming the default preserveAspectRatio). You can then control the rendered size entirely through CSS, setting width: 100% to make the SVG fill its parent container.
For more complex responsive behavior, you can use CSS media queries inside the SVG itself (when used inline or via <object>). This allows the SVG to adapt its internal layout based on the viewport size — hiding labels at small sizes, simplifying shapes, or rearranging elements. This technique is sometimes called "responsive SVG" and is particularly powerful for data visualizations and infographics that need to work across devices.
The combination of resolution independence, small file sizes, DOM integration, CSS styling, animation capabilities, and accessibility makes SVG an indispensable part of the modern web developer's toolkit. Whether you are building an icon system, creating an interactive data visualization, or simply ensuring your logo looks sharp on every screen, understanding SVG deeply will serve you well. And with tools like the SVG Optimizer, keeping your SVG assets lean and production-ready is a straightforward part of any build process.
Related Tools
SVG Optimizer
Optimize and minify SVG files by removing unnecessary metadata, comments, and whitespace while preserving visual quality.
SVG to PNG/JPG
Convert SVG vector graphics to high-quality raster images (PNG, JPG, WebP) with custom scaling and background options.
Image to SVG Tracer
Convert raster images (PNG, JPG) to scalable SVG vector graphics. Black & white or color tracing with adjustable threshold, smoothing, blur, and path options.
Related Articles
Try Our Free Tools
200+ browser-based tools for developers and creators. No uploads, complete privacy.
Explore All Tools