visit
This tutorial covers the ins and outs of using the srcset
attribute, and how imgix makes the process easier.
Serving correctly-sized images is important because it can minimize bytes transferred and . The srcset
attribute is one of the best ways to do so today.
srcset
provides a simple way to specify different images for different device resolutions. It allows sites to serve 2x, 3x, or higher versions of images to modern devices with high-resolution displays. Using it within an img
tag is easy:
<img
srcset="asset.png 1x, asset-2x.png 2x, asset-3x.png 3x"
src="asset.png"
>
With imgix, your entire image library is srcset
-ready instantly.
Using the imgix w
and dpr
URL parameters, we can simplify the amount of effort it takes to generate the srcset
attributes on our images. For this example, we will use the image located at:
//assets.imgix.net/examples/bluehat.jpg
<img
srcset="//assets.imgix.net/examples/bluehat.jpg?w=400&dpr=1 1x,
//assets.imgix.net/examples/bluehat.jpg?w=400&dpr=2 2x,
//assets.imgix.net/examples/bluehat.jpg?w=400&dpr=3 3x"
src="//assets.imgix.net/examples/bluehat.jpg?w=400"
>
We get an image tag that serves out the best resolution for each device based on its device-pixel ratio (DPR). imgix will automatically serve more pixels when given the dpr
parameter.
You can see that we’ve applied dpr=1
, dpr=2
, and dpr=3
to our 1x, 2x, and 3x assets, respectively. The dpr
parameter instructs imgix to treat the w
parameters as (also known as “CSS pixels”).
Thus, the 400×300 image at dpr=2
will actually be an 800×600 pixel image. The dpr=3
image will be 1200×900 pixels.
This also removes the headache if and when a 4x
device comes out. imgix currently supports up to dpr=5
.
This practice works best with fixed image layouts. Using srcset
with dpr
is currently .
srcset
and sizes
With Media QueriesA different approach to handling responsive images for fluid layouts is to use size definitions with srcset
. This solution gives you the ability to target sizes based on media query definitions within a sizes
attribute.
The following example demonstrates sizing three images with imgix at 1024, 640, and 480 pixels wide. Using the sizes
attribute, we are targeting two queries for behavior for the images.
At a viewport of 36em
or above, the images will display at 1/3 the viewport width.
<img
srcset="//assets.imgix.net/unsplash/bridge.jpg?w=1024&h=1024&fit=crop 1024w,
//assets.imgix.net/unsplash/bridge.jpg?w=640&h=640&fit=crop 640w,
//assets.imgix.net/unsplash/bridge.jpg?w=480&h=480&fit=crop 480w"
src="//assets.imgix.net/unsplash/bridge.jpg?w=640&h=640&fit=crop"
sizes="(min-width: 36em) 33.3vw, 100vw"
>
There is more to think about when delivering the best images possible with srcset
and imgix.
One of the challenges of using srcset
is that you want to generate enough sizes so that most of your users are downloading images that are close in size to what they see on the screen, but if you generate too many sizes you can end up impacting cache-ability, which could have a negative performance impact.
Luckily, many of the can automatically generate an optimal srcset
for you.
fit=max
Using the fit=max
parameter on an imgix URL will ensure that an image is never delivered larger than its original size. This way, when requesting a dpr=3
image, there won’t be any image extrapolation. Read more about fit
in the documentation.
auto=format
The auto=format
parameter will deliver lightweight WebP images for browsers that support it (Chrome, Firefox, etc.) and fall back to the original format for other instances.
When setting dpr
with imgix, you may want to consider adjusting the quality of your images. Setting the q
parameter to lower values for higher DPRs allows you to reduce the file size while maintaining a denser pixel set for your image.
?q=80
The image size is 21.3kB. .
?dpr=2&q=40
The image size is 34.7kB.
?dpr=3&q=20
The image size is 42.1kB.
Here is an implementation of these examples as applied to our srcset
DPR example:
<img
srcset="//assets.imgix.net/examples/bluehat.jpg?w=400&dpr=1 1x,
//assets.imgix.net/examples/bluehat.jpg?w=400&fit=max&q=40&dpr=2 2x,
//assets.imgix.net/examples/bluehat.jpg?w=400&fit=max&q=20&dpr=3 3x"
src="//assets.imgix.net/examples/bluehat.jpg?w=400"
>