The Impact of Cumulative Layout Shift on Bootstrap 4 Grid System

Experiencing an issue with high CLS (Content Layout Shift) using Bootstrap (4.5) grid for a two-column layout with column order change.

CLS is a crucial Core Web Vital metric that Google monitors to ensure webpage elements do not shift during loading, potentially impacting SEO rankings.

My layout features two columns on higher resolutions - main content on the right and sidebar on the left. However, on smaller screens, the sidebar content is pushed down below the main content.

<div class="container-fluid">
    <div class="row">
        <div class="col-lg-8 order-lg-2">
        </div>
        <div class="col-lg-4 order-lg-1">
        </div> 
    </div> 
</div>

The issue arises when, upon page render on desktops, the main content momentarily appears on the left side before shifting to its correct position on the right. This shift is subtle on simple pages but becomes noticeable with more complex layouts or external resources.

For reference, here's an example page I've prepared. To evaluate CLS, I use Chrome Lighthouse, which indicates a value of 0.326 for the shifting columns. Results may vary depending on rendering resources, but Google Page Insight typically offers similar findings:

https://i.sstatic.net/OowJx.png

Is there a way to prevent this shift during page load? 

Answer №1

It appears that the issue is more closely linked to Chrome than either flexbox or bootstrap. Upon closer examination, it seems that the root cause lies in premature rendering. Specifically, Chrome's parser initiates rendering: 

  • after analyzing 65536 bytes of data, 
  • upon encountering a <script> tag after reading 50 "tokens" (which are essentially HTML tags).

The provided example illustrates the first scenario (though my actual website encounters CLS due to the second scenario). Both situations have associated "bugs" reported: 1130290 and 1041006

Therefore, the solution hinges on resolving these "bugs". In the interim, depending on the specific cause, you may consider limiting file sizes or removing <script> tags.    

Answer №2

Insightful Answer

Simplify the HTML structure to resolve the issue effortlessly.

In-Depth Explanation

Upon investigation, while I may not have a definitive answer, I have formulated a feasible solution and provided some rationale behind it. I am open to further insights from others.

The Epiphany

During an analysis of the page load process, I observed that there were two separate tasks for parsing the HTML document.

One task handled lines 1-770, while the other focused on lines 771 onwards.

This caused the initial rendering of the first 770 lines followed by a layout recalculation during the second parsing task, due to the rearranged order that placed the .col-lg-4 column in the later part of the HTML.

This behavior is not typically noticeable on a "regular" page since rendering in DOM sequence usually results in correct layouts, with the additional parsing pass contributing to finer details.

It seemed consistent where the page cut off, prompting me to eliminate line breaks and whitespace. My hypothesis was that the algorithm determining the split points in the HTML factored in line numbers.

By reducing the effective lines to around 15, my aim was to force the algorithm to parse the HTML within a single pass.

While it still utilized two passes, the final one consisted only of processing the closing </html> tag, which proved inconsequential. This allowed for accurate layout calculations when combining the parsed HTML with the CSSOM.

Although somewhat unconventional, this workaround should suffice up to a certain depth of page complexity.

Note: Doubling the DOM node count rendered this workaround ineffective. Similarly, altering the content length of list items without changing the structure had no impact. Therefore, the stopping point of the HTML parser seems to be influenced by a combination of DOM element count and line numbering.

A Potential Remedy

Consider reverting to traditional layout models involving float:left and float:right, as opposed to utilizing flexbox. The issue at hand appears to stem from the intricate design of the webpage (high number of DOM nodes) combined with the use of flexbox.

Given that flexbox tends to be slower than older layout methods and may require multiple passes (unlike the single pass nature of older models), making this adjustment could likely resolve the persisting problem.

Further insight on multiple layout passes in specific scenarios

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

The iteration count and stop code in CSS are failing to function correctly

Is there a way to make this loop once and then stay as is unless the page is refreshed? I've tried multiple methods I came across. Can you modify the code so I can see where the changes are being made? The goal is to have it display once and then per ...

What is the trick to utilizing CSS transitions to enlarge the font size of a Bootstrap card when it is hovered over

I'm having trouble increasing the font size and changing the color on hover for text inside a Bootstrap card. My current code only changes the color without adjusting the font size or implementing any transitions. I don't have much experience wit ...

Ways to incorporate transparency into the background color

Is there a way to make the following CSS statement partially transparent? background: none repeat scroll 0 0 #205081; If CSS2 doesn't support it, how can I add an opacity declaration in CSS3? ...

Side-to-Side Bootstrap Display Panel

I have been attempting to customize a Bootstrap panel to create a horizontal version, but I am struggling to align the panel heading vertically with the content in the panel body. I believe it may be related to clearing the divs. Front-end development is ...

Invisible boundary line within the column

My task involves creating a table using div elements. The layout of the table includes two columns structured as follows: <div> <div class = "columnborder"><span>Some text for column 1</span></div> <div><span>Some ...

The clickable functionality of buttons and text labels in Vue is currently not working

When I try to input text into the registration page in vue, nothing happens when I click the register/login button. However, if I press TAB and then enter my password followed by another TAB and ENTER, it works. This is the code snippet for the login-box: ...

Is it necessary to have col-xs-12 in Bootstrap 3?

In order to achieve the desired display for different screen sizes, we have implemented the following code: <div class="col-md-6">left</div> <div class="col-md-6">right</div> When viewed on smaller screens than md, this code succe ...

What is the ideal location for storing my CSS files?

I have a medium-sized website that shares a common header/footer, and each page has its own specific layout file. The common header/footer is stored in a separate html file, but I am struggling with where to place the CSS file because the link element in f ...

How can I use Angular 7 to create a background image for a View made up of multiple components?

Here is the HTML code that I am working with: <app-navbar></app-navbar> <router-outlet></router-outlet> My goal is to have an image set as the background for the home view, which consists of two components: navbarComponent and hom ...

Tips on introducing a random pattern into a Javascript gaming experience

To kick off the game, let's generate a pattern randomly. Follow these steps: -Include a function that generates the pattern. (Hint: JavaScript's Math.random function might come in handy) -Invoke your new function from the startGame function I&a ...

Items in a pair of distinct columns

I want to enhance a picture by adding three paragraphs on the left-hand side. Below is my HTML code: <md-toolbar layout-align="center center"> <h3>Traffic Light Component</h3> </md-toolbar> <md-grid-list md-cols ...

CSS search icon malfunctioning

Within my CSS, I have included the code snippet below: #myInput { background-image: url('/css/searchicon.png'); background-position: 10px 10px; background-repeat: no-repeat; width: 100%; font-size: 16px; padding: 12px 20px 12px 40px; ...

Prevent a specific image from being impacted by CSS styling

I've implemented the following CSS on my website for image opacity: img { opacity:0.4; filter:alpha(opacity=40); } img:hover { opacity:1; filter:alpha(opacity=100); } Now, I am looking to exclude certain images from being affected by this ...

Accordion A and B both require the first category of accordion A to be opened first in order for the first category of accordion B to also be

Greetings, this is my initial inquiry, please excuse any errors as English is not my first language. For my project, I am utilizing Angular 6, jQuery, and BS. I have two accordions, each with different categories: A1 and A2. My goal is for clicking on A ...

When combining <a> with the class line-through, the line does not appear

I am currently utilizing a class to replace the deprecated strike in HTML: .entfall { text-decoration: line-through; } However, when using Firefox 38.x and the following code: <span class="entfall"><a href="some link">text</a></span ...

Creating a Scrollable Content Container with Bootstrap's Flex Box

I am in the process of developing a typical application layout using Bootstrap 5 and flexbox. The layout consists of a top bar, bottom bar, and a content area that adjusts its size automatically. Within the content area, there is a side bar and a main cont ...

How are the plugins configured for `postcss-import` implemented?

Recently, I've transitioned to using PostCSS exclusively with Webpack. As I explore the functionalities of using postcss-import to inline external stylesheets, I'm noticing that its options offer the ability to configure plugins and transformers ...

Manipulating one element's behavior on hover by means of another using CSS

I am currently experimenting with creating a unique layout on a webpage that features two columns. The goal is to have both columns shift towards the center if you hover over the left side, emphasizing the left column. Similarly, if you hover over the righ ...

Utilizing conditional statements like if/else and for loops within a switch statement in JavaScript allows for

I am currently developing a request portal that involves dynamic checkboxes, labels, and textboxes that are dependent on an option list. As a beginner in javascript, I am facing challenges with creating conditional statements. I have managed to make progr ...

Issue with Masonry layout not adjusting to change in window size

Something seems to be fixed on displaying four rows, no matter the size of the window. Previously, it would adjust to three rows or fewer as the browser was resized. I recently played around with columnWidth, but reverting it back to 250 doesn't seem ...