What is a picture tag?

🔍 What is it?

  1. The <picture> tag is an HTML element used to specify multiple sources for an image and allow the browser to choose the most appropriate one based on factors like screen resolution, viewport size, and image format support.

❓ How is it used?

  1. It contains one or more <source> elements, each specifying a different image source and its corresponding media conditions.
  2. It also contains an <img> element, which serves as a fallback in case none of the <source> elements match the browser's criteria.

Why is it needed?

  1. It ensures that users receive the most suitable image based on their device capabilities and screen characteristics, optimizing both performance and visual quality.
  2. It helps developers implement responsive images that adapt to various devices and screen sizes without sacrificing quality or wasting bandwidth.

Examples:

<picture>
  <source srcset="image-large.jpg" media="(min-width: 1024px)">
  <source srcset="image-medium.jpg" media="(min-width: 768px)">
  <img src="image-small.jpg" alt="Description">
</picture>

In this example:

- The browser will choose image-large.jpg for screens wider than 1024 pixels, image-medium.jpg for screens wider than 768 pixels, and image-small.jpg as a default for smaller screens or unsupported media conditions.