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

Use srcset and sizes on img element in picture #191

Merged
merged 2 commits into from
Jan 15, 2024

Conversation

adamwolf
Copy link
Contributor

Thanks, zachleat, for all your hard work. I really appreciate it.

Previously, in the case where the image has multiple formats and multiple sizes, eleventy-img would generate output somewhat like

<picture>
<source type="image/webp" srcset="/img/KkPMmHd3hP-200.webp 200w, /img/KkPMmHd3hP-400.webp 400w" sizes="100vw">
<source type="image/jpeg" srcset="/img/KkPMmHd3hP-200.jpeg 200w, /img/KkPMmHd3hP-400.jpeg 400w" sizes="100vw">
<img alt="" src="/img/KkPMmHd3hP-200.jpeg" width="400" height="266">
</picture>

This change makes eleventy-img instead produce output like

<picture>
<source type="image/webp" srcset="/img/KkPMmHd3hP-200.webp 200w, /img/KkPMmHd3hP-400.webp 400w" sizes="100vw">
<img alt="" src="/img/KkPMmHd3hP-200.jpeg" srcset="/img/KkPMmHd3hP-200.jpeg 200w, /img/KkPMmHd3hP-400.jpeg 400w" sizes="100vw" width="400" height="266">
</picture>

This adds srcset and sizes to the img tag, and doesn't produce a source tag for the same format provided by the img tag.

The initial motivation around this was to get RespImageLint to stop complaining by adding srcset and sizes to the img tag. I'm not an HTML expert, and I'm not sure this change is strictly correct. It seems to me to matter for browsers that don't support picture or don't support any of the formats in source tags.

Previously, in the case where the image has multiple formats and multiple sizes, elventy-img would generate output somewhat like

```
<picture>
<source type="image/webp" srcset="/img/KkPMmHd3hP-200.webp 200w, /img/KkPMmHd3hP-400.webp 400w" sizes="100vw">
<source type="image/jpeg" srcset="/img/KkPMmHd3hP-200.jpeg 200w, /img/KkPMmHd3hP-400.jpeg 400w" sizes="100vw">
<img alt="" src="/img/KkPMmHd3hP-200.jpeg" width="400" height="266">
</picture>
```

This change makes eleventy-img instead produce output like

```
<picture>
<source type="image/webp" srcset="/img/KkPMmHd3hP-200.webp 200w, /img/KkPMmHd3hP-400.webp 400w" sizes="100vw">
<img alt="" src="/img/KkPMmHd3hP-200.jpeg" srcset="/img/KkPMmHd3hP-200.jpeg 200w, /img/KkPMmHd3hP-400.jpeg 400w" sizes="100vw" width="400" height="266">
</picture>
```

This adds srcset and sizes to the img tag, and doesn't produce a source
tag for the same format provided by the img tag.

The initial motivation around this was to get RespImageLint to stop
complaining by adding srcset and sizes to the img tag. I'm not an HTML expert, and I'm not sure this change is strictly correct.
@zachleat zachleat added this to the Eleventy Image v3.1.9 milestone Jan 15, 2024
@zachleat zachleat merged commit 6812731 into 11ty:main Jan 15, 2024
@zachleat
Copy link
Member

zachleat commented Feb 2, 2024

Shipping with v3.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants