Skip to content

autoSizes

The autoSizes function calculates the sizes attribute for a CSS selector, a DOM element, a list of DOM elements, or an array of DOM elements if the data-sizes attribute is set to auto.

To lazy load images, refer to the lazyLoad method.

Type Declarations

ts
function autoSizes<T extends HTMLImageElement | HTMLSourceElement>(
  /**
   * A CSS selector, a DOM element, a list of DOM elements, or an array of DOM elements to calculate the `sizes` attribute for.
   *
   * @default 'img[data-sizes="auto"], source[data-sizes="auto"]'
   */
  selectorsOrElements?: string | T | NodeListOf<T> | T[]
): void

Example

html
<img
  srcset="image-320w.jpg 320w, image-640w.jpg 640w"
  data-sizes="auto"
>

Released under the MIT License.