The Lighthouse tool consistently indicates a high Cumulative Layout Shift (CLS) score, even after adjusting

On the Lighthouse report, Cumulative Layout Shift shows a high number: 0.546

Upon analyzing the trace, I noticed that the image block initially does not take up the required space and pushes down the content below. Refer to the image: https://i.sstatic.net/u8zDc.jpg

I find this surprising, considering that the container div for the image element has a min-height setting:

CSS

div[itemprop="image"] {
    display: inline-block;
    float: left;
    margin-right: 10px;
    min-height: 400px;
}

#prodimage {
    width: 100%;
    max-width: 400px;
    border: 1px solid #FFF;
}

HTML

<div itemprop="image" itemscope="" itemtype="http://schema.org/ImageObject">                
    <a href="#"><span itemprop="url" content="https://www.example./com/images/10055041.png"></span>
        <picture><source type="image/webp" data-srcset="/images/10055041.webp" srcset="/images/10055041.webp">
            <img id="prodimage" data-src="/images/10055041.png" src="/images/10055041.png">
        </picture>
    </a>
</div>

Answer №1

Ensure to specify both height and width for your image in the inline CSS.

When you insert an <img/> element, the browser cannot determine its dimensions until it communicates with the server.

Since you have embedded your CSS inline, the browser will render the page based solely on the initial request (your page HTML and inline styles).

Due to this, it attempts to calculate the layout, scans your inline CSS for size details of the image, fails to find any information regarding width, and therefore struggles to allocate horizontal space for the image.

The browser later fetches the image from your server, acquires the dimensions, and then allocates 400px by 400px for it.

This results in a layout shift from a space that was initially 0px wide and 400px tall to one that is now 400px wide and 400px tall (plus a 1px border).

You were close by setting a min-height for the image, but it is also essential to define a min-width.

Therefore, your CSS should be:

div[itemprop="image"] {
    display: inline-block;
    float: left;
    margin-right: 10px;
    min-height: 400px;
    min-width: 400px;
}

An Alternative Approach for Handling Images

Instead of specifying fixed sizes for images, consider using a responsive design.

In this method, you add the image within a <div>, which has a percentage-based width defined.

Subsequently, place your <img/> inside that <div> with width: 100%.

This resolves the issue of the browser needing to compute the width.

To address the height, add width="400" and height="400" to your <img/>.

Most browsers utilize this information to calculate the image's aspect ratio.

By doing so, the browser can allocate sufficient vertical space by multiplying the width by the aspect ratio.

Illustration

In the following example, note that I set the <img/> width to 40 and height to 40.

This demonstrates that the specific values do not matter as long as they maintain the correct aspect ratio. For instance, on a 16:9 image, you could set width="16" and height="9", even for an image sized 800 by 450 pixels.

The browser follows this process:

  • Determine the div's size on screen (on a 660px wide screen, it is 200 pixels).
  • Evaluate the image's width (100% * 200 pixels = 200 pixels).
  • Calculate the aspect ratio (width / height => 40 / 40 = 1:1).
  • Multiply the derived width by the height (200 pixels * 1).

Hence, the browser assigns a 200px by 200px area for the image before initiating the request, eliminating any layout shifts.

<style>
   .holder{
       width: 33%;
       background-color: #333;
   }
   img{
       width: 100%;
       height: auto;
   }
</style>

<div class="holder">
  <img src="https://placehold.it/400x400" width="40" height="40" />
</div>
    
    <p>text that will not move</p>

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

Adjust background image size to fit the dimensions of the foreground content

I have a div where an image and content are inside. I am trying to make the background image adjust its size based on the content within it, rather than showing the full image size with empty space. My current styling uses flexbox: .section { height: ...

Is there a way to create a customized select handle using CSS?

Is there a way to create a resizable border that appears when a user clicks on the div? And when they move the mouse over it, can the cursor change accordingly? I also want the user to be able to drag the element's border and have the element resize a ...

Container for Magazines using HTML and CSS

Looking to develop a digital magazine application with smooth swipe transitions and fade in effects when swiping left and right. I attempted using jQuery.mobile, but struggled with adapting the background image height. My goal is to create a universal web ...

Sort the DOM elements in jQuery based on their CSS color property

Displayed on the page below is a list of usernames in a random order. I'm looking to sort them using jQuery in the following sequence: red blue green purple black This is my current progress: <script type="text/javascript"> $(function() { ...

Arrange the bootstrap columns in a grid layout both vertically and horizontally adjacent to one another

I need help with displaying my columns in this specific layout https://i.stack.imgur.com/2hzju.jpg Code <div class="row"> <div id="id-orders-stats" class="col-md-6 col-sm-6"> </div> <div class="col-md-6 col-sm-6 stats-box"&g ...

Having trouble positioning the image at the center of the ion-slides

I'm currently working on designing a slide within an ion item. Everything seems to be functioning correctly, however, the image inside the slide is not appearing in the center. <ion-item style="height:45%; padding-left: 0;"> <ion-slides ce ...

What could be the reason behind CSS internal style not working while inline style is functioning properly?

Here is the code I am currently working with. It seems that there may be an issue, but I'm not sure what it could be. The first part contains internal CSS and below is inline CSS which appears to be functioning properly. My suspicion is that the image ...

Troubleshooting the Problem with CSS Margin in HTML Footer

I'm encountering an issue with the footer on my website. The margin: auto; command doesn't seem to be working for the list items in the footer. I want the items in the footer to occupy one-third of the width, but no matter where I place the marg ...

The header and sub-navigation are in disarray and require some help

Dear web experts, After six months of learning to code websites, I'm tackling a big project with some challenges. The recent changes to the header and subnav have thrown everything off balance, making it look wonky and not quite right. Specifically, ...

Having difficulty with the placement of a div element in HTML code, wondering how to position it according to my specific

As a beginner, I ran into some trouble with my code while trying to create a simple 'game' for fun. The initial result looked like this. After that, I attempted to add another div onto the brown element using the following CSS: #energy_and_coins ...

Unconventional arrangement of gaps for radio buttons

Hey there, I've been searching for solutions on various platforms but haven't found any luck so far. These are the questions I've looked into: Radio buttons and label to display in same line Documentation on <FIELDSE ...

AngularJS 1 - CSS glitch occurs when navigating between tabs

Upon loading my homepage, it appears as follows: Initially, certain rows were filled with a blue background color using ng-class, specifically "row-answered-call" as shown below: <tr ng-repeat="item in list" ng-class="{'row-answered-call': i ...

CSS navigation issue

When using the third example provided on this page: http://simple-navigation-demo.andischacke.com/ I encountered an issue where the main page would display an empty div on the left instead of the content filling the entire area. However, when navigating ...

Why do CSS changes in Chrome Console only take effect locally and not when the website is live?

I recently encountered an issue where I wanted to incorporate negative margins into a specific section of my website, as seen in the Chrome Console (link to image): .woocommerce div.product div.images .woocommerce-product-gallery__wrapper { -webkit-tr ...

What is the reason behind needing to set both top/bottom and left/right to 0 so that my body::before can effectively cover the entire page?

I am currently diving into the world of front-end web development and I am having some trouble grasping the concept behind the behavior of body::before. Below is a snippet of my CSS code: body::before { content: ""; position: fixed; width: ...

When you hover over a Wordpress page, the entire text on the page will be underlined

Currently designing my own website using WordPress with the Elementor free plugin and Phlox theme. I've made some progress by completing a few sections which include text, buttons, and images. However, I'm encountering an issue where all the text ...

Trouble extracting data with XPath query in Python

Currently, I am attempting to extract specific information from this particular webpage. However, there are three crucial pieces of data that have proven to be quite elusive. Firstly, I am struggling to retrieve the grade, denoted by '5.6' next ...

The combination of DOMDocument and DOMXPath is a powerful duo

I am currently working on converting CSS to inline using DOMDocument and DOMXPath. However, I'm encountering an issue where the HTML I provide is not being recognized as valid XML. Is there a way for me to configure these functions to ignore unknown ...

Static CSS box positioned above dynamic scaling box

Let's Think About It <div id="foo"> Lorem ipsum </div> <div id="bar"> sit amet </div> Is there a way to maintain the size of foo as it is while allowing bar to fill the remaining space on the page when the window size cha ...

Node-modules CSS

In my React application, I am utilizing CSS modules. By configuring modules:true in my webpack.config.js, I can successfully apply styles from files within my src folder. However, when importing components from node-modules, the corresponding CSS is not be ...