You have a picture for a web page and no idea how many pixels wide it should be. Start from the size at which the picture appears on the page, measured in CSS pixels, and multiply by the pixel density of the screens you want to serve. A file with far more pixels than the page shows only adds bytes that every visitor has to download.
This guide separates the sizes that get confused, gives a formula and a width table, puts numbers on the cost of oversized pictures and shows the HTML that lets the browser pick the right file.
Three sizes that get mixed up
| Term | Meaning | Where it is set |
|---|---|---|
| Intrinsic (pixel) dimensions | The real size of the image file, in pixels | In the file itself |
| Display size | The size the picture occupies on the page, in CSS pixels | By page layout and CSS |
| Device pixels | The physical pixels of the screen | By the screen |
The format reference calls an image's intrinsic size its real size. A CSS rule such as width: 320px sets the display size, and the same guide notes these units are also called CSS pixels. In CSS, 1 inch equals 96 CSS pixels, a fixed rule defined in the CSS specification.
Screens differ in how many device pixels sit inside one CSS pixel. The responsive-images guide gives the case: on a standard display one device pixel represents each CSS pixel, and on a display with two device pixels per CSS pixel a larger file can be chosen. Even though the image is displayed at the same size, the higher-density screen shows more detail.
The rule: display width times screen density
needed width in pixels = display width in CSS pixels × device pixels per CSS pixel
needed height = needed width × original height ÷ original width
The second line keeps the shape. The aspect ratio calculator does that step, and aspect ratios explained shows the method.
Example: picture slots with a 16:9 shape.
| Display width (CSS px) | Display height | Needed at 1x | Needed at 2x | Needed at 3x |
|---|---|---|---|---|
| 480 | 270 | 480 × 270 | 960 × 540 | 1440 × 810 |
| 800 | 450 | 800 × 450 | 1600 × 900 | 2400 × 1350 |
| 1200 | 675 | 1200 × 675 | 2400 × 1350 | 3600 × 2025 |
Each step up in density multiplies both sides, so 2x means four times the pixels. For 800 × 450 that is 360,000 pixels at 1x and 1,440,000 at 2x.
What an oversized picture costs
The beginner's guide to HTML images warns that a size set too big wastes bandwidth on an image that does not fit the user's needs. It advises setting the image to the correct size before the picture goes on the page, and using CSS only if you must alter the displayed size afterward.
Worked example. Example: a 4000 × 3000 original (4:3) placed in a slot 600 CSS pixels wide.
- Display height: 600 × 3 ÷ 4 = 450, so the slot is 600 × 450.
- Pixels in the slot at 1x: 600 × 450 = 270,000. Pixels in the original: 4,000 × 3,000 = 12,000,000.
- The original has 12,000,000 ÷ 270,000 = 44.4 times as many pixels as the slot can show.
- Raw size at 24 bits per pixel: 12,000,000 × 3 = 36,000,000 bytes against 270,000 × 3 = 810,000 bytes.
- For a 2x screen the file needs 1,200 × 900 = 1,080,000 pixels, still 11.1 times fewer than the original.
The image file size calculator gives these raw figures. A saved JPEG or PNG will not shrink by exactly the same factor, because compression depends on the picture, as the JPEG standard notes. Fewer pixels still means less data to store, and the dimensions are the first setting to fix. Page-level effects are covered in web page weight.
What the extra bytes cost in seconds
Transfer time follows seconds = bytes × 8 ÷ (megabits per second × 1,000,000). Real speeds vary and include protocol overhead, so treat the results as best cases. The download time calculator accepts an overhead percentage.
Example: two versions of one picture at an assumed 20 Mbps, using sizes picked for the exercise and not measured:
| Version | File size | One image at 20 Mbps | Ten images at 20 Mbps | One image at 5 Mbps |
|---|---|---|---|---|
| Oversized | 2,000,000 bytes | 0.8 s | 8 s | 3.2 s |
| Right-sized | 100,000 bytes | 0.04 s | 0.4 s | 0.16 s |
Check one cell: 2,000,000 × 8 ÷ 20,000,000 = 0.8 seconds. The oversized file is 20 times larger, so in this calculation it takes 20 times longer on any connection. How to calculate download time covers overhead in detail.
Letting the browser choose: srcset, sizes, width and height
Resizing files by hand does not help if one file must serve every screen. HTML lets you list several versions. With srcset and sizes, the browser picks a file based on the slot size and the screen, and the responsive-images guide says the user agent chooses among the sources at its discretion.
html
<img src="photo-800.jpg"
srcset="photo-480.jpg 480w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(width <= 600px) 480px, 800px"
width="800" height="450" alt="Short description" loading="lazy">
The w number in srcset must match the pixel width of that file. The width and height attributes let the browser calculate the aspect ratio before the file loads and reserve the space, which reduces layout shift. The loading="lazy" value defers pictures that are not near the visible area.
After the dimensions are right, the format is the next saving: see JPEG vs. PNG and WebP and AVIF.
Common mistakes
- Shrinking an original with CSS only. The browser still downloads every pixel. Resize the file itself and keep CSS for small adjustments.
- Forgetting dense screens. A file sized for 1x looks softer on a 2x screen. Offer a larger version and let the browser choose.
- Forcing a different shape. Setting width and height to a ratio that differs from the file distorts the picture. Calculate one side from the other.
- Leaving out width and height attributes. The page then jumps when the picture loads. The attributes reserve the space in advance.
- Mixing
wandxdescriptors in onesrcset. The element reference calls that incorrect. Use one kind per list. - Searching for one universal size. The right dimensions depend on the slot and the density, so calculate them for each layout.
FAQ
What image size should I use for a website?
Use the size of the slot in CSS pixels, multiplied by the screen density you want to support. A 600-pixel slot needs 600 pixels across at 1x and 1,200 at 2x.
Do I need pictures for 2x screens?
Only if you want the added detail they show. A 2x version costs four times the pixels, so offer it through srcset rather than to every visitor.
Does PPI matter for web images?
Pixel dimensions are what a page uses. The density tag is covered in PPI vs. DPI.
Is a smaller file always better?
Not if it looks wrong at the display size. Aim for the smallest pixel dimensions that still fill the slot at the density you serve.
Sources
- Mozilla (MDN Web Docs), Using responsive images in HTML. Accessed November 6, 2026.
- Mozilla (MDN Web Docs), <img> HTML image embed element. Accessed November 6, 2026.
- Mozilla (MDN Web Docs), HTML images. Accessed November 6, 2026.
- W3C, CSS Values and Units Module Level 4. Accessed November 6, 2026.
- ITU-T, Information technology - Digital compression and coding of continuous-tone still images - Requirements and guidelines (Recommendation T.81). Accessed November 6, 2026.



