This article explores how you can create responsive images in HTML using srcset and the picture element.
Why Should You Use Responsive Images?
When the software engineers were creating the web, they did not consider how browsers would handle responsive images. After all, users were only accessing the web from desktops or laptops. Of course, that is not true today.
According to Statista, over 90 percent of the global internet population go online using their mobile phone. Most of the web pages on the internet contain images and these images are one of the metrics used to measure web performance. To improve performance, you need to optimize your images by making them responsive.
How to Create Responsive Images in HTML
You can approach responsive images from two angles—either by serving the same image with different sizes or serving different images according to the display characteristics. You could use
Using srcset
The standard HTML only allows you to specify one image file. If you want to display a different image depending on the size of the device, then you should use srcset.
Syntax:
srcset allows you to provide additional source files, and the browser will choose the image that seems optimal for that image size.
srcset is made of three parts: The image filename which specifies the path to the source image, a space, and the intrinsic or real width of the image.
Using srcset With sizes
The issue with using srcset is that you have no control of what image the browser will pick to display. Combining srcset with sizes solves this problem. sizes define a set of media conditions that hint at the image with the optimum size.
You can now rewrite the tag above as follows.
sizes is made up of a media condition, in this example it’s (max-width: 600px) which represents the viewport width, space, and the slot width (480px) specifying the space the image will fill if the media condition is true.
Here, the browser will first check the device width and compare it to the media condition. If the condition is true, it will check the slot width and load an image from srcset with the same width or the next bigger one.
Note that you are also including src which provides the image to fall back on browsers that don’t support srcset and sizes.
srcset also allows you to serve images at different resolutions using x-descriptors.
In this example, if the device has a resolution of two device pixels per CSS or more the browser will load the cute-cat-high1.jpg image.
Hardware and Software Pixels
The problem with this solution is that the images are only responsive in terms of the device’s pixel density. This is the ratio of the hardware pixels to the software or CSS pixels. A hardware pixel is the actual dot of light on the screen while the software pixel or CSS pixel is a unit of measurement. The pixel density determines the device’s resolution.
When rendering responsive images, don’t only consider the resolution; the display size is also important. Otherwise, you might end up unnecessarily loading large images or images that are too pixilated.
Using
element. While
makes images responsive by serving different sizes of the same image,
Syntax:
Consider a situation where you have a large landscape image. The image displays and looks proportional on a desktop, but it shrinks significantly on mobile such that elements on the image become tiny. The non-responsive image contributes to a bad user experience. With
Like in the first approach,
Fallback for WebP Image Format
Another thing that
Why Create Responsive Images in HTML and Not in CSS?
CSS offers robust options for handling responsive images. So why not use it? The browser preloads images before it parses CSS. So before your site’s JavaScript detects the viewport width to make the appropriate changes to the images, already the original images have been preloaded. Due to this, it is better to handle responsive images using HTML.
Aim for the Best Possible Image Quality
You have seen how you can create responsive images in HTML using