Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

sizes — image source-selection width and icon dimensions

The sizes attribute has two distinct meanings: on img and source it describes the image width used to select a resource from srcset; on link it declares available icon dimensions. It does not replace CSS rules that determine the displayed image size.

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

Syntax

img/source: sizes="(media-condition) length, fallback-length" | sizes="auto, fallback-length"
link: sizes="16x16 32x32" | sizes="any"

On img and on source inside picture, sizes describes the image's intended layout width. The browser combines it with srcset candidates using width descriptors, such as 640w, to select a resource. It is a file-selection hint, not an instruction to change CSS dimensions. It is not used to choose between candidates with 1x and 2x descriptors.

The list is comma-separated, for example (max-width: 600px) 100vw, 50vw. The first matching condition supplies the width; a final unconditional length provides a fallback. Lengths must be nonnegative; units such as px, em, vw and math functions such as calc() are allowed, but percentages are not. If parsing finds no usable value, it returns 100vw.

On img, using w descriptors requires sizes. On source, the same rule applies unless the following img allows auto-sizes. Using sizes="auto" requires that this img have loading="lazy" and its own sizes equal to auto or starting with auto,. auto must come first; fallback entries may follow. The width then comes from the rendered image's size. Also supply dimensions through width and height or CSS. Omitting sizes on a preceding source is equivalent to auto in this case.

On link, the meaning differs: sizes advises which icon dimensions are available and is allowed only when rel contains icon or apple-touch-icon. Its value consists of unique space-separated tokens, such as 16x16 32x32, or any for arbitrary sizes. Pairs give width and height in pixels; numbers are positive with no leading zeros, separated by x or X. Tokens are ASCII case-insensitive. For image preloading through link, the corresponding attribute is imagesizes, not sizes.

The example embeds an SVG without external files. CSS sets the actual dimensions, while sizes="160px" describes the same width for source selection. Reading the attribute produces predictable text; it does not test the resource-selection algorithm.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>sizes</title>
<style>img { display: block; width: 160px; height: 80px; }</style>
<img id="sample" alt="Blue rectangle" width="160" height="80"
     sizes="160px"
     srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='160'%20height='80'%3E%3Cpath%20fill='blue'%20d='M0,0H160V80H0Z'/%3E%3C/svg%3E 160w">
<pre id="result"></pre>
<script>
  document.getElementById('result').textContent =
    'sizes=' + document.getElementById('sample').getAttribute('sizes');
</script>
</html>

Result

sizes=160px

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