paint-brush
A Beginner's Guide: Responsive Images With srcsetby@imgix
691 reads
691 reads

A Beginner's Guide: Responsive Images With srcset

by imgixJune 23rd, 2023
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

Optimizing images is extremely important for running a performant site and achieving responsive design. The `srcset` attribute is one of the best ways to do so today. With imgix, you can easily automate srcset, either in fluid-width or fixed-width form, and take advantage of automatic compression, format conversion, and variable quality settings.
featured image - A Beginner's Guide: Responsive Images With srcset
imgix HackerNoon profile picture

Because nearly 50% of a typical page’s weight is made up of images, optimizing images is extremely important for running a performant site. Every byte you don’t have to transfer to serve your content means a leaner page and faster load times for your users.


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 CPU overhead. The srcset attribute is one of the best ways to do so today.


Diagram of display densities


srcset & Display Densities

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"
>


While this delivers the best assets to users, it moves the burden to the service to generate and store each version of every asset. This can cause storage costs to balloon, and you may never serve every asset you generate.


When dealing with a large library or with user-generated content, this is untenable.


With imgix, your entire image library is srcset-ready instantly.

Using srcset With imgix

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:

https://assets.imgix.net/examples/bluehat.jpg


We want to serve this image at 400 pixels wide:

<img
  srcset="https://assets.imgix.net/examples/bluehat.jpg?w=400&dpr=1 1x,
          https://assets.imgix.net/examples/bluehat.jpg?w=400&dpr=2 2x,
          https://assets.imgix.net/examples/bluehat.jpg?w=400&dpr=3 3x"
  src="https://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.

Calculating Device-Pixel Ratio

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 device-independent pixels (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.


Diagram of different DPRs


This gives you the best of both worlds: full resolution for devices that support it, without delivering more data than necessary to devices that won’t use it. By using imgix, we only have to store the original asset, and then manipulate it on the fly, as we’ve seen above.


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 widely supported.

Using srcset and sizes With Media Queries

A 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 browser will request the most appropriate image or—depending on the browser—will load the best image from the cache when available.


Illustration of different device dimensions


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.


Below that size, the images will display at the full size of the viewport. At those sizes, the browser will determine which image to load when the page is rendering for the given target size.


<img
  srcset="https://assets.imgix.net/unsplash/bridge.jpg?w=1024&h=1024&fit=crop 1024w,
          https://assets.imgix.net/unsplash/bridge.jpg?w=640&h=640&fit=crop 640w,
          https://assets.imgix.net/unsplash/bridge.jpg?w=480&h=480&fit=crop 480w"
  src="https://assets.imgix.net/unsplash/bridge.jpg?w=640&h=640&fit=crop"
  sizes="(min-width: 36em) 33.3vw, 100vw"
>


See the Pen <a href="https://codepen.io/imgix/pen/VLNZaW/" target="_blank">VLNZaW</a> by imgix (<a href="https://codepen.io/imgix" target="_blank">@imgix</a>) on <a href="https://codepen.io" target="_blank">CodePen</a>.

Best Practices Using imgix

There is more to think about when delivering the best images possible with srcset and imgix.


imgix affords the ability to add additional operations to give you more control over your output images, and because they are defined in the URL, you can fine-tune your settings and make late-stage edits as decisions change.


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 imgix libraries can automatically generate an optimal srcset for you.

Use 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.

Use 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.


More modern formats like WebP can greatly cut down the amount of image data sent to the client; sometimes by as much as 35%. Read more about Automatic Content Negotiation in the documentation.

Use Variable Quality

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. Try it and see the difference in Sandbox.
Image where dpr is 1 and q is 80


?dpr=2&q=40 The image size is 34.7kB. Try it and see the difference in Sandbox. Image where dpr is 2 and q is 40


?dpr=3&q=20 The image size is 42.1kB. Try it and see the difference in Sandbox.
Image where dpr is 3 and q is 20


This common practice is made easier with the imgix URL API. Adjusting the quality works especially well with lossy formats such as WebP and JPEG.

Putting It All Together

Here is an implementation of these examples as applied to our srcset DPR example:


<img
  srcset="https://assets.imgix.net/examples/bluehat.jpg?w=400&dpr=1 1x,
          https://assets.imgix.net/examples/bluehat.jpg?w=400&fit=max&q=40&dpr=2 2x,
          https://assets.imgix.net/examples/bluehat.jpg?w=400&fit=max&q=20&dpr=3 3x"
  src="https://assets.imgix.net/examples/bluehat.jpg?w=400"
>


See the Pen <a href="https://codepen.io/imgix/pen/MwRjzZ/" target="_blank">MwRjzZ</a> by imgix (<a href="https://codepen.io/imgix" target="_blank">@imgix</a>) on <a href="https://codepen.io" target="_blank">CodePen</a>.

Summary and Related Tutorials

Responsive imagery is a rapidly-changing area of implementation, and different methods are applicable to different use cases. Here are our other tutorials that touch on aspects of responsive design.