-
Notifications
You must be signed in to change notification settings - Fork 14
A more elegant and easier to use solution #16
Comments
👍 Big fan of this proposal, particularly if the CSS ends up in the browser's UA stylesheet so that old and new sites gain aspect ratios on their images with little or no work. |
I'm into it. Seems like a fantastic idea. I like blogging so I blogged it. |
This is such a great idea! Hopefully, this can help with the proposed HTML Speaking of iframes, would it make sense to include |
I agree. |
Is it safe to assume that the aspect ratio computed from the image’s |
As much as I like that proposal: <div style="width:200px">
<img width="960" height="572" src="image-960x572.jpg" style="width:100%">
</div> https://jsfiddle.net/tpgn143f/4/ According to this proposal, the height will be calculated from the aspect ratio. |
I am concerned about this idea. For years since we have been building responsive fluid web layouts declaring width and height attributes on "img" elements could be problematic when the actual size of an image as displayed on the page is basically unknown and fluid. Unless the developer takes the time to set up their CSS correctly, images could become squished or stretched if the width and height attributes are present on the image tag. Plus, sometimes we want our images to be displayed at a different aspect ratio then actual aspect ratio of the image itself (with the help of object-fit). Finally, there is the "picture" element and art directing of images, sometimes we want a square-crop on mobile and landscape crop on desktop, these are two very different aspect ratios. I guess I am saying many sites and developers have been omitting the width and height attribute on images for a while. We would now have to make sure everyone put them back in, and then it is not always appropriate to do so. |
@michaelwhyte If I understand this correctly, this rule would have no effect on images without width and height attributes, leaving their behaviour unchanged. |
@jayhostan True, it would be necessary to set the CSS height to Also, it would not be possible to polyfill this without an additional custom-property But yes, I see that the proposal has the big advantage that you don't need an additional attribute. |
@ojanvafai @loonybear @dvoytenko - Thoughts? |
Also, @kinu & @domfarolino for lazy loading and range request downloads. |
Is attr() no longer experimental outside of pseudo content? |
The aspect ratio calculated from the
If we decide to go down this route, the mapping will probably be handled at the CSS presentational hint layer (in C++) where presentational attribute-to-CSS mappings happen, rather than using CSS syntax in the UA style sheet directly; as far as I'm aware we still don't have implementations of attr() outside |
Can you explain this more? Why does it require the author to explicitly css the width/height CSS properties to auto to hit the issue?
How would this work? It sounds like you're saying the aspect-ratio CSS property would not do anything to elements that have an intrinsic aspect ratio once loaded? |
Also, for others, here's the previous CSSWG discussion about this https://lists.w3.org/Archives/Public/www-style/2019Mar/0015.html and #4 (comment) seems like relevant data depending on the explicitly set to auto thing. |
@ojanvafai The aspect-ratio property only has meaning when one of height or width is set to auto - then it adjusts that automatic height/width to fit the aspect ratio. I've set up a test fiddle: https://jsfiddle.net/Sora2455/Lw5mob1d/. This change will only affect the last example, if I've understood this correctly. |
Big fan of this fantastic idea! |
Discussing this during in-betweens at the csswg f2f it seemed to me that @tabatkins had thoughts here that he might express? One thing that I was asking several people about is that it seems clear that there is a rasterizing step here and a change of the reported |
@yoavweiss @ojanvafai @jensimmons If the
|
The bits that are not clear to me about this solution is how it would be something deployable assuming that not all browsers out there support the new aspect-ratio scheme. e.g. looking at https://steady-jellyfish.glitch.me/, it seems like a non-supporting browser (e.g. all of them right now) will display responsive images very differently depending on their set width and height values. @fantasai - am I missing something here? Or were your comments regarding compat referring to supporting browsers with legacy content, and not compat issues with new content and legacy browsers? |
@yoavweiss Yes, the backward compatibility support is the biggest issue I see with this approach as well. |
Because the
There are use cases for
It would not regress any pages except the ones satisfying both conditions in #16 (comment) so unless you think that's a significant Web-compat problem, then there's no reason it can't be deployed. And that Web-compat problem has a solution as already described.
There would be no changes to that page under this proposal.
Maybe? I'm not understanding your concerns.
With legacy content. I don't think there's an issue with new content vs legacy content, either. Again the only cases where they would render differently are images that satisfy both conditions a) and b) in #16 (comment) And again, if this is really a problem we can address it. I would hope that new pages are authored with correct values for their width/height attributes, though.
There is no proposal here to change the reported .naturalHeight and .naturalWidth. If they should match the Again for everyone who seems to be confused: there is no proposal to change existing mappings of width/height HTML attributes to CSS width/height properties as defined right now in HTML. There is only an additional mapping of width/height to an aspect ratio so that a replaced element can have an aspect ratio factored into the layout algorithms before it is loaded. |
What that test page is showing is that including How would you advise developers to build new pages that work for the new scheme (but under which, the image's layout dimensions are known at initial layout time), as well as for legacy browsers (even if there, we won't get the "no reflow" benefits of the proposal)? Can you come up with an example of a responsive image layout where |
The same way many already do now: including
Yes. Setting |
There seems to be a lot of confusion. There are three combinations of code — and all of them will get the exact same layout result after adding
This code won’t be affected by the proposed change at all, because without
By adding Yes, Authors will need to be sure to include Browsers that implement the proposed code will get a performance gain. Older browsers will not have the performance gain. All browsers will render the layout exactly the same.
In a future where we add Our proposal does not change this behavior. |
In the past, we thought of So what happens if I lie, as the Author? How is What if I tell the browser the file is 800x600, when really it's 400x300? Well, nothing. Because the math works out. The aspect ratio is still 1.333:1. Everything about how this situation is handled is outside the scope of the proposed change. What if I tell the browser the file is 300x300, when it's really 400x300? Well, then the aspect ratio gets calculated as 1:1, when the image's actual aspect ratio is not that. But that's exactly what was going to happen with This is a very tiny change. It's grabbing the width and height attributes to use in layout calculations before the image downloads, and passing that information along via an idea that can be expressed as |
If we need position a 400x300 image inside a 800px How could we make it by
I guess the layout size of |
@cathiechen Your example code is # 3 in Jen's comment above. As she says, it is broken now and in the future. If you want it to work, you need pattern # 1 or # 2, which will work now and in the future. |
Layout would be calculated in the future (with this proposed change) exactly the same way it is calculated today.
You get an image that's stretched to be 100% of 800px — so 800 px wide. And yes, 300px tall. Because that's what you've coded. That happens today. That would still happen in the future, under this proposed performance enhancement. It would just happen slightly faster. If you want the image to be aspect-ratio constrained, then you need to use code that will do that. For example:
This layout will work today in exactly the same manner as it will with this performance enhancement. It'll just happen a tiny tiny bit faster with a performance improvement.
or all sorts of other things.... there are many ways too accomplish the layout you desire. But this issue is not about the This issue is about using a tweak to the browser's rendering engine to make What we are discussing affects the performance of layout. It does not affect the results of layout. |
To be clear, if the attributes are in What it does not handle is having multiple sources with different aspect-ratios depending on the viewport size or what not, which is the comment above. As @annevk mentioned that probably needs attributes in |
So showing a 16:9 on landscape screens and a 1:1 on portrait screens wouldn't work. Would it work if you dynamically changed the width and height attrs of the img tag at the right breakpoints in this scenario? |
Yes. As long as the attributes are on the |
Unfortunately, when |
As suggested, I made a new proposal for aspect-ratio in regards to art-direction with multiple sources, each having their own aspect-ratio. |
…height HTML attributes, a=testonly Automatic update from web-platform-tests Compute img aspect ratio from width and height HTML attributes As per this intent to implement: https://groups.google.com/a/chromium.org/forum/?fromgroups#!topic/blink-dev/hbhKRuBzZ4o Spec: WICG/intrinsicsize-attribute#16 Bug: 979891 Change-Id: I0f9ffa1584fa12a41393ef05daffb7238c97e990 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1790472 Commit-Queue: Christian Biesinger <cbiesingerchromium.org> Reviewed-by: Emilio Cobos Álvarez <emiliochromium.org> Auto-Submit: Christian Biesinger <cbiesingerchromium.org> Cr-Commit-Position: refs/heads/master{#699465} -- wpt-commits: 86ff86de45a67eccc781b84f50ab261c18aad5cf wpt-pr: 18945 UltraBlame original commit: e8962724f6cc584847fedccd8c0912b6bd00dab6
…height HTML attributes, a=testonly Automatic update from web-platform-tests Compute img aspect ratio from width and height HTML attributes As per this intent to implement: https://groups.google.com/a/chromium.org/forum/?fromgroups#!topic/blink-dev/hbhKRuBzZ4o Spec: WICG/intrinsicsize-attribute#16 Bug: 979891 Change-Id: I0f9ffa1584fa12a41393ef05daffb7238c97e990 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1790472 Commit-Queue: Christian Biesinger <cbiesingerchromium.org> Reviewed-by: Emilio Cobos Álvarez <emiliochromium.org> Auto-Submit: Christian Biesinger <cbiesingerchromium.org> Cr-Commit-Position: refs/heads/master{#699465} -- wpt-commits: 86ff86de45a67eccc781b84f50ab261c18aad5cf wpt-pr: 18945 UltraBlame original commit: e8962724f6cc584847fedccd8c0912b6bd00dab6
…height HTML attributes, a=testonly Automatic update from web-platform-tests Compute img aspect ratio from width and height HTML attributes As per this intent to implement: https://groups.google.com/a/chromium.org/forum/?fromgroups#!topic/blink-dev/hbhKRuBzZ4o Spec: WICG/intrinsicsize-attribute#16 Bug: 979891 Change-Id: I0f9ffa1584fa12a41393ef05daffb7238c97e990 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1790472 Commit-Queue: Christian Biesinger <cbiesingerchromium.org> Reviewed-by: Emilio Cobos Álvarez <emiliochromium.org> Auto-Submit: Christian Biesinger <cbiesingerchromium.org> Cr-Commit-Position: refs/heads/master{#699465} -- wpt-commits: 86ff86de45a67eccc781b84f50ab261c18aad5cf wpt-pr: 18945 UltraBlame original commit: e8962724f6cc584847fedccd8c0912b6bd00dab6
Proposal is no longer active. A more elegant solution has been proposed and is now being implemented by browsers: WICG/intrinsicsize-attribute#16
For when the current request is not available or it doesn't have an intrinsic ratio (such as some SVG content). This specifies the proposal in WICG/intrinsicsize-attribute#16, which is implemented in both Firefox and Chromium behind a flag. Tests: https://github.com/web-platform-tests/wpt/blob/a57ec1432f22ac42e8e219a32e2abd7c0baa5b09/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio.tentative.html.
Thank you everyone who's made this happen. It's an exciting improvement to the web. I made a video to explain it — https://youtu.be/4-d_SoCHeWE |
Absolutely, one step closer to a less janky web! 👌👏👏 |
The CSS Working Group is working on a solution to this problem.
The HTML for web developers will look like this:
The UA-stylesheets of all browsers will add this code:
This will calculate an aspect ratio, based on the width & height attributes — before the image has loaded — and have that information available at the very beginning of layout calculation. The moment an image is told to be
width: 100%
, for example, the aspect ratio is used to calculate the height.There is no need for a web developer to do anything else, or anything new (besides return to the habit of using width and height attributes in HTML for a performance gain). It'll just happen.
We are implementing this in Firefox as an experiment, in part to make sure such a solution will not break existing websites.
The text was updated successfully, but these errors were encountered: