-
Notifications
You must be signed in to change notification settings - Fork 694
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
[css-conditional] [css-contain] srcset
and sizes
interaction with container queries
#5889
Comments
@yoavweiss @cramforce WDYT? |
@chrishtr The use case I was mentioning earlier basically the opposite. Containers often determine the width of their child. So what I want is: <main sizes="50vw">
<img srcset="1.avif 1920w, 2.avif 640w"><!-- no redundant sizes attribute on children of main -->
<img srcset="1.avif 1920w, 2.avif 640w">
<img srcset="1.avif 1920w, 2.avif 640w">
</main> |
I completely agree that this would be a very powerful and useful feature. My understanding was that responsive image sources are resolved before styles – making this reference impossible. But I'm not a browser engineer, and would love to discover that I'm wrong about this. :) |
The CSS Working Group just discussed The full IRC log of that discussion<astearns> topic: [css-conditional] [css-contain] `srcset` and `sizes` interaction with container queries<astearns> github: https://github.com//issues/5889 <dlibby> una: next issue is related - how to deal with responsive images in container queries <dlibby> una: would like to explore the solution space, since it seems like it'll be pretty common <dlibby> una: adding a ??? to srcset, setting images on each compute, since they come from server <bkardell_> q+ <dlibby> una: how should container queries interact, and background images are also a consideration <bkardell_> how would this work with preloading? https://web.dev/preload-responsive-images/ <dlibby> s/???/new syntax/ <dlibby> una: do something simliar to media queries but seems important to consider for container queries <astearns> ack bkardell_ <dlibby> bkardell: preloading - see linked article. you can't know what to load until later <dlibby> una: yes, tricky because working at compute time, not sure how this works for viewport size <dlibby> emilio: you can estimate what your viewport is <dlibby> emilio: you can make a decent guess in iframes <dlibby> emilio: you can't really do this in containers <dlibby> iank: relatively sure that gecko and chromium reference image via background: url(foo), but can have multiple decls <dlibby> iank: UA load images after computed value time so you don't load lots of things in the background <dlibby> iank: it's a performance tradeoff <dlibby> emilio: <img> tags are a little more important as user wants to immediatly see it <dlibby> florian: worried that this could make slow connections slower <dlibby> florian: you normally don't get all resources at the first request, progressive modification, which happens more on slow networks. as this happens container can change, possible to congest/thrash network <dlibby> florian: that sounds unfortunate side effect <dlibby> una: if youre using container queries and there's a hero, or in a card, or sidebar, those are different <dlibby> una: if container queries allow for composable styles in components, I can't imagine that we can't have different image sizes based on the size <bkardell_> not just image sizes maybe different art direction too? <dlibby> una: not sure if it is slower to load a bunch of images first, or layout first then download other images <bkardell_> I suggest we send this to RICG :) <dlibby> florian: use case makes sense - should and can are different questions <dlibby> florian: if you have multiple small parts, many changes could be happening, and as the stylesheet comes in over network, you might end up loading all the images <dlibby> florian: ending up there sounds bad, but perhaps there's another approach <dlibby> myles: problem appears endemic to container queries <dlibby> florian: most thrashing doesn't hit the network for each change <dlibby> una: perhaps we can scope to initial load to avoid this problem - maybe solves the majority use cases <dlibby> emilio: not sure - if you resize the window, you'd bet stuck in possible suboptimal state <fantasai> I was about to say the same thing also <dlibby> myles: I don't think visual layout should be permanently affected based on network speed <dlibby> myles: CSS shouldn't be sensitive to timing of bytes on the wire <dlibby> una: might be misunderstanding. when we figure out the layout size keep that image <dlibby> una: refresh page would get different images <dlibby> emilio: layout while stylesheets are outstanding is common. stylesheet in body can affect the page layout, this should be taken into account <dlibby> iank: the place where this functionality may make sense is html layout. some components are already stateful, but this should be carefully considered <dlibby> e.g. a oneshot load - how does this affect the html layout, something to consider <nicole> a balance btw not requesting unnecessary images and not waiting to load images until css is done parsing <dlibby> myles: maybe specify behavior, UAs can use different heuristics <dlibby> myles: CSSWG doesn't have to specify when loads are triggered <dlibby> iank: option for this would be adding to Image specificiation (perhaps additional data on <img>) once this has been laid out and it reaches 'stable' state, then UA can fire off request <dlibby> ???: problem with containment in general? <fantasai> s/????/nicole/ <fantasai> s/???/nicole/ <dlibby> emilio: perhaps one approach is to define what the behavior should be (lazy or something). just a gneeral problem with containment. <dlibby> s/containment/container queries/ <dlibby> iank: reasonably easy to polyfill, prototype, iterate to discover 'good behavior' <dlibby> iank: don't have to nail down spec text right now, we can try to find optimal via prototyping <dlibby> bkardell: might be good to ask folks on ??? CG <jensimmo_> yes, do ask Mat. <dlibby> bkardell: to the point of wanting different size, you might want different art directives(?) <dlibby> astearns: is it possible to load different images? <dlibby> iank: yes this is possible to get different urls <dlibby> astearns: that is for separate declarations, but this is for a single declaration <dlibby> iank: i was referring to img tag attribute, there's nothing in CSS currently that allows for this <dlibby> nicole: having it in CSS should be the same problem as having this specified in the html itself. likely has same characteristics <dlibby> una: i recognize that this is probably for specification in HTML <dlibby> una: glad bkardell brought up different images - third party service could provide different crops <dlibby> bkardell: precisely, that's what I wanted to be recognized <dlibby> astearns: what else would you like to get from the group? <dlibby> una: this conversation basically <dlibby> astearns: perhaps prototyping per iank is the way forward, thanks for bringing it up |
Can't speak to the implementation side, but I like this idea, @una. Just like with container queries, I think the container size is what we typically want to know for specifying image sources. Also, in the off chance that |
This seems really exciting for art direction (e.g., However, part of the goal of For art direction... maybe that's a high performance price we have to pay to enable art direction within responsive components, which seems important. But for I can maybe see a path forward to instant loading here if the queried container, itself, has a
But reading, writing, and maintaining |
Under this proposal, the selection of the size would indeed depend on style and layout. Which would mean the loading of these images would be significantly delayed. The same problem occurs with However, there are real advantages to this pattern, and there may be many good use cases where the image is not critical for first-load rendering time. |
The natural conflict between prioritizing the ability to preload images before scripts/styles load and the desire to optimize the image loaded based on layout was always the trickiest thing to solve for responsive images. What I see above are basically two different use cases, each of which (as far as I can tell) would need to carry different assumptions about how that prioritization works. 1. Preload images based on a container width To make this work, you would need to let the browser know what the container width is intended to be before layout is calculated, by applying something like If the image does include a 2. Lazy load images based on container width In the event that image markup relies on a container query or container relative measurements in it's I think those are acceptable constraints/tradeoffs for the benefits that come from being able to use these new tools in building reusable interface elements that respond to their layout context. |
One loves to see it. I think @joemcgill nailed my thoughts on this, but one thing worries me a little: “lazy load images based on container width” feels like both a sensible default for omitted What worries me is that I’m a little concerned that the lazyloading pattern would end up telephone-gamed into “we don’t need |
I've thought the same about sizes tbh. When we have aspect-ratio on hand and available, plus loading=lazy, then we can safely let the browser decide which asset to pull because it already knows the width it actually needs all by itself without hinting. |
belatedly catching up... I agree with @eeeps about the inherent tradeoff here, about the ability to go with nested I similarly agree with @chrishtr that for anything outside any initial viewport, lazy loading it has no performance cost and should be encouraged. But, that leaves us with "images inside a container query that are in an initial viewport of some form factors".
I'm not sure what the answer is, but seems like we need one. |
I don't think it's reasonable. Maintaining an accurate set of inline styles for the initial viewport is a very hard task and I've never met a team who does it well. (Sidenote: it is very annoying to see this recommandation in performance tooling as it is quite unrealistic) Currently, responsive images can be fetched before the external CSS has been parsed. Changing this behaviour because of the introduction of container queries seems like a breaking change to me. I think browsers could keep the existing behaviour if no condition in sizes targets a container. Using the draft syntax: <!-- Starts fetching with only HTML known -->
<img srcset="elva-fairy-480w.jpg 480w,
elva-fairy-800w.jpg 800w"
sizes="(max-width: 600px) 480px,
800px"
src="elva-fairy-800w.jpg"
alt="Elva dressed as a fairy"> <!-- Waits for external CSS to choose an image to fetch -->
<img srcset="elva-fairy-480w.jpg 480w,
elva-fairy-800w.jpg 800w"
sizes="container(max-width: 600px) 480px,
800px"
src="elva-fairy-800w.jpg"
alt="Elva dressed as a fairy"> |
I think this is reasonable to aim for a first layout that runs fast enough. Recent features such as This leaves the question of whether it is reasonable to expect the relevant CSS to be fast to arrive. This would require critical styles to be inlined into the HTML, or something like Early Hints, if "fast to arrive" means no extra round-trips. |
A few of us are planning a breakout to discuss this topic/use cases on Thursday March 11 at 9am ET, the invitation for anyone intersted in joining that is open.. Please reach out to @una or myself and we'll share a meeting link/calendar invite. |
Informal breakout meeting notes: https://docs.google.com/document/d/1AgCqgnPymQPzwdl0-BgNd22Sc01xXebjVezkme2ofu4/edit?usp=sharing
|
@una can you export the google doc into an archival format and send it as an attachment to www-archive? Then we can have a more permanent link to add here. |
I have a hunch that many (most?) Auto sizes might be a net win for performance, even if it delays image loading until after layout. Call it |
Fwiw I don't think the use case of "request an asset as fast as possible" is anything like as important as "load the right asset and avoid wasting potentially hundreds of Kb of inappropriate image data". The part of the feature that allows for early http requests is far less impactful for any page of a realistic size than getting the more appropriate asset for the context in use. Given that the majority of the time "saved" by this behaviour is actually dns lookup time and/or connection establishment... those are mostly redundant? A huge portion of images are going to be on the same domain or a CDN that don't incur an extra lookup, or over http2/3 which don't suffer the same connection overhead. Unless I'm missing something? I'd happily ditch the "load a thing as soon as possible" for a "load the right thing". |
Ohh, on that note, if all the potential sources for an I think this might be a common case. Eg images on https://www.theguardian.com/uk are on a different origin to the page, but the images themselves are all on the same origin as each other. |
Ugh, unless the image turns out to be |
True, though I'd think that's not any different than how that scenario would be today anyway? Also; don't we already provide a mechanism for doing quick DNS lookups when you know a domain will be needed later in the page? https://developer.mozilla.org/en-US/docs/Web/Performance/dns-prefetch I'd say that it's easier to know that ahead of time as a page author or content editor, than it is to know what sizes an asset will render at once loaded - in order to provide a sizes property value that's actually correct. Or to be more plain: it's simpler, more reliable, and easier to automate what domains the assets of a page use and bang a dns-prefetch in the head for those, than it is to generate real values for the sizes property at different breakpoints. |
One of the core design constraints for Tying layout-aware responsive image loading to lazy-loading makes sense, as we're not adding any additional delay. Tying it to container queries makes less sense, from this point of view. But it would be nice to quantify both:
I'm doing some analysis on ① as part of the HTTP Almanac this year, and am very excited about it. I have some A/B demos that investigate ② synthetically, but don't have good ideas about how to extend that analysis to the web, as it exists, as a whole. |
Also see: whatwg/html#8007 |
I’m a bit late to this discussion, and I’m not certain how much of a contribution this will be to finding a solution, but I wrote an article about the challenge and my feeling that maybe this is the time when we should revisit some of the assumptions that led us to the responsive images syntax. |
Has any of the discussion for this considered looking at the problem from a different angle?
Introduce that constraint and introduce an attribute that can provide that aspect ratio and suddenly for initial viewport rendering you can speculatively download whatever is smallest. If once layout happens the image hasn't been downloaded yet, then you can abort the pending request and transparently upgrade to the appropriate sized image. Doesn't even have to be the smallest defined image in the srcset. Many of the major performance problems with bringing multiple files over the wire have been greatly reduced by HTTP/2 & /3 - it's really just the latency of 'until the image is downloaded' that remains a bother for the first paint and perceived performance. And having a stop-gap candidate that is explicitly marked as such, with the expectation that it will be a very small temporary-use low-quality file to transfer, could go a long way to solving this issue. |
@rjgotten I'm not sure it's acceptable to load lowest res image first and after layout load the appropriate image. I think that would cause "double download" of all images in the general case, which is generally seen as a bug. Ideally only the appropriate image is loaded, without blocking on layout. @grigs For new image formats, as far as I can tell the art direction use case is not handled. With the bookstore demo also it's a completely different image, not just a crop. I suppose an image file could in theory contain different images though. |
@zcorpan Isn't the |
@pc-erin yes, but it's limited to media queries. |
Not that it matters for your greater point, but the bookstore demo is the same image with CSS effects applied to it in different contexts.
So the art direction use case would still require It seems that developers have had the most trouble using Web Almanac says |
But it's complicated; almost all of those But idk 80% "flexible image use case" vs 20% "art direction use case" seems like a reasonable floor, given the numbers we have... especially considering the anecdotal evidence that many |
In the "Picture" component, i have to pass "SourceSettings" because this issue has not been solved yet w3c/csswg-drafts#5889
With container queries, the way developers write responsive styles changes from querying the global page for information to querying for a parent with containment for information. This shifts responsive paradigms. One case I can forsee is the need to query the parent for sizing and placement of its children. This includes
srcset
andsizes
for responsive imagesSee also, the need for
cw
andch
units: #5888Would the following be able to work with container queries instead of global queries? (ignore syntax, just trying to show the idea):
The text was updated successfully, but these errors were encountered: