You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
auto-sizes concrete object size ignoring natural dimensions is not implementable #9448
If I understand the intended behavior, I think it may not be implementable.
The goal of the spec was to get stable, predictable, non-racy loading behavior of auto-sizes images. Therefore the natural dimensions of the loaded image shouldn't cause a different image to be selected from srcset.
Consider a page like this:
<img loading=lazy sizes=auto srcset="a 2w, b 4w, c 8w, d 16w, ..." style="min-width: 1px">
The layout width before loading anything is 1px, so maybe the browser would select a. When that has loaded, the layout width changes to 2px, which could cause the browser to instead select b. And so on until the largest image has loaded.
The spec checks for 0px concrete object size ignoring natural dimensions and translates auto to 100vw. But since min-width can be used, the image's natural dimensions can still affect the layout size as shown above.
@emilio pointed out that the natural dimensions can affect outer elements, so it's not easy to determine the concrete object size ignoring natural dimensions without doing full layout twice.
So I think we need a different approach here. Maybe checking if there's a specified 'width' (or 'height' + 'aspect-ratio'), and if not use 100vw. Any other ideas?
There are cases even with the spec as-is where you could create a cyclic dependency. A path forward might be to add size containment for images with this behaviour.
Requiring strict dimensions (width or height + aspect-ratio) makes sense to me, and seems like it'd be easier to apply to existing content compared to contain: size. Would strict dimensions not be enough to prevent cyclic dependency?
As for height + aspect-ratio we'd need to disallow auto aspect-ratio (used by default) since it again makes layout width different between no image loaded and an image loaded.
When reading up on containment, I found (this link) which says size containment was used to solve a similar cycle issue for container queries, so there is some precedent with this approach. Is it possible to make auto sizes imply size containment so it doesn't need to be specified?
An alternative is to modify source selection to only go up in srcset options. The spec states that source selection is implementation-defined (selecting an image source), and Chromium currently implements this only-go-up behavior as an optimization to use larger images if they are available in the cache. This would not work for art direction, but that is probably best solved with other APIs (w3c/csswg-drafts#5889 has some info).
The timing for parsing sizes is not synced with layout, so making the former cause style changes (sometimes! sizes could resolve to something other than auto) also seems like a source of layout instability.
Size containment for images makes the height collapse to 0 if you only specify a width. It seems nice if we can avoid size containment as a requirement.
An alternative is to modify source selection to only go up in srcset options.
I think that still doesn't solve this, since the selection can go up to the largest image as noted in OP. Maybe you need width: 110% or zoom: 1.1 or similar (could be applied on :hover for example) in a fit-content parent.
Size containment for images makes the height collapse to 0 if you only specify a width. It seems nice if we can avoid size containment as a requirement.
We have inline-size containment, which doesn't. I guess for this we would need width containment, which would be a different, new thing.
Basically we need to ensure that image layout size doesn't rely on intrinsic width. We can check if intrinsic width will have any effect beforehand, and if so sub 100vw for auto (same behavior that happens today), or explicitly mandate that intrinsic width will not have any effect using containment.
Checking & falling back to 100vw feels harder, spec-wise. It feels like there are many edge cases (?). I like that it wouldn't break existing layouts that are relying on intrinsic widths, but don't like that when sizes=auto isn't doing what people think it's doing, the problem will often be invisible. Shrinking intrinsically-sized images to 0-width is more noticeable, for better or for worse.
We can check if intrinsic width will have any effect beforehand
Detecting if we've used an intrinsic size in layout gets into known hard layout problems very quickly. I suspect containment will be the best path forward.
An option (perhaps not a good one) is to add a UA-style similar to img[sizes=auto] { width: 100% } to provide a better default.
Could the concrete object size be adjusted so the natural dimensions don't change as the result of auto sizes selection? For example, if natural dimensions are available when selecting the auto sizes source, adjust the selected image's natural dimensions to match. For cases where layout depends on intrinsic sizes, this would only perform two cycles.
For the original issue of selecting all srcset options:
This approach would select "3x3.png 2w", resulting in a layout size of 3x3, then select "5x5.png 4w", which would still result in a layout size of 3x3 because the natural dimensions of 5x5.png would be adjusted to 3x3.
This approach would select "2x2.png 2w", resulting in a layout size of 4x4, then select "4x4.png 4w", which would still result in a layout size of 4x4 because the natural dimensions of 4x4.png would be adjusted to 2x2.
Maybe checking if there's a specified 'width' (or 'height' + 'aspect-ratio'), and if not use 100vw.
This would be easy to write in the spec but maybe less easy to implement, since specified style isn't generally available per @emilio. Is this feasible in Chromium and WebKit?
Detecting if we've used an intrinsic size in layout gets into known hard layout problems very quickly. I suspect containment will be the best path forward.
OK.
An option (perhaps not a good one) is to add a UA-style similar to img[sizes=auto] { width: 100% } to provide a better default.
This doesn't work because sizes can be omitted (equivalent to sizes=auto) and auto keyword can be used anywhere in sizes e.g. sizes="(min-width: 500px) 500px, auto" or sizes="auto, 50vw" (provide fallback for older browsers).
@progers two requests per image which hasn't been acceptable in the past.
in the sizes parsing algorithm: if the computed value of contain is not size, then map auto to 100vw. Otherwise use the concrete object size (even if it's 0).
How does that work? Does sizes parsing update style sync? That seems wrong. Or is it using an existing style (whatever we were laid out with?).
I think we should probably enforce that sizes="auto"forces (not only maps to) contain: size. I don't think we have precedents for !important attribute mappings, but they should be implementable. Otherwise a magic adjustment seems not-hard-to-do either.
I was worried that even though ~80% of <img>s with srcset+w have height and width HTML attributes, not all of those attributes would be accurate representations of the aspect ratio of the eventually-loaded resource. This worry was unfounded; on mobile (where accuracy is worse), 75% are as right as they can be, and 98% are accurate to within a couple of percentage points.
in the sizes parsing algorithm: if the computed value of contain is not size, then map auto to 100vw. Otherwise use the concrete object size (even if it's 0).
How does that work? Does sizes parsing update style sync? That seems wrong. Or is it using an existing style (whatever we were laid out with?).
The latter. Should we have a note in the spec?
I think we should probably enforce that sizes="auto"forces (not only maps to) contain: size. I don't think we have precedents for !important attribute mappings, but they should be implementable. Otherwise a magic adjustment seems not-hard-to-do either.
Hmm interesting, yeah that would be an alternative and then we don't need to check computed value.
Is it helpful to have stricter syntax constraints around auto so that we can use a selector in the UA stylesheet like:
Remember that sizes can be defined on any of the <source> elements in the <picture> element.
I don't think the selected source is exposed to CSS selectors, so it might be hard to write a UA rule?
Ideally the applied style rules don't change from initial layout. Resource selection for lazy images happens later, so having that affect UA style isn't good.
We can require sizes=auto on the img for auto-sizes to work on source (and then sizes can be optional on source).
@eeeps thanks, yeah agreed it's a bit surprising. Adding contain-intrinsic-size seems ok, what do others think? 300x150 seems better to match the default size of most other elements (canvas, video, iframe...). That it's not most common is ok, we want authors to set the width/height.
Activity
bfgeek commentedon Jun 21, 2023
There are cases even with the spec as-is where you could create a cyclic dependency. A path forward might be to add size containment for images with this behaviour.
[1] E.g. https://www.software.hixie.ch/utilities/js/live-dom-viewer/?saved=11811
Here the width of the outer box is the natural-size of the image.(Say 200px). Then the image 50% resolves against this, and becomes 100px.
We select an image based off this with a natural-size of say 400px, and wash-rinse-repeat.
Ian
zcorpan commentedon Jun 22, 2023
So require
contain: sizeto enable auto-sizes, else resolveautoto100vw?yoavweiss commentedon Jun 22, 2023
Requiring strict dimensions (
widthorheight+aspect-ratio) makes sense to me, and seems like it'd be easier to apply to existing content compared tocontain: size. Would strict dimensions not be enough to prevent cyclic dependency?zcorpan commentedon Jun 22, 2023
Percentage widths as in @bfgeek's example is still problematic. Do you mean disallow percentages with "strict dimensions"?
zcorpan commentedon Jun 22, 2023
As for
height+aspect-ratiowe'd need to disallow auto aspect-ratio (used by default) since it again makes layout width different between no image loaded and an image loaded.progers commentedon Jun 22, 2023
When reading up on containment, I found (this link) which says size containment was used to solve a similar cycle issue for container queries, so there is some precedent with this approach. Is it possible to make auto sizes imply size containment so it doesn't need to be specified?
An alternative is to modify source selection to only go up in srcset options. The spec states that source selection is implementation-defined (selecting an image source), and Chromium currently implements this only-go-up behavior as an optimization to use larger images if they are available in the cache. This would not work for art direction, but that is probably best solved with other APIs (w3c/csswg-drafts#5889 has some info).
zcorpan commentedon Jun 22, 2023
The timing for parsing
sizesis not synced with layout, so making the former cause style changes (sometimes!sizescould resolve to something other thanauto) also seems like a source of layout instability.Size containment for images makes the height collapse to 0 if you only specify a width. It seems nice if we can avoid size containment as a requirement.
I think that still doesn't solve this, since the selection can go up to the largest image as noted in OP. Maybe you need
width: 110%orzoom: 1.1or similar (could be applied on:hoverfor example) in a fit-content parent.bfgeek commentedon Jun 22, 2023
One additional thing is that it's possible to create these cyclic dependencies with "auto" width/heights as well.
E.g. https://www.software.hixie.ch/utilities/js/live-dom-viewer/?saved=11816 (the final width of the images are dependent on each others height).
eeeps commentedon Jun 22, 2023
@zcorpan:
We have inline-size containment, which doesn't. I guess for this we would need width containment, which would be a different, new thing.
eeeps commentedon Jun 23, 2023
Basically we need to ensure that image layout size doesn't rely on intrinsic width. We can check if intrinsic width will have any effect beforehand, and if so sub
100vwforauto(same behavior that happens today), or explicitly mandate that intrinsic width will not have any effect using containment.Checking & falling back to 100vw feels harder, spec-wise. It feels like there are many edge cases (?). I like that it wouldn't break existing layouts that are relying on intrinsic widths, but don't like that when sizes=auto isn't doing what people think it's doing, the problem will often be invisible. Shrinking intrinsically-sized images to 0-width is more noticeable, for better or for worse.
bfgeek commentedon Jun 23, 2023
Detecting if we've used an intrinsic size in layout gets into known hard layout problems very quickly. I suspect containment will be the best path forward.
An option (perhaps not a good one) is to add a UA-style similar to
img[sizes=auto] { width: 100% }to provide a better default.progers commentedon Jun 25, 2023
Could the concrete object size be adjusted so the natural dimensions don't change as the result of auto sizes selection? For example, if natural dimensions are available when selecting the auto sizes source, adjust the selected image's natural dimensions to match. For cases where layout depends on intrinsic sizes, this would only perform two cycles.
For the original issue of selecting all srcset options:
This approach would select "3x3.png 2w", resulting in a layout size of 3x3, then select "5x5.png 4w", which would still result in a layout size of 3x3 because the natural dimensions of 5x5.png would be adjusted to 3x3.
For the width: 200%, fit-content parent issue:
This approach would select "2x2.png 2w", resulting in a layout size of 4x4, then select "4x4.png 4w", which would still result in a layout size of 4x4 because the natural dimensions of 4x4.png would be adjusted to 2x2.
zcorpan commentedon Jun 26, 2023
I wrote
This would be easy to write in the spec but maybe less easy to implement, since specified style isn't generally available per @emilio. Is this feasible in Chromium and WebKit?
@eeeps
Good point, we could allow
contain: inline-sizeif writing mode is horizontal.@bfgeek
OK.
This doesn't work because
sizescan be omitted (equivalent tosizes=auto) andautokeyword can be used anywhere insizese.g.sizes="(min-width: 500px) 500px, auto"orsizes="auto, 50vw"(provide fallback for older browsers).@progers two requests per image which hasn't been acceptable in the past.
progers commentedon Jun 26, 2023
Don't we still have the cycle issues with size containment approaches that don't constrain all axes?
23 remaining items
emilio commentedon Jul 5, 2023
How does that work? Does
sizesparsing update style sync? That seems wrong. Or is it using an existing style (whatever we were laid out with?).I think we should probably enforce that
sizes="auto"forces (not only maps to)contain: size. I don't think we have precedents for!importantattribute mappings, but they should be implementable. Otherwise a magic adjustment seems not-hard-to-do either.eeeps commentedon Jul 5, 2023
Another research note:
I was worried that even though ~80% of
<img>s withsrcset+whaveheightandwidthHTML attributes, not all of those attributes would be accurate representations of the aspect ratio of the eventually-loaded resource. This worry was unfounded; on mobile (where accuracy is worse), 75% are as right as they can be, and 98% are accurate to within a couple of percentage points.zcorpan commentedon Jul 6, 2023
The latter. Should we have a note in the spec?
Hmm interesting, yeah that would be an alternative and then we don't need to check computed value.
Is it helpful to have stricter syntax constraints around
autoso that we can use a selector in the UA stylesheet like:edit: should be
:is()zcorpan commentedon Jul 6, 2023
@eeeps thank you!
johannesodland commentedon Jul 6, 2023
Remember that
sizescan be defined on any of the<source>elements in the<picture>element.I don't think the selected source is exposed to CSS selectors, so it might be hard to write a UA rule?
zcorpan commentedon Jul 6, 2023
@johannesodland indeed! Good call.
Ideally the applied style rules don't change from initial layout. Resource selection for lazy images happens later, so having that affect UA style isn't good.
We can require
sizes=autoon theimgfor auto-sizes to work onsource(and thensizescan be optional onsource).Force size containment for auto-sizes
zcorpan commentedon Jul 6, 2023
PR: #9493
eeeps commentedon Jul 6, 2023
My own feelings and a tiny, informal poll lead me to believe that disappearing images without
width+heightwill be worrying and confusing to authors who applysizes=autowithout understanding thecontain: sizemechanism, or the reasoning behind it.One thing we might be able to do to help here is add a
contain-intrinsic-sizeto the UA stylesheet rule set. I guess this is equivalent to changing the default object size for these elements. I would suggest either300px 150pxto be consistent with<video>or200px 200pxto capture the most common image aspect ratio (1:1) at the (rounded) most common pixel count.zcorpan commentedon Jul 7, 2023
@eeeps thanks, yeah agreed it's a bit surprising. Adding
contain-intrinsic-sizeseems ok, what do others think? 300x150 seems better to match the default size of most other elements (canvas, video, iframe...). That it's not most common is ok, we want authors to set the width/height.Force size containment for auto-sizes
Force size containment for auto-sizes
🐛 Fix: remove sizes="auto" prop what was used for lazysizes.js in old…
Force size containment for auto-sizes