My website hosts a lot of images of all sizes. These images are responsive and change size at all browser widths from desktop to mobile. I see in my Google Search Console that I have a poor CLS (cumulative layout shift) of .25s. The layout of my website shifts as the images load.
Since my images are responsive, I can't specify exact sizes of the images, or have placeholders to reserve the space.
What is a modern way to prevent CLS with responsive images?
Layout here: https://jsfiddle.net/exspwgab/
Update: I tried one suggestion on the internet which is to specify the image file's width and height within the img tag like:
<img src="photo.jpg" width="1504" height="752">
And then in CSS you do:
width: 100%;
height: auto;
This didn't seem to work in any browser. And the elements on my webpage still moved all over as the images loaded.
If anyone has a solution that works in all browsers please let me know. I essentially need placeholders to hold the space while the images load to prevent the page jank issue.
JSFiddle of my responsive layout here: https://jsfiddle.net/exspwgab/
Layout shifts are the shifting of page elements on a web page without any prior input from the user. In simple terms, this means that visitors on your website may find page elements such as images, videos, fonts, or buttons shifting around unexpectedly as the page is still downloading.
Using the ‘object-fit: contain’ CSS property can be a viable solution to prevent cumulative layout shifts, especially when you are unable to add image dimensions and aspect ratio to the HTML. All you need to do is estimate a proper aspect ratio and use the CSS property.
A big factor that causes large layout shifts is unoptimized custom fonts. Using downloaded custom fonts can cause FOIT and FOUT that lead to large layout shifts on your website. FOIT or flash of invisible text happens when elements using the custom font stay hidden until the font is downloaded completely.
To reserve the correct space you first need to set the width/height attributes as you’ve already done. (Technically this doesn’t need to be the true pixel size of the image but don’t use a 1000px wide image if it will never be displayed that large.)
I am not sure if this is exactly "a modern solution" to the CLS issue but just trying to be helpful as much as I can.
Obviously, it's not logically possible to put constant-sized placeholders for the responsive image elements. What if we use placeholders/elements with fixed-sizes for the responsive contents?
For example:
img.placeholder-image {
width: 100%;
height: 256px;
object-fit: contain;
}
With the fixed-height, this element won't add up anything negative to the CLS policy while keeping the whole image content inside the element itself even if the viewport gets resized.
I'd very much suggest you consider using <div>
s instead of <image>
elements to display image contents (using background
property), however, I can't vouch that's not another violation of audit rules.
My two cents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With