Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

imagesrcset — responsive image preloading

The imagesrcset attribute lets the browser select an image variant to preload. It is allowed only on a link element with rel="preload" and as="image", and corresponds to the srcset attribute of a later image.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<link rel="preload" as="image" imagesrcset="URL WIDTHw, URL WIDTHw" imagesizes="SOURCE_SIZE">

imagesrcset specifies image preload candidates, not an image displayed on the page. It is allowed only on a link element with rel="preload" and as="image". A separate img element displays the image.

Its value uses srcset syntax: a comma-separated list of URLs, optionally with width descriptors such as 400w or density descriptors such as 2x. Do not mix w and x in one list. w describes the resource's actual width in pixels; x describes pixel density.

If the list contains w descriptors, imagesizes is also required and describes the expected image width in the page layout. It does not set CSS dimensions. The browser selects a candidate using the source size and device conditions; the list does not mean that all variants are downloaded.

Match imagesrcset and imagesizes to the target img element's srcset and sizes. You can omit href to avoid preloading an unsuitable variant in a browser without imagesrcset support. The example has one candidate and an embedded resource, making its visual result independent of external files; it does not measure network benefits.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>imagesrcset demo</title>
  <link rel="preload" as="image"
        imagesrcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='40'%20height='40'%3E%3Cpath%20fill='red'%20d='M0%200h40v40H0z'/%3E%3C/svg%3E 40w"
        imagesizes="40px">
</head>
<body>
  <img width="40" height="40" alt="Red square"
       srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='40'%20height='40'%3E%3Cpath%20fill='red'%20d='M0%200h40v40H0z'/%3E%3C/svg%3E 40w"
       sizes="40px">
</body>
</html>

Result

A red square measuring 40 × 40 CSS pixels; no console output.

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources