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 }}
This repository was archived by the owner on Jan 9, 2020. It is now read-only.
Repository navigation
This repository was archived by the owner on Jan 9, 2020. It is now read-only.
The CSS Working Group is working on a solution to this problem.
The HTML for web developers will look like this:
<img src="dog.jpg" width="400" height="300">
The UA-stylesheets of all browsers will add this code:
img, video {
aspect-ratio: attr(width) / attr(height);
}
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.
Reacted by Ahmad Shadeed, Scott Jehl, Prae Songprasit, Chriztian Steinmeier, Fotis Papadogeorgopoulos, Sora2455, Fynn Ellie Becker, Sven Wolfermann, Martin Sottnik, Jože Rožman and 82 more
Reacted by CarolMcKay, William Lin, Philipp Tautz, Akis Laoutaris, Yisi, Maximilian Franzke, sandstrom and Aymen
Reacted by Rubén Salvador García San Juan, Olly Hodgson, Rhian van Esch, Kai, Markus Peter, Dan Tonon, Andrej Mernik, Y.Skrypnyk, Yevhenii Skrypnyk, sumanita and 21 more
Reacted by Pierre, William Lin, sandstrom, Aymen and Sobral
👍 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.
Reacted by Masni Bennett, Philipp Tautz, Cathy Dutton, Addy Osmani, Adrian D. Alvarez and Thomas
This is such a great idea! Hopefully, this can help with the proposed HTML loading attribute, as well (whatwg/html#3752). The attribute is designed to prevent making a network request for an image or iframe until it is scrolled into view. However, Chrome's implementation still makes a small request for the first few bytes of the image to get its dimensions. If this rule is added to the UA stylesheet and the author has specified the width and height attributes, Chrome would not need to do its metadata prefetching.
Speaking of iframes, would it make sense to include iframe in this CSS rule? Websites that use iframes to embed videos or animated gifs from other websites like YouTube would be able to scale while maintaining the aspect ratio, too.
Reacted by BezV8, Olly Hodgson, Tamme Schichler, Valtteri Laitinen and Philipp Tautz
Is it safe to assume that the aspect ratio computed from the image’s width and height attributes will always be the same as the aspect ratio of the rendered image after it loads and is styled by the page’s CSS?
As much as I like that proposal:
If I don't make a memorial mistake, this will break the web.
Today, this will squeeze/deform the image, the height stays at 572px:
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.
But yes, I see that the proposal has the big advantage that you don't need an additional attribute.
And above all, we have the long-awaited aspect-ratio for other things as well!
The aspect ratio calculated from the width and height HTML attributes won't have any effect unless the author has explicitly set at least one of the width or heightCSS properties to auto. Therefore the only case where this proposal can result in a change in rendering is if the author has both: a) set incorrect width/height values in the HTML and b) set one of the image's dimensions to auto in the CSS. Furthermore if that situation happens to be a Web-compat problem, we can change the priority so that the image's own aspect ratio wins over the declared one once it is loaded.
Is attr() no longer experimental outside of pseudo content?
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 content. But the effect would be equivalent.
Reacted by Šime, Sora2455, Dan Tonon and Jen Simmons
The aspect ratio calculated from the width and height HTML attributes won't have any effect unless the author has explicitly set at least one of the width or height CSS properties to auto.
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?
Furthermore if that situation happens to be a Web-compat problem, we can change the priority so that the image's own aspect ratio wins over the declared one once it is loaded.
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?
That seems like a pretty big oversight, for which I feel particularly guilty. @zcorpan mentioned that to me in passing during TPAC as well, and it seems like something we should tackle.
@emilio - I feel like we could have defined at "update the image data" that the height/width attribute values that HTMLImageElement goes with are passed along with the selected source. Do you predict any issues with a solution along those lines?
Reacted by Nicolas Hoizey, Andrew S and MassDebates
I recommend opening an issue against HTML to discuss that use case, similar to how there's an issue opened for video et al at whatwg/html#4961.
It seems to me there's various things to consider for responsive images, such as whether each potential image source should be able to set its own aspect ratio as suggested above. (That would require introducing new attributes for source as well.)
@emilio - I feel like we could have defined at "update the image data" that the height/width attribute values that HTMLImageElement goes with are passed along with the selected source. Do you predict any issues with a solution along those lines?
To be clear, if the attributes are in <img> it should already work, I don't think any spec change is needed. But that constrains you to have all sources with the same aspect-ratio (which is most of the use cases, afaict).
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 <source> and figure out how that should work in the various edge cases.
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?
To be clear, if the attributes are in <img> it should already work, I don't think any spec change is needed. But that constrains you to have all sources with the same aspect-ratio (which is most of the use cases, afaict)
Unfortunately, when <picture> is involved, the use case is typically art direction where the different sources are not of the same aspect ratio.
Reacted by Robert Linder, Nicolas Hoizey, Moses Holmström, Sami Resua, Thomas Deinhamer and RoNoMaD
As suggested, I made a new proposal for aspect-ratio in regards to art-direction with multiple sources, each having their own aspect-ratio.
Particularly, In regards to <picture> or <video> as both can utilize multiple <source>
Reacted by MassDebates, Sami Resua and Eric Fuhrmann
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
so we can pass the word, and teach people how to make sure their sites get the benefit of this change.
Reacted by Nestor Vera, Thomas and fregante
Reacted by Robert Linder, Sora2455, sephore, Sami Resua, Nestor Vera and Eric Portis
Activity
scottjehl commentedon Jun 6, 2019
👍 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.
chriscoyier commentedon Jun 6, 2019
I'm into it. Seems like a fantastic idea.
I like blogging so I blogged it.
JoshTumath commentedon Jun 6, 2019
This is such a great idea! Hopefully, this can help with the proposed HTML
loadingattribute, as well (whatwg/html#3752). The attribute is designed to prevent making a network request for an image or iframe until it is scrolled into view. However, Chrome's implementation still makes a small request for the first few bytes of the image to get its dimensions. If this rule is added to the UA stylesheet and the author has specified thewidthandheightattributes, Chrome would not need to do its metadata prefetching.Speaking of iframes, would it make sense to include
iframein this CSS rule? Websites that use iframes to embed videos or animated gifs from other websites like YouTube would be able to scale while maintaining the aspect ratio, too.scottjehl commentedon Jun 6, 2019
I agree.
iframewould be nice, as wouldobject.simevidas commentedon Jun 6, 2019
Is it safe to assume that the aspect ratio computed from the image’s
widthandheightattributes will always be the same as the aspect ratio of the rendered image after it loads and is styled by the page’s CSS?nuxodin commentedon Jun 7, 2019
As much as I like that proposal:
If I don't make a memorial mistake, this will break the web.
Today, this will squeeze/deform the image, the height stays at 572px:
https://jsfiddle.net/tpgn143f/4/
According to this proposal, the height will be calculated from the aspect ratio.
michaelwhyte commentedon Jun 7, 2019
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.
Sora2455 commentedon Jun 7, 2019
@michaelwhyte If I understand this correctly, this rule would have no effect on images without width and height attributes, leaving their behaviour unchanged.
nuxodin commentedon Jun 7, 2019
@jayhostan True, it would be necessary to set the CSS height to
auto.So it would not break the web.
Also, it would not be possible to polyfill this without an additional custom-property
--aspect-ratioor manually parsing the stylesheets.https://github.com/nuxodin/intrinsicsize-polyfill
But yes, I see that the proposal has the big advantage that you don't need an additional attribute.
And above all, we have the long-awaited
aspect-ratiofor other things as well!yoavweiss commentedon Jun 7, 2019
@ojanvafai @loonybear @dvoytenko - Thoughts?
yoavweiss commentedon Jun 7, 2019
Also, @kinu & @domfarolino for lazy loading and range request downloads.
chrisg88 commentedon Jun 7, 2019
Is attr() no longer experimental outside of pseudo content?
fantasai commentedon Jun 7, 2019
The aspect ratio calculated from the
widthandheightHTML attributes won't have any effect unless the author has explicitly set at least one of thewidthorheightCSS properties toauto. Therefore the only case where this proposal can result in a change in rendering is if the author has both: a) set incorrect width/height values in the HTML and b) set one of the image's dimensions toautoin the CSS. Furthermore if that situation happens to be a Web-compat problem, we can change the priority so that the image's own aspect ratio wins over the declared one once it is loaded.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
content. But the effect would be equivalent.ojanvafai commentedon Jun 7, 2019
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?
ojanvafai commentedon Jun 7, 2019
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.
77 remaining items
yoavweiss commentedon Oct 3, 2019
That seems like a pretty big oversight, for which I feel particularly guilty. @zcorpan mentioned that to me in passing during TPAC as well, and it seems like something we should tackle.
@emilio - I feel like we could have defined at "update the image data" that the height/width attribute values that HTMLImageElement goes with are passed along with the selected source. Do you predict any issues with a solution along those lines?
annevk commentedon Oct 3, 2019
I recommend opening an issue against HTML to discuss that use case, similar to how there's an issue opened for
videoet al at whatwg/html#4961.It seems to me there's various things to consider for responsive images, such as whether each potential image source should be able to set its own aspect ratio as suggested above. (That would require introducing new attributes for
sourceas well.)sami616 commentedon Oct 3, 2019
supporting
<picture>would be super helpful!emilio commentedon Oct 3, 2019
To be clear, if the attributes are in
<img>it should already work, I don't think any spec change is needed. But that constrains you to have all sources with the same aspect-ratio (which is most of the use cases, afaict).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
<source>and figure out how that should work in the various edge cases.sami616 commentedon Oct 3, 2019
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?
emilio commentedon Oct 3, 2019
Yes. As long as the attributes are on the
<img>inside the picture it'd work.yoavweiss commentedon Oct 3, 2019
Unfortunately, when
<picture>is involved, the use case is typically art direction where the different sources are not of the same aspect ratio.AndresInSpace commentedon Oct 3, 2019
As suggested, I made a new proposal for aspect-ratio in regards to art-direction with multiple sources, each having their own aspect-ratio.
Particularly, In regards to
<picture>or<video>as both can utilize multiple<source>Bug 1580000 [wpt PR 18945] - Compute img aspect ratio from width and …
Bug 1580000 [wpt PR 18945] - Compute img aspect ratio from width and …
Bug 1580000 [wpt PR 18945] - Compute img aspect ratio from width and …
chore: remove intrinsicsize
Use width and height as intrinsic aspect ratio for img elements
jensimmons commentedon Nov 8, 2019
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
so we can pass the word, and teach people how to make sure their sites get the benefit of this change.
sami616 commentedon Nov 9, 2019
Absolutely, one step closer to a less janky web! 👌👏👏