The <picture>
element in HTML is a powerful tool for delivering responsive images, allowing you to serve different image files or formats based on factors like screen size, resolution, or format support. It is often used in conjunction with <source>
elements and a fallback <img>
tag to ensure compatibility.
Basic Syntax
<picture>
<source srcset="image-small.jpg" media="(max-width: 600px)">
<source srcset="image-medium.jpg" media="(max-width: 1200px)">
<img src="image-large.jpg" alt="Description of the image">
</picture>
Explanation of Components
<picture>
:- Acts as a container for multiple image sources.
- Contains one or more
<source>
elements followed by a fallback<img>
tag.
<source>
:- Specifies a different image file and its media query or type.
- Attributes:
srcset
: The image file to use.media
: A media query to define when this image source should be used.type
: Specifies the MIME type of the image (e.g.,image/webp
).
<img>
:- The fallback image for browsers that don’t support the
<picture>
element or the<source>
attributes. - The
src
attribute specifies the default image. - The
alt
attribute provides alternative text for accessibility.
- The fallback image for browsers that don’t support the
Responsive Example with Different Image Sizes
<picture>
<source srcset="small.jpg" media="(max-width: 600px)">
<source srcset="medium.jpg" media="(max-width: 1200px)">
<img src="large.jpg" alt="A beautiful landscape">
</picture>
- If the viewport width is 600px or smaller, the browser loads
small.jpg
. - If the viewport width is between 601px and 1200px, the browser loads
medium.jpg
. - If the viewport width is greater than 1200px, the browser loads
large.jpg
.
Example with Different Image Formats
<picture>
<source srcset="image.webp" type="image/webp">
<source srcset="image.jpg" type="image/jpeg">
<img src="image.jpg" alt="A beautiful view">
</picture>
- Modern browsers that support WebP will load
image.webp
. - Older browsers or browsers that don’t support WebP will fall back to
image.jpg
.
Example with Art Direction (Different Crops)
The <picture>
element can also be used for art direction by serving entirely different crops or designs of the same image depending on the screen size.
<picture>
<source srcset="wide-banner.jpg" media="(min-width: 800px)">
<source srcset="square-banner.jpg" media="(max-width: 799px)">
<img src="fallback-banner.jpg" alt="Promotional banner">
</picture>
- Wide screens (800px or more) will display
wide-banner.jpg
. - Narrow screens (799px or less) will display
square-banner.jpg
. - If neither condition applies,
fallback-banner.jpg
will be used.
Key Points to Remember
- The
<img>
element is required as a fallback for browsers that don’t support the<picture>
element. - The browser evaluates the
<source>
elements from top to bottom and uses the first one that matches. - The
<picture>
element is ideal for responsive design, art direction, and serving optimized images. - Always include the
alt
attribute for accessibility.
Let me know if you’d like more detailed examples or further clarification!