Skip to main content

HTML Tags

HTML <picture> Tag

Written by Updated HTML <picture> Tag exercises

The picture tag lets you give the browser several images and let it pick the right one: a square crop for phones, a wide one for desktops, a modern format where supported. The img inside is the fallback, and also the part that is displayed.

This page is the tag reference. A longer walk-through with more examples is in HTML Picture Element.

Syntax

html

<picture>
  <source srcset="image-small.jpg" media="(max-width: 600px)">
  <source srcset="image-large.jpg" media="(min-width: 601px)">
  <img src="image-default.jpg" alt="Description">
</picture>
AttributeUsed onWhat it does
srcsetsourceThe image file, or several sizes of it.
mediasourceA media query, like (min-width: 601px). The source is used when it matches.
typesourceThe file format, like image/webp. Skipped if the browser cannot show it.
sizessourceHow wide the image will be shown, so the browser can pick from srcset.

The <picture> element itself has no attributes of its own. alt, width and height go on the <img>.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Picture Tag Example</title>
</head>
<body>

  <h2>Responsive Images with &lt;picture&gt;</h2>
  <picture>
    <source srcset="https://picsum.photos/id/237/300/300.webp" media="(max-width: 600px)">
    <source srcset="https://picsum.photos/id/237/600/300.webp" media="(min-width: 601px)">
    <img src="https://picsum.photos/id/237/200/200.webp" alt="Sample Responsive Image" width="600">
  </picture>

</body>
</html>

On a narrow screen (600px or less) you see a square crop; on a wider one, a wide crop of the same photo. Resize the preview to see it switch.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Every current browser supports it. Very old ones simply show the <img>, so nothing breaks.

Good to know

  • The browser uses the first <source> that matches, so order matters.
  • Always include the <img>; without it, nothing appears.
  • If you only need the same image at different sizes, a plain <img srcset sizes> is simpler than <picture>.

HTML <picture> Tag exercises

Offer a wide version

Make a picture that shows https://ik.imagekit.io/html5andcss3/lesson-samples/sample_thumbnail.png from a screen width of 800px. Otherwise it shows https://html5andcss3.org/samples/star.svg described as A star.

Result

Check your answer to see it here.