Sub topic 2
Raster versus vector images
This chapter assumes you can use a design tool such as Figma, Sketch, Photoshop, or Illustrator. It also assumes you know what layers, artboards, and frames are, and that you know a web page is built from HTML, CSS, and image files. You should already know that a pixel is one colored dot on a screen and that screens differ in density. That was covered in the previous chapter.
Two ways to store a picture
Every image file stores a picture in one of two ways.
- A raster image is a grid of pixels. The file records the color of every pixel in the grid.
- A vector image is a set of drawing instructions. The file records shapes, such as "a line from here to there" or "a circle of this radius with this fill color". The viewer draws those shapes at whatever size it needs.
This difference decides how an image behaves when you scale it, how big the file is, and which formats can hold it.
How raster images behave
A raster image has a fixed number of pixels. A photo that is 800 pixels wide holds 800 columns of color, and that is all the detail it has.
If you display it smaller, the browser discards detail. The result usually looks fine. If you display it larger, the browser has to invent pixels between the ones it has. It blends neighbors together, and the result looks soft or blocky. No export setting can add detail that the file does not contain.
File size grows with pixel count. An image of 1600 by 900 pixels has four times as many pixels as one of 800 by 450. Compression changes the final size, but the pixel count still sets the starting point.
Raster is the right model when the content has continuous variation: photographs, textured backgrounds, painted artwork, and screenshots. Describing a photo as shapes would take millions of tiny shapes, so a grid is simpler and smaller.
The raster formats you will meet in this tutorial are JPEG, PNG, GIF, WebP, and AVIF.
How vector images behave
A vector image has no built-in pixel size. It has a coordinate system and a list of shapes in it. The browser turns the shapes into pixels at the moment it draws them. On a 1x screen it draws one set of pixels, and on a 3x screen it draws more. Edges stay crisp in both cases, because the browser computes them for the screen at hand.
File size depends on how many shapes there are and how complex they are. A simple icon may be a few hundred bytes. A detailed illustration with thousands of paths can be larger than a compressed raster version of the same picture.
Vector is the right model for logos, icons, line art, diagrams, and flat illustrations made of clean shapes and solid or gradient fills.
On the web, the vector format is SVG, which stands for Scalable Vector Graphics. SVG files are text, so you can open one in a code editor, and CSS can style them. The tutorial's SVG example uses currentColor so the icon takes its color from the surrounding text:
<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 viewBox defines the internal coordinate system, here 24 by 24 units. The width and height set the displayed size. Change those two values and the same shapes are drawn larger, with no loss of sharpness.
Comparing the two
| Property | Raster | Vector (SVG) |
|---|---|---|
| Stored as | Grid of pixel colors | Shapes and paths |
| Enlarging | Loses sharpness | Stays sharp |
| File size depends on | Pixel count and compression | Number and complexity of shapes |
| Best content | Photos, textures, screenshots | Logos, icons, line art, diagrams |
| Formats | JPEG, PNG, GIF, WebP, AVIF | SVG |
| Needs 2x versions | Yes, for sharp high-density screens | No |
| Can be styled with CSS | No | Yes, for inline SVG |
Common errors
Treating "vector" as a property of the design tool. Work drawn in a vector tool is not automatically exported as a vector. If you export a Figma frame as PNG, you get a raster image, however the layers were drawn. The format you choose in the export panel decides what you get.
Exporting a photo as SVG. Some tools let you wrap a photo in an SVG file. The file contains an embedded raster image and is usually larger than a plain JPEG. It gains none of the benefits of vector.
Exporting a complex illustration as SVG without checking size. Traced artwork and illustrations with many gradients, blurs, and thousands of points can produce very large SVG files. Compare the SVG size to a WebP or PNG version before you commit to it.
Upscaling a raster logo. A small PNG logo stretched to fit a large header looks blurry. If a vector source exists, export SVG. If only a raster exists, ask for the vector original rather than enlarging the pixels.
Relying on live text and effects. Fonts that are not available on the user's machine can change how an SVG with text renders. Convert text to outlines in the design tool, or use real HTML text instead of putting words in the image.
Forgetting size attributes. An SVG with no width, height, or viewBox may fill its container or collapse to a default size. Always keep a viewBox and state a size.
Decision rules
Use these questions in order for each image:
- Is it made of shapes and flat or gradient fills, such as a logo, icon, or simple illustration? Prefer vector.
- Is it a photograph or has continuous tonal variation? Use raster.
- Is it a screenshot or a UI mockup with fine text? Use raster, because the exact pixels matter.
- Is a vector version heavier than a raster of the same picture? Use raster.
Later chapters pick the exact raster format. For now, the first decision is only raster or vector.
Exercise
Your design file contains a company logo drawn with vector shapes and a hero photograph. A teammate exports both as PNG at 1x. On a high-density phone, the logo looks soft. Explain why, and say what you would change for each image.
Show answer
PNG is a raster format, so the logo was converted to a fixed grid of pixels at 1x. A high-density screen shows more physical pixels than the file has, so the browser stretches the grid and the edges look soft. Export the logo as SVG, which is drawn at the screen's own density and needs no 2x file. The photograph should stay raster. Export it at the displayed size and also at 2x so it stays sharp on dense screens, and choose its format later based on its content.
Exercise
An illustration exported as SVG is 640 KB. A WebP export of the same picture at the displayed size is 90 KB. Which would you use, and why?
Show answer
Use the WebP, assuming it looks acceptable. The SVG is large because it has a very high number of shapes or effects, so vector gives no size advantage here. File size and loading speed matter on the web. Check the WebP at 2x for sharpness, and consider simplifying the vector artwork only if you specifically need infinite scaling or CSS styling.