KodingKaro
HTML · Intermediate · Responsive Images

srcset and sizes for resolution choices

One image file is not always efficient for a small phone and a large high-density screen.

✓ What you will learn in this topic

  • Explain the purpose of srcset and sizes for resolution choices in a real HTML document.
  • Choose and apply the elements shown in both examples.
  • Recognize the specific mistake described in this lesson.

Concept explanation

The srcset attribute lists alternative image resources. Width descriptors such as 480w describe each file’s intrinsic pixel width. The sizes attribute describes the expected CSS display width under layout conditions; the browser combines that information with its display density to choose a candidate. Keep a fallback src for browsers or situations that do not use the candidates. These attributes describe resources, not exact layout styling.

Syntax

List file-width pairs in srcset and describe the rendered slot with sizes.

Example 1: a small page

The browser can choose a candidate based on slot width and screen density.

<img src=\"trail-800.jpg\" srcset=\"trail-400.jpg 400w, trail-800.jpg 800w\" sizes=\"100vw\" alt=\"A path through pine trees\" width=\"800\" height=\"500\">

Expected result: The trail image displays while the browser selects a suitable file for the available width.

Example 2: a practical page

The sizes expression matches a full-width phone image and a half-width desktop card; lazy loading suits below-fold content.

<img src=\"class-960.jpg\" srcset=\"class-480.jpg 480w, class-960.jpg 960w, class-1440.jpg 1440w\" sizes=\"(max-width: 40rem) 100vw, 50vw\" alt=\"Students shaping clay at a pottery wheel\" width=\"1440\" height=\"960\" loading=\"lazy\">

Expected result: The browser chooses among three image widths based on viewport and pixel density.

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 srcset for alternatives and sizes to tell the browser how wide the image will be displayed.
  • Use width descriptors for resolution candidates, sizes for the intended rendered width, and picture when art direction or format choice matters.

Knowledge check

What does a 960w descriptor in srcset describe?

Show the explanation

It describes the image resource’s intrinsic width as 960 pixels.

Practical example

html
<img src=\"trail-800.jpg\" srcset=\"trail-400.jpg 400w, trail-800.jpg 800w\" sizes=\"100vw\" alt=\"A path through pine trees\" width=\"800\" height=\"500\">

Expected result:

The trail image displays while the browser selects a suitable file for the available width.

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