Responsive Images with HTML
Tags: frontend, beginners, tutorial, html
Published: 2024-06-26 14:33:10

Providing the best user experience across various devices is very crucial. One essential aspect of this experience is the use of responsive images. There is no need to embed large images on a page if it is being viewed on a small screen. Mobile users don't intend to waste bandwidth by downloading a large image designed for desktop or large screen users when a small image would suffice for their device. In contrast, a small image starts to look pixelated when displayed on a large screen. It's ideal to have multiple image resolutions made available to the user's web browser and let the browser determine the optimal resolution to load based on the screen size of the user's device. 
The `<picture>` element in HTML5 offers a compelling way to handle responsive images, ensuring that the appropriate image is displayed based on the user's device, screen size, and resolution. In this post, I'll delve into the basics of the `<picture>` element and how to implement it to achieve responsive images across different devices.

**Why Use the `<picture>` Element?**

- Optimized Performance: By serving the appropriate image size and 
  resolution for different devices, you can reduce load times and 
  save bandwidth.
- Enhanced Flexibility: The <picture> element provides fine-grained 
  control over which image is displayed based on specific 
  conditions.

**Syntax of the `<picture>` Element**

The `<picture>` element is a wrapper containing several `<source>` elements that provide different "sources" for the browser to choose from, followed by the all-important `<img>` element.
Here's a basic example of how to use the <picture> element:

```
<picture>
   <source media='(min-width: 800px)' srcset='./large-1440w.png' />
   <source media='(min-width: 410px)' srcset='./medium-768w.png' />
   <source media='(max-width: 400px)' srcset='./small-375w.png' />
   <img src='./assets/bg-img1-1440w.png' alt='random image desc' />
</picture>
```
In the example above, we have an image of three different sizes. The  `<source>` element has a `media` attribute, where you specify the media condition. if the viewport width is 800px wide or more, the first `<source>` element's image will be displayed. Otherwise, if the viewport width is 410px or more, it'll be the second one, and if it's below 400px the last `<source>` element will be displayed.

Always include an `<img>` element with both src and alt attributes just before the closing `</picture>` tag. This ensures that an image will display as a default when no media conditions are met and provides a fallback for browsers that do not support the <picture> element.

**Note**: To see which images were loaded at different screen sizes, you can use Firefox DevTools's Network Monitor tab or Chrome DevTools's Network panel.

![A screenshot of a chrome network tab showing the image rendered on a screen size of 768px](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4aaa1debtudbuvkfvy7t.png) 


**Conclusion**

In conclusion, utilizing the `<picture>` element is an effective method to ensure your images are responsive, providing an optimized experience for users across all devices. By tailoring the image size and resolution to the specific requirements of different screen sizes, you can significantly improve load times, conserve bandwidth, and maintain image quality.