The HTML <picture> element is used to provide alternative image sources for different display scenarios, such as different screen sizes or resolutions, and to ensure that the appropriate image is displayed for each scenario.
The <picture> element contains one or more <source> elements and one <img> element. The <source> element specifies the image to be displayed for a particular scenario, using attributes such as “media” and “srcset”. The <img> element provides a fallback option in case none of the <source> elements match the current scenario.
Here’s an example of how the <picture> element can be used:
<picture>
<source media="(min-width: 1024px)" srcset="large-image.jpg">
<source media="(min-width: 768px)" srcset="medium-image.jpg">
<img src="small-image.jpg" alt="An image">
</picture>
In this example, if the screen width is greater than or equal to 1024 pixels, the “large-image.jpg” will be displayed. If the screen width is between 768 and 1023 pixels, the “medium-image.jpg” will be displayed. And if the screen width is less than 768 pixels, the “small-image.jpg” will be displayed. If none of the <source> elements match the current scenario, the <img> element will be displayed.
Using the <picture> element can improve the performance and user experience of your website by ensuring that the appropriate image is loaded for each scenario, rather than loading a large image and scaling it down or loading a small image and scaling it up.