O good ol' days where @Quentin's answer was the single truth!
One thing first: @Quentin's answer is still valid for basically 99.999999% of all image requests. But:
Since Chrome 46 (and soonishly in Firefox and Edge) there is something called Client Hints. It works like this:
- The browser encounters an image element with
src
pointing to /image.jpg
.
- It determines, at which width and resolution ("
DPR
") the image should be displayed.
It adds these values as additional HTTP headers:
GET /image.jpg HTTP/1.1
DPR: 1.0
Width: 100
The server is now free to react to these hints, e.g. send a pre-processed image over the wire, that is just the right size as requested by the browser.
This means, in the context of your answer, that you could have a 200x200
image on your server, and resize it with CSS to 100x80
. A CDN/proxy like CloudFlare, that you put in front of your website, then enables client hints and resizes your images on the fly. You have 200x200
on the server, but the client gets 100x100
, resized to 100x80
(note, that there's no Height
header in the client hints).
100px 100px
browser -----------> proxy -------------> server ¯\_(ツ)_/¯
|
| 200px
|
100px 200px v
browser <------- proxy resizes <--------- proxy