What strategies can I implement to minimize jank and minimize layout shifts when using responsive images?

On my website, I have a variety of images in different sizes that adjust responsively to the browser width. However, Google Search Console indicates that there is a significant CLS issue with a shift time of .25s due to the layout moving as the images load.

Given that the images are responsive, it's challenging to set specific dimensions or placeholders to reserve space for them.

Is there a modern approach to addressing CLS concerns with responsive images?

Check out the layout here: https://jsfiddle.net/exspwgab/


Update: To tackle this problem, I attempted a technique found online which involves specifying the image file's width and height within the img tag like this:

<img src="photo.jpg" width="1504" height="752">

Additionally, I applied the following CSS properties:

width: 100%;
height: auto;

However, this method didn't provide the desired results and the webpage elements continued to shuffle during image loading.

If anyone knows of a solution that works across all browsers, please share it with me. I require placeholders to maintain space integrity while images load, in order to avoid any jarring effects on the page.

For reference, here is a JSFiddle showcasing my responsive layout: https://jsfiddle.net/exspwgab/

Answer №1

While I can't claim that this is the ultimate solution to the CLS dilemma, I am simply offering some helpful insight.

It's clear that using constant-sized placeholders for responsive image elements is not a feasible option. But what if we opt for fixed-size placeholders/elements for responsive content instead?

Consider the following example:

img.placeholder-image {
    width: 100%;
    height: 256px;
    object-fit: contain; 
}

By setting a fixed height, this element can maintain a positive impact on the CLS policy by containing the entire image content within itself even during viewport resizing.

I recommend considering the use of <div> elements instead of <img> tags to display image contents (utilizing the background property), although I cannot guarantee that it doesn't violate any audit regulations.

Just my two cents.

Answer №2

Having encountered the exact same issue myself, I found a solution that worked like a charm. Simply replace width: 100% with max-width: 100%.

This workaround is subtly mentioned on this website.

img {
 max-width: 100%; /* or width: 100%; */
 height: auto;
}

Answer №3

Website Code:

<img width="300" height="450" src="300x450.jpg">

CSS Styling:

    img {
        height: auto;
        aspect-ratio: 2/3;
        max-width: 100%;
    }

Compatible Browsers:

  • Chrome 88+
  • Edge 88+

Related Readings:

Answer №4

Don't stress if you need to do what you're doing.

Tools designed to flag potential issues with your website don't always consider the full context. For instance, imagine a scenario where my website features a large 20 MB image that takes a few seconds to load. Google's analysis tools might highlight this as a problem, but perhaps in this case, the image is essential for hosting scientific imagery that necessitates a high-quality, sizable file. In such instances, users would be patient and willing to wait for the necessary data to load.

If your website layout demands loading images that are subsequently resized dynamically, then this is simply a part of the requirements that should not cause undue concern.

Answer №5

After exploring various options, I came across a helpful solution on this page: http://davidecalignano.it/lazy-loading-with-responsive-images-and-unknown-height/#:~:text=And%20here%20is%20the%20formula,flashes%20on%20lazy%20loaded%20images.

Here is the HTML code snippet:

<a class="thumb lazy-container" href="#">
 <img class="lazy" data-original="image.jpg" alt="">
</a>

And here is the corresponding CSS:

.lazy-container {
 display: block;
 position: relative;
 height: 0;
}

.post .lazy-container {
 padding-bottom: 55.3%;
}

.lazy-container img {
 position: absolute;
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
}

Since my images varied in height, I opted to set the padding-bottom percentage as an inline style for each individual image.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Change the border color of radio buttons when clicked

When one of the radio buttons is clicked, it should give an outer border with color. When the other radio button is clicked, it should give an inner border that surrounds the image. <!DOCTYPE html> <html> <head> ...

Tips on extracting the value from the chosen option in 2 separate rows with identical IDs

While looping through my table, each row is identified by the id="batchNo". I am trying to retrieve the selected value of each select option in every row. Although I attempted to achieve this with the provided code snippet, it only retrieves the selected ...

How can custom CSS and JS be loaded in Sencha Touch based on the user's device - PC, tablet, or smartphone?

Is there a way to configure a webpage so that when the user is accessing it from a PC (using Safari/Chrome/Firefox), they see the "normal" version of the page, but if they are using an iPad to view the same URL, they get Sencha Touch (css, js) files in t ...

Exploring the HTML5 File API: Features and Capabilities

After looking into the File API, I'm curious about when all major browsers will fully support it: Firefox has supported it since version 3.6 Chrome since version 8.0 What about Opera and IE? Is the File API meant to replace flash-based uploaders li ...

Selenium IDE - Verify the color in a test step

I've exhausted all the suggestions I came across on Stack Overflow, but unfortunately none of them have been successful in solving my issue. Can you provide guidance on how to confirm that the background color is actually blue for the specified eleme ...

Different ways to style this form using only CSS

Consider the HTML code below: <form> <p> <label for="id_name">Name:</label> <input id="id_name" maxlength="40" name="name" type="text"> </p> <p> <label for="id_format">Fo ...

What is the method to choose an option from a dropdown menu that is devoid of the "select" tag using Selenium in Python?

I am analyzing the HTML code of a dropdown menu that I am interested in. Here is the link to the "Helpfulness" dropdown that I am referring to: After using Selenium to navigate to the page, I need to automate the process of selecting the first option in ...

Arrangement with mutual prime numbers and rowspan

I have a set of unique data points (A, B1, B2, C1, C2 and C3) that I want to organize into a table layout as shown below: +----+----+----+ | | B1 | C1 | | +----+ | | A | B2 +----| | +----+ C2 | | | B3 | | +----+----+----+ After trying ...

What is causing the extra space in Flexbox layout?

Currently, I am in the process of coding a layout that requires me to evenly distribute cards next to each other in rows using flex. However, I am encountering an issue with spacing on the right side and I cannot pinpoint the exact reason behind it. After ...

The JQuery condition is failing to accurately recognize the span checkbox

I am facing an issue with checking the status of a span checkbox to see if it is checked or not. For some reason, I believe that the detection of the checkbox's status is not working correctly. Below is a simplified version of my current code: ...

How can you use JavaScript/jQuery to scroll to the top of a DIV?

When a link is clicked, I have a <div> popup that appears. This popup contains scrollable content when there is overflow. If the same link is clicked again, I want the <div> to be positioned at the top of the scrollable area. While this functi ...

Tips for accessing distinct ng-model values within ng-include within a single HTML document

Consider the following scenario with two HTML files: index1.html <input type="text" ng-model="model.Name"> <!-- Some complex form !--> index.html <div>Journalist's Details: Name:<div ng-include="index1.html"></div> ...

What is the best way to add animation to a button within a form using CSS?

Creating a Form in index.html: <form id="mc-embedded-subscribe-form" class="validate" name="mc-embedded-subscribe-form" method="post" action="mailing.php"><pre> <fieldset> <div class="mc-field-group"> <div class="input-cont"> ...

Using jQuery to adjust the scrollLeft property of a div upon clicking a label

I am attempting to enable horizontal scrolling on a div when a label is clicked. Unfortunately, my current code is not functioning properly. It appears that the code is unable to detect when a label has been clicked and is also failing to scroll the div as ...

Can you explain the attributes "lsdata" and "lsevents" to me?

Just starting out with selenium here. I'm working on automating form filling using selenium and I'm struggling to click a button with the following tag. <a class="urLink urT" id="mq120h" ct="LN" lsdata="{0:'Move\x20Left',1:&apo ...

Adjust the width of the container and rotate the text accordingly

I am looking to display text vertically (like a y-axis chart label) and need the ability to rotate text of varying lengths while keeping it on one line. My attempt to achieve this using CSS3 transforms can be seen in this JSFiddle: .rotate { transform ...

Adjust the width of the div according to the space available from its neighboring div

Is there a way in Bootstrap to have two divs on the same row, where if one is removed, the remaining div takes up the full width? <div class="row"> <div class="col-sm-6" style="background-color:lavender;">Div1</div> <div class ...

eliminate element from array and refresh table

I am facing an issue with my code where I have an array bound to ng-repeat and a button. When the user clicks on the button, I want to remove an element from the array. Here is my current code snippet: var app=angular.module('app',[]); app.con ...

The username index is not defined in the file path C:xampphtdocsAppX1signin.php on line 6

Experiencing some challenges with a php script I recently created. As a beginner in php, I understand that my code may not be optimal. These error messages are displayed when the form is submitted: Notice: Undefined index: username in C:\xampp&bsol ...

mobile devices experiencing scroll due to absolutely positioned elements

Is there a way to have a webpage that takes up the full height and width of the client window, but restricts scrolling on mobile devices? I want to position div elements using absolute positioning with specific transformations, such as playing cards on a t ...