Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent jank and reduce layout shift with responsive sized images?

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/

like image 337
javinladish Avatar asked Sep 23 '20 21:09

javinladish


People also ask

What are layout shifts and how to fix them?

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.

How to prevent cumulative layout shifts with CSS?

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.

What causes large layout shifts on your website?

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.

How do I Reserve the correct space between images?

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.)


1 Answers

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.

like image 53
Nick Song Avatar answered Sep 26 '22 19:09

Nick Song