Sub topic 1
Pixels, resolution, and screen density
Prerequisites
This chapter assumes you can use a design tool such as Figma, Sketch, Photoshop, or Illustrator. You should know what layers, artboards, and frames are. You should also know that a web page is built from HTML, CSS, and images.
Two kinds of pixel
A raster image is a grid of pixels, and each pixel holds one color. An image that is 800 × 450 has 800 columns and 450 rows, which is 360,000 pixels. Both sharpness and file size depend on that grid.
On the web, "pixel" means two different things:
- A device pixel is one physical dot on a screen. It is also one dot in an image file.
- A CSS pixel is the unit that page layout uses. A box that is
400pxwide in CSS is 400 CSS pixels wide, however sharp the screen is.
Screen density
Screen density is the number of device pixels that fit in one CSS pixel along one side. It is also called the device pixel ratio (DPR). A basic monitor has a DPR of 1. Many phones and laptops have a DPR of 2 or 3.
On a DPR 2 screen, a box 400 CSS pixels wide spans 800 device pixels. If the image file is only 400 pixels wide, the browser stretches each pixel over four dots (2 × 2), and the image looks soft. To stay sharp, the file needs to be 800 pixels wide.
| Displayed width | DPR 1 file width | DPR 2 file width | DPR 3 file width |
|---|---|---|---|
| 120 CSS px | 120 px | 240 px | 360 px |
| 400 CSS px | 400 px | 800 px | 1200 px |
Resolution is not dpi
A common mistake is exporting at "72 dpi for the web". The dpi or ppi value stored in a file is a hint for printing. Browsers ignore it. An 800 × 450 file displays the same whether it is labeled 72 or 300 ppi. Only the pixel dimensions matter.
Worked example
Suppose the design shows a logo at 120 × 40 in a Figma frame. Frame units in Figma are CSS pixels, so export two files:
logo.pngat 120 × 40 for DPR 1 screens.[email protected]at 240 × 80 for DPR 2 screens.
The developer then lists both files, and the browser picks one based on the screen:
<img
src="logo.png"
srcset="logo.png 1x, [email protected] 2x"
width="120"
height="40"
alt="Acme logo">The width and height attributes give the displayed size in CSS pixels. They stay at 120 and 40 for both files.
Common errors
- Exporting the file at its display size only, so it looks blurry on high-density screens.
- Exporting at 3x or larger for everything, which wastes bytes with no visible gain on most screens.
- Upscaling a small image to reach a target size. The new pixels are guesses, so the image does not gain detail.
Exercise
A card image is displayed 300 CSS pixels wide. What pixel widths should you export for 1x and 2x screens, and why does the dpi label in the file not matter?
Show answer
Export 300 px wide for 1x and 600 px wide for 2x, because a DPR 2 screen has two device pixels per CSS pixel. The dpi label is print metadata that browsers ignore, so only the pixel dimensions affect how the image displays.