Sub topic 2
Raster versus vector images
This chapter assumes you can work in a design tool such as Figma, Sketch, Photoshop, or Illustrator, and that you know what pixels and screen density are from the previous chapter. It covers the two ways an image can store a picture and what that means for the files you export.
Two ways to store a picture
A raster image is a grid of pixels. Each pixel holds one color. The file records the color of every cell in the grid. Photographs, screenshots, and painted textures are raster images.
A vector image is a set of instructions. The file records shapes, such as "draw a line from here to there" or "fill this curve with this color". The browser draws those shapes when the page loads. Icons, logos, and flat illustrations are often vector images.
What changes when you scale
A raster image has a fixed number of pixels. If you show a 400-pixel-wide image at 800 pixels, the browser has to invent the missing pixels. The result looks soft or blurry. If you show it smaller, the extra pixels are wasted bytes.
A vector image has no fixed size. The browser recalculates the shapes at whatever size and screen density it needs. The edges stay sharp on a standard screen and on a 2x screen, and you export one file.
| Property | Raster | Vector |
|---|---|---|
| Stores | Pixel colors | Shapes and paths |
| Scaling up | Gets blurry | Stays sharp |
| File size | Grows with pixel count | Grows with shape complexity |
| Best for | Photos, textures, complex images | Icons, logos, simple illustrations |
| Common web formats | JPEG, PNG, GIF, WebP, AVIF | SVG |
Where the file size comes from
A raster file grows with its pixel dimensions. A photo that is 2000 by 1000 pixels holds 2 million pixels, and compression only reduces that cost.
A vector file grows with the number of shapes and points. A 24-pixel icon with one path is tiny. A detailed illustration with thousands of paths, gradients, and effects can be larger than a compressed raster version of the same picture.
Common mistakes
- Exporting a logo as PNG at one size. It will blur on high-density screens unless you also export a 2x file. An SVG avoids this.
- Exporting a photo as SVG. A photo has no simple shapes. Tracing it into vectors produces a huge file that looks worse than a JPEG.
- Embedding a raster image inside an SVG. Some design tools export an SVG that just wraps a PNG. You get the weight of a raster file with none of the scaling benefit. Open the file in a text editor and check for a long
data:image/png;base64string. - Leaving live effects in vector artwork. Blurs, drop shadows, and some filters may be rasterized on export. Check the result.
A rule of thumb
Ask what the image is made of. If it is made of shapes with clear edges and flat color, start with vector. If it is made of continuous tones and fine detail, such as a photo, use raster. The later chapters on formats build on this choice.
Exercise
A designer exports a company logo as a 200-pixel-wide PNG. On a 2x phone screen the logo looks soft. Give two ways to fix this, and say which one you would prefer.
Show answer
One fix is to export a 400-pixel-wide PNG and serve it to 2x screens, for example with srcset. The other is to export the logo as SVG. SVG is usually better because a logo is made of shapes, one file stays sharp at every size and density, and it is often smaller than the PNG files.