Sub topic 1
Pixels, Resolution, and Screen Density
Prerequisites
This chapter assumes you can do the following:
- Use a design tool such as Figma, Sketch, Photoshop, or Illustrator.
- Work with layers, artboards, and frames.
- Recognize the basic parts of a web page: HTML for structure, CSS for styling, and images that the page loads.
You do not need to write code. The few HTML snippets here are short, and each one is explained.
Why this comes first
Every later decision in this tutorial depends on one question: how many pixels should this file contain? If you export too few, the image looks blurry. If you export too many, the page loads slowly and the extra detail is never seen. This chapter gives you the working model for answering that question.
What a pixel is
A pixel is the smallest single dot of color in a raster image, which is an image built from a grid of dots. An image that is 800 pixels wide and 450 pixels tall holds 800 × 450 = 360,000 pixels. Each pixel stores a color value, so more pixels means more data and usually a larger file.
The word "pixel" is used for two different things, and mixing them up causes most export mistakes:
- Image pixels (also called device pixels or physical pixels) are the dots stored in the file and the tiny lights on a physical screen.
- CSS pixels are the units that web layouts use. A CSS pixel is a logical unit. It is not always one physical dot.
When a developer writes width: 400px in CSS, they mean 400 CSS pixels. How many physical dots fill that space depends on the screen.
Resolution: two meanings
"Resolution" is also used in two ways.
- Pixel dimensions. The width and height of the image, such as 1600 × 900. This is what matters for web export.
- Pixels per inch (PPI). A number saved in the file that says how large to print the image. A file marked 72 PPI and the same file marked 300 PPI contain exactly the same pixels.
For the web, PPI does nothing. A browser ignores it and uses only the pixel dimensions. You do not need to set 72 PPI for web images. The pixel dimensions are what count.
Screen density and the device pixel ratio
Screen density describes how many physical pixels a screen packs into the same physical space. Phones, tablets, and many laptops use high-density screens, often marketed as "Retina" or "HiDPI".
The device pixel ratio (DPR) is the number of physical pixels used for each CSS pixel along one side. It tells you how much detail a screen can show.
| Screen type | Typical DPR | Physical pixels for a 100 × 100 CSS pixel box |
|---|---|---|
| Standard desktop monitor | 1 | 100 × 100 = 10,000 |
| Most modern phones and laptops | 2 | 200 × 200 = 40,000 |
| Some high-end phones | 3 | 300 × 300 = 90,000 |
Notice that the pixel count grows with the square of the ratio. A 2x screen needs four times as many pixels in the same space, not twice as many. A 3x screen needs nine times as many.
A worked example
Your design shows a logo at 120 × 40 in a frame. The frame uses CSS pixels, so the logo will be displayed in a box 120 CSS pixels wide and 40 tall.
| Export scale | File dimensions | Sharp on |
|---|---|---|
| 1x | 120 × 40 | DPR 1 screens only. Looks soft on DPR 2 and 3. |
| 2x | 240 × 80 | DPR 1 and DPR 2. Slightly soft on DPR 3. |
| 3x | 360 × 120 | All three, but wasteful for DPR 1 and 2. |
On a DPR 2 screen, the 120 × 40 box is made of 240 × 80 physical pixels. A 1x file has only 120 × 40 pixels of data, so the browser must stretch it to fill the box. It invents the missing pixels by blending neighbors, and the result looks blurry.
Here is how you tell the browser about both files:
<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. The srcset attribute lists the candidates. The browser picks logo.png on a 1x screen and the double-size [email protected] on a 2x screen.
Matching your design tool to the web
Most design tools work in CSS pixels already. A Figma frame that is 1440 wide represents 1440 CSS pixels. When you export, the tool's scale setting multiplies the dimensions:
- Export at 1x: file dimensions equal the layer size.
- Export at 2x: file dimensions are doubled.
- Export at 3x: file dimensions are tripled.
Photoshop and Illustrator behave differently. They work in image pixels, and the document's PPI setting can change how sizes are reported when you use inches or points. For web work, set the document units to pixels and ignore PPI.
Common errors
- Exporting a huge original. A 4000 × 3000 photo displayed at 400 CSS pixels wide is far larger than any screen needs. Even at 2x, 800 pixels wide is enough. The extra pixels cost download time and add nothing visible.
- Upscaling a small file. Enlarging a 200-pixel image to 400 pixels does not add detail. The tool only guesses. Always export from the largest source, not from an already shrunk copy.
- Scaling a raster layer after placing it. If you shrink an image layer in your design and then export it at 1x, the file may be fine. If you enlarge it past 100%, the export will be soft. Check the layer's scale before export.
- Odd dimensions at 2x. A layer 121 pixels wide exports at 242 pixels at 2x, which is fine. But at 1.5x it becomes 181.5 pixels, and the tool must round. Round layer sizes to whole numbers before export so edges stay crisp.
- Changing only the width. If you resize a file by hand, lock the aspect ratio. A stretched image looks wrong, and the width and height attributes in the HTML will no longer match the file.
How much detail is enough
A practical rule: export at two times the displayed CSS size for most images. Three times is rarely worth the cost, because the eye can seldom tell the difference between 2x and 3x for photographs, and the file grows by more than twice. Logos, text in images, and thin lines are exceptions. Sharp edges reveal low resolution more easily than photos do.
If an image is vector (an image described by shapes and not pixels), this problem disappears, because it redraws cleanly at any density. The next chapter covers that difference.
Key points
- Browsers lay out pages in CSS pixels, while screens draw with physical pixels.
- DPR is the number of physical pixels per CSS pixel on one side.
- PPI metadata does not affect how a web image displays. Pixel dimensions do.
- A 2x export has four times the pixels of a 1x export.
- Export at about 2x the displayed size, and never upscale a small file.
Exercise
A hero image is displayed in a box 600 CSS pixels wide and 300 CSS pixels tall. What pixel dimensions should the 2x file have, how many pixels does it contain compared to the 1x file, and what should the HTML width and height attributes say?
Show answer
The 2x file should be 1200 × 600 pixels. It has 1200 × 600 = 720,000 pixels, which is four times the 180,000 pixels (600 × 300) of the 1x file. The width and height attributes stay at the displayed size in CSS pixels: width="600" height="300".
Exercise
A colleague exports a photo at 72 PPI and says that is why it will load quickly. Another sets 300 PPI and says the photo will look sharper on the web. Are they right? Explain using what the browser reads from the file.
Show answer
Neither is right. The browser ignores the PPI value and uses only the pixel dimensions. Two files with the same pixel dimensions display identically in a web page whatever their PPI, and their file sizes are nearly the same. To change loading speed or sharpness, change the pixel dimensions or the compression, not the PPI.