Avoiding image layout shifts (CLS): aspect-ratio vs. width and height
jakearchibald.com
jakearchibald.com
- Allows for using the img tag sizes (and srcset)
- When using sizes (w/ srcset) I don't have to worry if the image the browser picks from the srcset isn't the correct size and/or aspect ratio.
> So, which method should we use?
> We've got one solution, aspect-ratio, which is CSS-based. And the other solution, presentational hinting, which uses width and height attributes. The question is very similar to "should this image be a <img> or a CSS background-image?" and the answer is the same: Is it content or design?
it sounds like the author takes an either-or approach to this, using either aspect-ratio (for design images) or height and width (for content images). having only skimmed, i haven't yet digested why hinting via attr(height) and attr(width) isn't the (future) preferred best-of-both-worlds, other than it not being supported yet ( https://caniuse.com/css3-attr ).
So the only way to ensure cover is object-fit.
Note: this is also useful for background images in the sense totoo often the same large HD+ image is served to mobile devices.
i’m also really excited by :has() which i think will be the biggest improvement to css since grid. firefox has just started working on it, while safari and chrome already have an initial implementation in place.
Best I can tell, the article works if you are 100% certain that all the images in the srcset have the same aspect ratio.
With, for example WordPress, that is often not the case.
From https://html.spec.whatwg.org/multipage/images.html#introduct...
> The srcset and sizes attributes can be used, using the w descriptor, to provide multiple images that only vary in their size (the smaller image is a scaled-down version of the bigger image).
If you want different representations of the content, those should be different <source>s.
From https://html.spec.whatwg.org/multipage/images.html#introduct...
> The picture element and the source element, together with the media attribute, can be used to provide multiple images that vary the image content (for instance the smaller image might be a cropped version of the bigger image).
More details on responsive images in general https://jakearchibald.com/2015/anatomy-of-responsive-images/.
Maybe you're using a custom theme that introduces buggy behaviour?
WordPress can't know, or at least be 100% certain of the image's dimensions. Yes, you can add custom images but there's no guarantee those will yield after the auto-resizing.
Also, have to uploaded any images that might not satisfy the necessary dimensions.
If WP was doing it correctly all the time there would be a question or two per week about images in the various WP groups I'm in.
At the end of the day, you can have rules for WP cropping / resizing images. But those don't necessary guarantee the dimension of the image output from that. And WP certainly isn't checking each any every image for it's actual dimensions.
And WP aside, it just doesn't make sense any output from a CMS is going to exactly what you expect. object-fit is a fail-safe. Object-fit is also smarter and better than a fixed size background image.
The article has a narrow-ish use case.
Regardless, when using WP this isn't going to happen.
Furthermore, even if all the images in the srcset have the same a-ratio, that doesn't mean that ratio is the same as the ratio in the markup + CSS. Yeah, you can assume. But why risk it?
It says "the smaller image is a scaled-down version of the bigger image". If you scale an image down evenly, it will have the same aspect-ratio. In case you're going to pick at the wording: I know that it's the intention of the spec authors and browser implementers that all entries in the srcset have the same ratio.
> even if all the images in the srcset have the same a-ratio, that doesn't mean that ratio is the same as the ratio in the markup + CSS. Yeah, you can assume. But why risk it?
The last section of the article discusses this.