-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
✨Add img-sizes
attribute to amp-img
.
#24223
Conversation
This allows specifying the `sizes` to use for an image, without getting the layout sizing behavior of `sizes` AMP adds.
For applying layout, "server-side rendering (SSR)" does not remove boilerplate when a |
builtins/amp-img.md
Outdated
@@ -125,6 +125,10 @@ For the `<img>` tag in `HTML`, the `sizes` attribute is used in conjunction with | |||
|
|||
See [Responsive images with srcset, sizes & heights](https://amp.dev/documentation/guides-and-tutorials/develop/style_and_layout/art_direction) for usage of `sizes` and `srcset`. | |||
|
|||
**img-sizes** | |||
|
|||
Like the `sizes` attribute, the value is applied to the underlying `<img>` as `sizes`. In contrast, this does not affect the width of the `<amp-img>` element and is only used to select one of the sources from the `srcset`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what this does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reworded, see updated text
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't understand why this is necessary. Why wouldn't they just use sizes
? How does having <amp-img img-sizes="..."><img sizes="..."></amp-img>
have a different behavior than <amp-img sizes="..."><img sizes="..."></amp-img>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of what sizes
does in AMP: https://amp.dev/documentation/guides-and-tutorials/learn/common_attributes/#sizes
Unfortunately, AMP sets the width of an element based on the value that the sizes
attribute resolves to. This prevents developers from using sizes
, since they want it for picking the source, but do not want to actually set the <amp-img>
to that width.
Since this behavior already exists, we cannot remove it (from amp-img
or any other element).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For context, we discussed this in design review some time ago: #11575 (comment). I did not implement img-sizes
because we thought that auto-sizes
would suffice for most cases (and it did for WP), but from #21736, it seems like we do need a way to explicitly set <img>
sizes
without accidentally also setting AMP sizes
. Sizes always has a side effect in AMP, and we could not remove that side effect due to backwards compatibility issues, hence the unfortunate introduction of a new attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a few demos:
Given the img-sizes
and manual sizes
are incompatible together, I think we need to ban using both at the same time. And if we do that, we could probably use a boolean attribute instead, saying, "behave like regular sizes".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it comes down to which one of these is syntactically less confusing:
so we will either do
2
or3
. Alternative name suggestions:
for (2):sizes-behavior=<native, layout>
,use-native-sizes
, ??
for (3):native-sizes
(Src: #11575 (comment))
Either will achieve the intended purpose.
@honeybadgerdontcare The new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new img-sizes doesn't affect layout like sizes does. This simply forwards the value to the img tag, and is only used for the browser selecting a src from the srcset.
Could we double check and verify this for layout=intrinsic
and layout=fixed-height
? I think all other layouts override the width
already with runtime CSS.
We had an issue in #23453, where the developer applied width: auto
, height: auto
, along with layout=intrinsic
, and I think with position: absolute
. In this case, auto-generating srcset and sizes broke their intended styling so we ended up removing auto-sizes
for layout intrinsic.
It's possible that developer styling (e.g. width: auto), combined with layout=intrinsic
may force the image size to be dependent on the natural size of the image, which would change depending on which image url was downloaded (which is impacted by img-sizes
). So at least in that case, we may need to keep the boilerplate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the changes in this PR look good to me. I don't have strong opinions on the syntax of how to represent this (whether we introduce a new img-sizes
sizes attribute or a cancel-layout
boolean attribute) since they enable the same thing. Maybe it would be good to get more general feedback on naming, since this is a component with extremely high usage, and once we introduce new attributes, it will be very difficult to rename them?
From the engineering point of view, a parameter that changes the behavior makes a whole lot more sense here than a duplicate set of confusingly named sizes parameters. |
+1 to @dvoytenko's suggestion for a boolean attribute that disables AMP magic in images. Maybe we can even include the "don't optimize image in cache" behavior under this flag, which IIRC blocked Cloudinary from using plain |
Hi, any updates here? We are dying for this functionality. |
@@ -125,6 +125,10 @@ For the `<img>` tag in `HTML`, the `sizes` attribute is used in conjunction with | |||
|
|||
See [Responsive images with srcset, sizes & heights](https://amp.dev/documentation/guides-and-tutorials/develop/style_and_layout/art_direction) for usage of `sizes` and `srcset`. | |||
|
|||
**img-sizes** | |||
|
|||
Like `sizes` above, sets the [`sizes`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes) attribute on the underlying `img`. Unlike `sizes`, this does not change the width of the `amp-img` and is only used by the browser for picking which source to use from the `srcset`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is trying to say:
The img-sizes
attribute is used by the browser to pick up which source to from the srcset
, unlike the above sizes
attribute, it does not change with width of the amp-img
.
Hey @ampproject/wg-caching, these files were changed:
|
@@ -5210,6 +5210,8 @@ tags: { # <amp-img> | |||
attrs: { name: "object-fit" } | |||
attrs: { name: "object-position" } | |||
attrs: { name: "placeholder" } | |||
# Used to set sizes on the <img> without AMP's sizes behavior | |||
attrs: { name: "img-sizes" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please keep attributes alphabetized by attribute name
Any consideration around this? |
Just as a status update for this PR, we're exploring using a boolean attribute to disable the layout portion rather than propagating Will keep this PR open for further discussion, but it looks like we will be moving to an alternate approach. |
Closing this PR for now since the last comment indicates we'll be going with a different approach. Please reopen if needed. |
@archon810 #27083 was the alternative approach. Feel free to continue the discussion in #21736 which led to both this and that PR. |
This allows specifying the
sizes
to use for an<img>
, without gettingthe sizing behavior of
sizes
in AMP (which adds a width based on which media query matches).For #21736