Skip to content

auto-sizes concrete object size ignoring natural dimensions is not implementable #9448

Closed

Description

@zcorpan

Feedback from @progers in https://chromium-review.googlesource.com/c/chromium/src/+/4520266/comment/d6012d27_30cf4ea2/ about concrete object size ignoring natural dimensions introduced in #8008

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?

cc @tcaptan-cr @yoavweiss

Activity

  1. added
    topic: img
    on Jun 21, 2023
  2. bfgeek commented on Jun 21, 2023

    @bfgeek
    Member

    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

  3. zcorpan commented on Jun 22, 2023

    @zcorpan
    MemberAuthor

    So require contain: size to enable auto-sizes, else resolve auto to 100vw?

  4. yoavweiss commented on Jun 22, 2023

    @yoavweiss
    Contributor

    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?

  5. zcorpan commented on Jun 22, 2023

    @zcorpan
    MemberAuthor

    Percentage widths as in @bfgeek's example is still problematic. Do you mean disallow percentages with "strict dimensions"?

  6. zcorpan commented on Jun 22, 2023

    @zcorpan
    MemberAuthor

    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.

  7. progers commented on Jun 22, 2023

    @progers

    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).

  8. zcorpan commented on Jun 22, 2023

    @zcorpan
    MemberAuthor

    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.

  9. bfgeek commented on Jun 22, 2023

    @bfgeek
    Member

    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).

  10. eeeps commented on Jun 22, 2023

    @eeeps
    Contributor

    @zcorpan:

    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.

  11. eeeps commented on Jun 23, 2023

    @eeeps
    Contributor

    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.

  12. bfgeek commented on Jun 23, 2023

    @bfgeek
    Member

    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.

  13. progers commented on Jun 25, 2023

    @progers

    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:

    <img loading=lazy sizes=auto style="min-width: 1px" srcset="3x3.png 2w, 5x5.png 4w, 9x9.png 8w, ...">
    

    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:

    <div style="width: fit-content;">
      <img loading=lazy sizes=auto style="min-width: 1px; width: 200%;" srcset="2x2.png 2w, 4x4.png 4w, ...">
    </div>
    

    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.

  14. zcorpan commented on Jun 26, 2023

    @zcorpan
    MemberAuthor

    I wrote

    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?

    @eeeps

    We have inline-size containment, which doesn't. I guess for this we would need width containment, which would be a different, new thing.

    Good point, we could allow contain: inline-size if writing mode is horizontal.

    @bfgeek

    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.

  15. progers commented on Jun 26, 2023

    @progers

    Don't we still have the cycle issues with size containment approaches that don't constrain all axes?

  16. 23 remaining items

  17. emilio commented on Jul 5, 2023

    @emilio
    Contributor
    • 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.

  18. eeeps commented on Jul 5, 2023

    @eeeps
    Contributor

    Another research note:

    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.

  19. zcorpan commented on Jul 6, 2023

    @zcorpan
    MemberAuthor
    • 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:

    img:is([sizes="auto" i], [sizes^="auto," i]) { contain: size !important; }
    

    edit: should be :is()

  20. zcorpan commented on Jul 6, 2023

    @zcorpan
    MemberAuthor

    @eeeps thank you!

  21. johannesodland commented on Jul 6, 2023

    @johannesodland

    Is it helpful to have stricter syntax constraints around auto so that we can use a selector in the UA stylesheet like:

    img:has([sizes="auto" i], [sizes^="auto," i]) { contain: size !important; }
    

    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?

    <picture>
      <source srcset="..." sizes=2400px>
      <img sizes=auto loading=lazy>
    </picture>
  22. zcorpan commented on Jul 6, 2023

    @zcorpan
    MemberAuthor

    @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=auto on the img for auto-sizes to work on source (and then sizes can be optional on source).

  23. added a commit that references this issue on Jul 6, 2023
    c40185d
  24. zcorpan commented on Jul 6, 2023

    @zcorpan
    MemberAuthor

    PR: #9493

  25. eeeps commented on Jul 6, 2023

    @eeeps
    Contributor

    My own feelings and a tiny, informal poll lead me to believe that disappearing images without width + height will be worrying and confusing to authors who apply sizes=auto without understanding the contain: size mechanism, or the reasoning behind it.

    One thing we might be able to do to help here is add a contain-intrinsic-size to the UA stylesheet rule set. I guess this is equivalent to changing the default object size for these elements. I would suggest either 300px 150px to be consistent with <video> or 200px 200px to capture the most common image aspect ratio (1:1) at the (rounded) most common pixel count.

  26. zcorpan commented on Jul 7, 2023

    @zcorpan
    MemberAuthor

    @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.

  27. added a commit that references this issue on Sep 5, 2023
    e90b823
  28. added a commit that references this issue on Nov 30, 2023
    193c67d
  29. added a commit that references this issue on Mar 10, 2025
    bad27e3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: img

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions