Sub topic 6
SVG: icons, logos, and illustrations
SVG (Scalable Vector Graphics) is an image format that describes shapes with text instead of pixels. This chapter assumes you know the difference between raster and vector images from the earlier chapter. It also assumes you can export from a design tool such as Figma, Sketch, or Illustrator, and that you know basic HTML.
What an SVG file contains
An SVG file is an XML text file. It lists shapes (paths, circles, rectangles, text) and their styling. The browser draws those shapes at whatever size it needs. Because the file stores instructions and not a grid of pixels, a 24-pixel icon and a 2400-pixel icon come from the same file, and both stay sharp.
This has three practical effects:
- You do not need 1x and 2x versions. One file covers every screen density.
- File size depends on how complex the shapes are, not on how large the image is displayed.
- You can style and animate the image with CSS and JavaScript, because the shapes are part of the page's structure when the SVG is placed inline.
Here is a complete icon:
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-label="Add">
<path d="M12 5v14M5 12h14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
</svg>The path draws a vertical line and a horizontal line, which together make a plus sign. Setting stroke="currentColor" makes the lines take the text color of the surrounding element. If a button's text turns white on hover, the icon turns white too, with no extra file.
The viewBox and sizing
The viewBox attribute defines the SVG's own coordinate system. In viewBox="0 0 24 24", the four numbers are the left edge, top edge, width, and height of the drawing area. Your shapes are positioned inside that box. The width and height attributes then set how large the box is displayed.
If viewBox is missing, the SVG will not scale with its container. This is the most common reason an exported SVG appears cropped or stays at a fixed size. Most design tools include it by default, but some export presets or optimizers remove it.
| Attribute | What it controls | Example |
|---|---|---|
| `viewBox` | The internal coordinate system and aspect ratio | `0 0 24 24` |
| `width`, `height` | The displayed size in CSS pixels | `24` and `24` |
| `xmlns` | The XML namespace, required when the SVG is a standalone file | `http://www.w3.org/2000/svg` |
You can change the displayed size in CSS, for example width: 48px. As long as viewBox is present and the aspect ratio is kept, the drawing scales cleanly.
Where SVG is the right choice
SVG fits images built from clean shapes and flat color:
- Icons and interface symbols
- Logos and wordmarks
- Simple illustrations, diagrams, and charts
- Decorative dividers and background shapes
SVG is a poor fit for photographs. A photo has millions of subtly different colors, and describing it as shapes produces a huge file that looks worse than a JPEG. If an illustration has heavy texture, gradients with noise, or many thousands of tiny shapes, test it against a PNG or WebP of the same size. The smaller file wins.
Preparing a design for clean SVG export
Good SVG output depends on how you build the design. Check these points before you export.
- Convert text to outlines if the exact letterforms matter. SVG text uses fonts installed on the viewer's device. If the font is missing, the browser substitutes another one. Logos should use outlined text. Body text in an illustration can stay live if you want it searchable and selectable, but then you must make sure the font is loaded by the page.
- Expand or flatten what the format cannot express. Some effects, such as layer blend modes, certain blurs, and complex shadows, either export as embedded raster images or are dropped. Check the result.
- Keep shapes simple. Merge overlapping shapes where it does not change the look, and remove hidden layers. Every extra path adds bytes.
- Use whole-pixel coordinates for small icons. An icon placed on a half-pixel position looks blurry at small sizes because the edges fall between pixels.
- Name the frame tightly. The export area becomes the viewBox. Extra padding in the frame becomes extra padding in the file.
Optimizing SVG files
Design tools add editor metadata, comments, unused definitions, and long decimal numbers. An optimizer removes these without changing the drawing. SVGO is the most widely used tool, and its web interface, SVGOMG, lets you toggle each cleanup and compare before and after.
Typical savings are 30 to 70 percent of the file size on icons exported straight from a design tool. Check two settings, because optimizers can break files when they are too aggressive:
- Keep the
viewBox. Some presets remove it to save bytes. Turn that option off. - Keep IDs you rely on. If a developer targets an element by ID for animation, do not let the optimizer rename or remove it. Also watch for ID collisions: two inline SVGs that both define a gradient with
id="a"can overwrite each other on the same page.
Serving the file with gzip or Brotli compression, which most web servers do for text files, shrinks SVG further.
Ways to place an SVG on a page
The method you choose decides what you can do with the image.
| Method | CSS can style inside it | Cached separately | Notes |
|---|---|---|---|
| `<img src="logo.svg">` | No | Yes | Simplest. Treated like any other image. |
| CSS `background-image` | No | Yes | Good for decoration. No alt text. |
| Inline `<svg>` in the HTML | Yes | No | Supports `currentColor` and animation. Adds to the HTML size. |
| `<object>` or `<iframe>` | Limited | Yes | Rarely needed. |
Use <img> for logos and illustrations that do not change color. Use inline SVG for icons that must follow text color or react to hover. An SVG loaded through <img> ignores the page's CSS and runs no scripts, which is also a safety benefit.
Accessibility
Decide whether each SVG carries meaning.
- A meaningful image, such as a logo that is the only link text, needs a text alternative. For an
<img>, use thealtattribute. For inline SVG, addrole="img"and anaria-label, as in the example above. - A decorative icon next to a text label should be hidden from screen readers with
aria-hidden="true", so the label is not read twice.
Security with untrusted files
An SVG can contain scripts and links to external resources. Files you export yourself are safe. If your site accepts SVG uploads from users, sanitize them first and serve them through <img>, not inline. As a designer, treat SVG files from unknown sources as code, not as pictures.
Decision flow
Common errors
- The icon is cropped or does not scale. The
viewBoxis missing or does not match the artwork bounds. - The logo shows the wrong font. Text was not converted to outlines.
- The file is far larger than expected. It contains an embedded bitmap or thousands of tiny paths.
- The icon will not change color. It is loaded with
<img>, or the paths have hard-coded fill colors instead ofcurrentColor. - Gradients look wrong when two icons share a page. Duplicate IDs collide. Make IDs unique or let the optimizer prefix them.
Exercise
A developer reports that your exported settings icon stays 300 pixels wide in a 24-pixel button and that it will not turn white when the button is hovered. The icon is placed with an img element. What two problems should you check in the file, and what change to the placement fixes the color issue?
Show answer
First, check whether the SVG has a viewBox attribute and whether fixed width and height values are set to 300. Without a viewBox, or with large fixed dimensions, the icon will not scale to its container, so add viewBox="0 0 24 24" (matching the artwork) and set width and height to 24. Second, check whether the paths use hard-coded fill or stroke colors instead of currentColor. The color issue is also caused by the img element, because an SVG loaded this way cannot inherit the page's text color. Place the SVG inline in the HTML and use currentColor so it follows the button's text color on hover.
Exercise
You have a photograph with a logo overlaid in the corner. Which parts, if any, should be SVG, and how would you deliver them?
Show answer
Export the photograph as a raster format such as JPEG, WebP, or AVIF, because a photo is not suited to vector shapes. Export the logo separately as SVG, with text outlined, and place it over the photo using HTML and CSS. This keeps the logo sharp at every size and keeps the photo file small.