HTMLLinkElement: imageSizes property

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("HTML DOM")}}

The **imageSizes** property of the {{domxref("HTMLLinkElement")}} interface indicates the size and conditions for the preloaded images defined by the {{domxref("HTMLLinkElement.imageSrcset", "imageSrcset")}} property. It reflects the value of the {{htmlelement("link")}} element's [imagesizes](/en-US/docs/Web/HTML/Reference/Elements/link#imagesizes) attribute. This property can retrieve or set the imagesizes attribute value.

The <link> element's imagesizes attribute is the same as the {{htmlelement("img")}} element's sizes attribute: a comma-separated source size list. Each source size includes a media condition, the size of the image as a {{cssxref("length")}}, or the keyword auto, which must come first. For more information about the syntax of the sizes attribute, see [<img>](/en-US/docs/Web/HTML/Reference/Elements/img#sizes).

The imagesrcset and imagesizes attributes are only relevant on <link> elements that have both a rel attribute set to preload and the as attribute set to image.

Value

A string composed of comma-separated source sizes, or the empty string "" if unspecified.

Examples

Given the following <link> element:

html
<link
  rel="preload"
  as="image"
  imagesrcset="narrow.png, medium.png 600w, wide.png 1200w"
  imagesizes="(width < 400px) 200px, (400px <= width < 600px) 75vw, 50vw" />
html hidden
css hidden
js hidden

…we can retrieve and update the imagesizes attribute value with the imageSizes property:

js
const link = document.querySelector("link");
log(`Original: ${link.imageSizes}`);

// Change the value
link.imageSizes = "50vw";
log(`Updated: ${link.imageSizes}`);

{{EmbedLiveSample('Examples',"","80")}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{domxref("HTMLLinkElement.imageSrcset")}}
  • {{domxref("HTMLImageElement.sizes")}}
  • Media queries
  • [Using the srcset and sizes attributes](/en-US/docs/Web/HTML/Reference/Elements/img#using_the_srcset_and_sizes_attributes)