sizes-auto-requires-lazy-loading
Requires loading="lazy" wherever sizes="auto" is used on <img> and <source> elements, per the HTML Living Standard. A <source> also needs the following sibling <img> to have sizes="auto", because only such an <img> allows auto-sizes. Whether an <img> allows auto-sizes depends on the literal value of its sizes, so an <img> whose sizes specifies auto must have a value that is exactly auto or starts with auto,, with no whitespace or comments before or directly after auto (sizes attributes).
An img element allows auto-sizes if: its loading attribute is in the Lazy state, and its sizes attribute's value is "auto" (ASCII case-insensitive), or starts with "auto," (ASCII case-insensitive).
Cite: HTML Living Standard: An img element allows auto-sizes
Split out of the former srcset-sizes-constraint rule, alongside no-unpaired-srcset-sizes, no-mixed-srcset-descriptors, and no-always-matching-source.
| # | Constraint | Target |
|---|---|---|
| 1 | sizes="auto" on <img> requires loading="lazy" | img |
| 2 | sizes="auto" on <source> requires the following sibling <img> to have loading="lazy" and sizes="auto" | source (in <picture>) |
| 3 | sizes on <img> that specifies auto must be exactly auto or start with auto, | img |
How It Works
- Only
<img>and<source>elements inside<picture>are checked, and only when the element has asrcsetattribute. On an<img>withoutsrcset,sizesis allowed only as the valueautowithloading="lazy":no-disallowed-attrreports asizeswithoutloading="lazy", andno-invalid-attr-valuereports a value other thanauto. A dynamicsizesis still reported byno-disallowed-attrwhen the image has noloadingattribute or aloadingother thanlazy; whenloadingis dynamic, whether the image is lazily loaded is not judged. - Dynamic attribute values (e.g., Vue
:sizes="x", JSXsizes={x}) and elements with spread attributes ({...props}of JSX,v-bind="obj"and:[key]of Vue,&attributesof Pug, a block of a template language in the place of an attribute such as<?= $attrs ?>) are skipped. A literal written as code (sizes={"auto"}) is evaluated as the value that the framework renders it to; a literal that the framework does not output (null,false) or renders in a way that is not known is skipped, as a dynamic value is. autois recognized as the first entry of the list, as the grammar of the value reads it (case-insensitive, with whitespace and comments around it allowed). For example,sizes="auto, 100vw"andsizes=" auto , 100vw"specifyauto, butsizes="auto 100vw"does not;autoafter the first entry (sizes="100vw, auto") is a syntax error thatno-invalid-attr-valuereports.- On a
<source>, whitespace and comments aroundautoare allowed. On an<img>, they are not (constraint 3), and the following sibling<img>that a<source>relies on allows auto-sizes only when itssizesis exactlyautoor starts withauto,.
❌ Examples of incorrect code for this rule
<img srcset="a.png 480w" sizes="auto" src="a.png" alt="photo" />
<picture>
<source srcset="a.webp 480w" sizes="auto" />
<img src="a.jpg" alt="photo" />
</picture>
<picture>
<source srcset="a.webp 480w" sizes="auto" />
<img src="a.jpg" loading="lazy" alt="photo" />
</picture>
<img srcset="a.png 480w" sizes=" auto , 100vw" loading="lazy" src="a.png" alt="photo" />
✅ Examples of correct code for this rule
<img srcset="a.png 480w" sizes="auto" loading="lazy" src="a.png" alt="photo" />
<picture>
<source srcset="a.webp 480w" sizes=" auto , 100vw" />
<img src="a.jpg" loading="lazy" sizes="auto" alt="photo" />
</picture>
References
- HTML Living Standard - The img element
- HTML Living Standard - The source element
- HTML Living Standard - An img element allows auto-sizes
- HTML Living Standard - Sizes attributes
Interface
{
"sizes-auto-requires-lazy-loading": boolean
}
Default Severity
error