Sub topic 12
Export dimensions and 1x/2x scales
This chapter assumes you can use a design tool such as Figma, Sketch, or Photoshop. It also assumes you know what a pixel is, what a frame or artboard is, and that a web page loads images through HTML. Formats and compression settings are covered in other chapters. Here the question is how many pixels each exported file should have.
Two kinds of pixels
A CSS pixel is the unit a developer uses in layout. When a developer writes width: 400px, the image occupies 400 CSS pixels of the page. A device pixel is a physical dot on the screen.
On a standard-density screen, one CSS pixel equals one device pixel. On a high-density screen, such as most phones and many laptops, one CSS pixel is drawn with several device pixels. The ratio is the device pixel ratio (DPR). A DPR of 2 means a 400 CSS pixel wide box covers 800 device pixels.
| Screen | Typical DPR | Device pixels for a 400 CSS pixel box |
|---|---|---|
| Older desktop monitor | 1 | 400 |
| Most laptops with a high-density display, many phones | 2 | 800 |
| Some premium phones | 3 | 1200 |
If you export a 400 pixel wide file for a 400 CSS pixel slot, a DPR 2 screen has to stretch it to 800 device pixels. The browser fills the missing detail by guessing, and the result looks soft.
Start from the displayed size
The right export width comes from the size at which the image is displayed, not from the size of the source file. Work in this order:
- Find the displayed width in CSS pixels. Read it from the layout in your design file, or ask the developer. For a fluid layout, use the largest width the image reaches.
- Multiply by the scale you need. The 1x file uses the displayed width. The 2x file uses double.
- Export from the original source, not from a copy you have already shrunk.
For example, a card thumbnail is displayed at 320 by 180 CSS pixels. The 1x file is 320 by 180. The 2x file is 640 by 360.
Exporting scales in a design tool
Most design tools can export several scales at once. In Figma, select the layer or frame, open the Export section in the right panel, and add one row per scale. Set the first row to 1x and the second to 2x. Figma then appends a suffix, so @2x becomes part of the second file name. Sketch and Adobe tools have equivalent settings.
Check what the scale applies to. A scale multiplies the size of the selected layer. If the frame is already 640 pixels wide because you designed at 2x, then a 1x export gives you 640 pixels, and a 2x export gives you 1280. Know which scale your file was designed at before you export.
Telling the browser which file to use
Exporting two files does nothing until the HTML points to both. The srcset attribute lists the candidate files, and the browser picks one based on the screen's density. The width and height attributes give the displayed size in CSS pixels.
<img
src="logo.png"
srcset="logo.png 1x, [email protected] 2x"
width="120"
height="40"
alt="Acme logo">Here logo.png is 120 by 40 pixels and [email protected] is 240 by 80 pixels. The width and height attributes stay at 120 and 40 in both cases. They describe the layout box, not the file. They also let the browser reserve space before the image loads, which stops the page from jumping.
Notice that the src attribute still names the 1x file. Browsers that do not understand srcset use it as a fallback.
Do you need 3x?
Phones with a DPR of 3 exist, but the gain over 2x is small for most images. A 3x file has 2.25 times the pixels of a 2x file, and the file size grows roughly in step. Photographs and illustrations rarely show a visible difference. Thin lines and small text inside an image are the exception, but those are better served by vector formats.
A practical default is to export 1x and 2x, and to skip 3x unless the developer or a test on a real device shows a clear need.
Edge cases
Never export larger than the source
If the source image is 1000 pixels wide, a 2x export for a 600 pixel slot needs 1200 pixels. You cannot create that detail by scaling up. The result is a larger file with no extra sharpness. Either export at 1000 pixels and accept a lower effective density, or ask for a larger source.
Odd sizes and rounding
When a displayed width is an odd number such as 333 pixels, the 2x file is 666 pixels, which is fine. Fractional sizes are the problem. A layer 333.5 pixels wide exports at 667 pixels at 2x, and the 1x file is rounded, so the two files no longer have the same proportions. Snap layers to whole pixels in the design file before you export.
Fluid and full-width images
A hero image that fills the viewport has no fixed displayed width. A 1x or 2x pair is not enough, because the image may be 360 pixels wide on a phone and 1440 on a desktop. Here you export several widths, such as 400, 800, and 1600, and describe them with srcset and sizes. The responsive images chapter covers this. For now, remember the rule of thumb: the largest file should be about twice the largest displayed width.
Images that do not need scales
Vector images (SVG) have no pixel size to match, so they need no 2x version. Export them once. This is one reason icons and logos are best kept as SVG.
Checking your numbers
Before you hand off, check each file against this list:
- The 1x file width equals the displayed width in CSS pixels.
- The 2x file width equals double the displayed width.
- Both files have the same aspect ratio.
- No file is wider than the source.
- File names make the scale clear.
Exercise
A product card shows its image at 280 by 210 CSS pixels. You designed the card at 1x. What pixel dimensions should the 1x and 2x exports have, and what would you write in the srcset attribute if the files are named card.jpg and [email protected]?
Show answer
The 1x file is 280 by 210 pixels and the 2x file is 560 by 420 pixels. The attribute is srcset="card.jpg 1x, [email protected] 2x". The width and height attributes should stay at 280 and 210.
Exercise
Your source photo is 900 pixels wide and the layout shows it at 600 CSS pixels wide. A teammate asks for a 2x export. What do you do?
Show answer
A 2x file would need 1200 pixels, which is more than the source has. Scaling up adds file size but no detail. Export at 900 pixels, which gives an effective density of 1.5x, or ask for a larger source image if full 2x sharpness matters.