KodingKaro
HTML · Intermediate · Responsive Images

picture, art direction, formats, and loading

Some responsive images need a different crop on small screens, not merely a smaller copy of the same image.

✓ What you will learn in this topic

  • Explain the purpose of picture, art direction, formats, and loading in a real HTML document.
  • Choose and apply the elements shown in both examples.
  • Recognize the specific mistake described in this lesson.

Concept explanation

Picture contains source candidates and a fallback img. Media conditions can select a crop suited to the viewport, while type can advertise an image format and let compatible browsers choose it. The img remains essential for fallback and alt text. Loading=\"lazy\" can defer below-the-fold images, but do not lazy-load the main image visible immediately because that can delay the page’s most important content.

Syntax

Place source elements before a fallback img inside picture; use media for crop choices and type for formats.

Example 1: a small page

A narrow viewport receives the portrait crop; other cases use the fallback image.

<picture>
  <source media=\"(max-width: 40rem)\" srcset=\"portrait-crop.jpg\">
  <img src=\"wide-garden.jpg\" alt=\"A gardener watering raised beds\" width=\"1200\" height=\"800\">
</picture>

Expected result: The browser chooses a source that matches the viewport and keeps the same meaningful alternative text.

Example 2: a practical page

Modern formats are offered first and the JPEG img remains as a fallback.

<picture>
  <source type=\"image/avif\" srcset=\"workshop.avif\">
  <source type=\"image/webp\" srcset=\"workshop.webp\">
  <img src=\"workshop.jpg\" alt=\"A woodworker sanding a small table\" width=\"1200\" height=\"800\" loading=\"lazy\">
</picture>

Expected result: A supported browser uses an offered modern image format; otherwise it falls back to JPEG.

Common mistakes

Do not list several srcset files with the same width descriptor when their actual pixel widths differ.

Best practices

Use width descriptors for resolution candidates, sizes for the intended rendered width, and picture when art direction or format choice matters.

Practice exercise

Prepare a responsive hero image with two resolutions, a mobile crop, width and height, descriptive alt, and lazy loading only if it begins below the fold.

Lesson summary

  • Use picture for art direction or format selection, preserve img as fallback, and lazy-load only noncritical images.
  • Use width descriptors for resolution candidates, sizes for the intended rendered width, and picture when art direction or format choice matters.

Knowledge check

Why should the img remain inside picture?

Show the explanation

It supplies the fallback source, dimensions, and alternative text when no source candidate is selected.

Practical example

html
<picture>
  <source media=\"(max-width: 40rem)\" srcset=\"portrait-crop.jpg\">
  <img src=\"wide-garden.jpg\" alt=\"A gardener watering raised beds\" width=\"1200\" height=\"800\">
</picture>

Expected result:

The browser chooses a source that matches the viewport and keeps the same meaningful alternative text.

Hands-on practice task

Prepare a responsive hero image with two resolutions, a mobile crop, width and height, descriptive alt, and lazy loading only if it begins below the fold.

In this section

Other lessons in Responsive Images

Broaden your knowledge

Related learning topics