Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMG tag: Omitting the sizes attribute when using width descriptors should be deemed correct. #7116

Open
ADTC opened this issue Sep 25, 2021 · 7 comments
Labels
clarification Standard could be clearer topic: img

Comments

@ADTC
Copy link

ADTC commented Sep 25, 2021

https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset

If the srcset attribute is present and has any image candidate strings using a width descriptor, the sizes attribute must also be present, ...

We should have the option to omit the sizes attribute and allow the user agent to default this attribute to 100vw. According to the algorithm given at the following link, user agents already do this. The absence of sizes attribute is considered as an empty string, and when parsing the empty string, no sizes are found, hence the default value of 100vw is returned:

https://html.spec.whatwg.org/multipage/images.html#parsing-a-sizes-attribute

However, the above quoted text is confusing to readers as it implies that the attribute must always be present (when using width descriptors in the srcset attribute) otherwise the HTML code of the <img> tag is deemed incorrect. I believe the HTML code of the <img> tag should still be deemed correct even if the sizes attribute is not present in such case. In its absence, the user agent falls back to assuming it to be 100vw and this behavior should be expected as a correct behavior.

The omission of sizes attribute is most useful for images that span the full width of the viewport (the so-called hero images).

@whatwg whatwg deleted a comment Sep 25, 2021
@whatwg whatwg deleted a comment Sep 25, 2021
@annevk
Copy link
Member

annevk commented Sep 27, 2021

cc @zcorpan @yoavweiss

@tabatkins
Copy link
Contributor

The current requirement is an author-level requirement, and I believe still has value; it's not obvious from inspection that <img srcset="..."> is intended to be 100vw wide, so it's better to require authors to be explicit there, imo.

We've still gotta do something with the image when the attribute isn't present, so folding it into the general error-handling is fine, but I think it should still be considered an error by validators.

@zcorpan
Copy link
Member

zcorpan commented Sep 28, 2021

Like @tabatkins said, this is a document conformance requirement intended to help web developers be mindful about providing a correct sizes value. If we allow omitting the attribute, web developers will likely omit it also in cases where 100vw is not appropriate.

See ResponsiveImagesCG/picture-element#253

@ADTC
Copy link
Author

ADTC commented Sep 28, 2021

Perhaps it can be resolved by adding a small note or warning saying that user agents will default the sizes attribute to 100vw if missing but validators will generate an error on its omission.

@zcorpan zcorpan added the clarification Standard could be clearer label Sep 28, 2021
@tlylt
Copy link

tlylt commented Jan 17, 2023

Hi, could I work on this?

If the srcset attribute is present and has any image candidate strings using a width descriptor, the sizes attribute must also be present, and is a sizes attribute. The sizes attribute contributes the source size to the source set (if no source element was selected).

(adding a note below saying that)

"User agents will default the sizes attribute to 100vw if it is missing but validators should generate an error on its omission."

@ADTC
Copy link
Author

ADTC commented Jan 17, 2023

Yes please! (But I'm not a maintainer so please wait for a maintainer to give the green signal.) @annevk @zcorpan

@zcorpan
Copy link
Member

zcorpan commented Jan 17, 2023

That seems ok, though note that #8008 makes sizes optional in some cases (when loading=lazy).

Also, notes shouldn't use normative keywords ("should"), use "will" instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clarification Standard could be clearer topic: img
Development

No branches or pull requests

5 participants