Ways to address the horizontal shift of an element caused by the Cumulative Layout Shift issue

My header is styled with a max-width and margin to position it in the center of the page horizontally. Despite this, running a lighthouse report reveals a horizontal shift issue.

If anyone has faced this problem before and knows how to fix the horizontal shifting, I would greatly appreciate your input. Thank you.

Screenshot: https://i.sstatic.net/g2rN6.png

Here is the HTML code:

<header class="header" role="banner" itemscope="" itemtype="http://schema.org/WPHeader">
    <div class="header__container"> 
        <div class="header__search">
            <form class="header-search">
                <input type="text" placeholder="" class="header-search__input" >
                <button type="submit" class="header-search__button" name="Search" aria-label="Search">
                    <i class="fa fas--search"></i>
                </button>
            </form>
        </div>
    </div>
</header>

And here is the CSS code snippet:

.header {
  width: 100%;
  background: #321428;
}

.header__container {
  padding: 10px;
  display: -ms-grid;
  display: grid;
  -ms-grid-columns: auto 1fr;
  grid-template-columns: auto 1fr;
  -ms-grid-rows: auto;
  grid-template-rows: auto;
  grid-gap: 10px;
  width: 100%;
  max-width: 1500px;
  margin: 0 auto;
}
@media (min-width: 768px) {
  .header__container {
    -ms-grid-columns: auto 1fr auto;
    grid-template-columns: auto 1fr auto;
    -ms-grid-rows: auto 40px;
    grid-template-rows: auto 40px;
    grid-gap: 20px;
  }
}
@media (min-width: 960px) {
  .header__container {
    -ms-grid-columns: 21.6% 1fr 25.7%;
    grid-template-columns: 21.6% 1fr 25.7%;
    padding: 10px 20px 15px;
    height: 115px;
  }
}

Answer №1

After some investigation, I came across the solution to my initial inquiry.

The reason for the horizontal shift is due to the ongoing rendering of the bottom parts of the page that are not visible initially, causing the scroll bar to show up later on.

To address this issue, I took a simple approach by including html { overflow-y: scroll; } to ensure the presence of the scrollbar from the beginning. As a result, there were no significant CLS problems indicated in my recent lighthouse assessment.

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

Utilizing CSS to Properly Position HTML Elements

Check out my code on jsFiddle I'm facing some challenges in positioning HTML elements using CSS. While I grasp the fundamental concepts of block and inline elements, implementing them in real coding scenarios can be confusing. I've shared my HTM ...

Load data onto a webpage using Jquery or json without the need to refresh the page, seamlessly integrate it

After receiving data on an html/php page from the database, I often find myself needing to edit it. However, I wish for this data to update automatically without the need to refresh the page each time I make changes and click the submit button. I've c ...

What is the best way to make one page disappear and replace it with a pop-up from

I am currently encountering an issue with my sign-in and registration pages. I have a pop-up page that includes both the sign-in and register tabs. When clicking on the register tab, the sign-in page is supposed to disappear and the registration page shoul ...

What is the best way to alter the header in Django when a user is authenticated?

In my project, I have two headers: header.html and headersuccess.html. When a user is logged in, I need to change the header from header.html to headersuccess.html. How can I implement that? Here is an excerpt from my views.py file where I render loginsuc ...

Issues with jQuery Sliders

Hello there! I am currently in the process of creating a basic jQuery slideshow. The specific requirement for this slideshow is quite simple - I want the images to continuously slide to the left. I have come across numerous tutorials online, but most of th ...

Creating text using the Bootstrap grid system

I am currently using Bootstrap and have set up a grid with a circle on the left side. I am now facing an issue where any text I try to add next to the circle overlaps with it. I need the text to not overlap with the circle and vice versa, while also being ...

PHP warning: Notice: Offset not defined

After creating an API to retrieve data from a database and display it as JSON in HTML, I encountered some PHP errors while trying to echo the data: Notice: Undefined offset: 80 in /opt/lampp/htdocs/ReadExchange/api.php on line 16 Notice: Undefined offse ...

Exploring the child element of the present DOM node within Angular

In my template, I have a code snippet similar to the one below: <button class="btn" > <audio src="mySourceFile.wav"></audio> </button> When the button is clicked, I want to play the audio object. Ideally, I would like to achieve ...

The conditional ng-class styling will be applied to each and every div on the page

For a patient, I have a modal displaying a list of card numbers. If the card is set as the default card, it should have a grey background, based on a true value for the object. <td ng-repeat="obj in paymentAndShipping"> <div ng-click= ...

Animate.css glitch: The webpage grows exponentially

While using animate.css to add animations to some elements, I noticed that when applying the classes fadeInRight and fadeInLeft, a strange issue occurs just before the animation finishes - the bottom scroll bar briefly appears. This is something new for m ...

What is the reason the child component is not being displayed?

The code within the APP.js component looks like this: import React from "react"; import Exam from "./exam.js"; export default function App() { return ( <Exam> <h1>hashemi</h1> </Exam> ); } Similarly, the ...

Create a mobile-friendly version of the website that retains all the functionality and features of the desktop

Currently, I am in the process of optimizing my website for mobile devices. However, creating a separate subdomain and folder for a new mobile version is proving to be a challenge since it was not originally requested. How can I ensure that the website ap ...

What is the best way to expand a footer's height to fill the remaining space on the screen?

Any idea how to achieve this look: Creating a footer that stretches to fill the remaining height of the screen at the bottom of the page. Can CSS make this happen? ...

Optimizing performance: Enhance readback operations in Canvas2D by enabling the willReadFrequently attribute for faster getImageData processing

I am currently utilizing the react-wordcloud package and encountering an issue where the word cloud adjusts to fit 70% of the screen size whenever the container size changes. My console is being flooded with this warning message: https://i.sstatic.net/vdV ...

Struggling to retrieve the dynamic select list information from HTML and pass it to PHP. Any suggestions or guidance would be greatly

<form method="POST" name="mailform" action="sendmail.php"> <fieldset> <?php require_once("mysql_connect.php"); $sql = mysql_query( " SELECT NAME FROM TABLE WHERE ID = ( SELECT ID FROM TABLE2 WHERE COLUMN = '$USERNAME') ORD ...

Placing a GIF on top of an image through the use of Javascript

Welcome to my first post on this platform! I'm currently in the process of creating a portfolio website featuring long panoramic photographs displayed within scrollable divs. You can check it out here: www.edentan.co.uk/portfolio.html. My challenge ri ...

What could be causing my object to not be added properly to a select element using JavaScript programmatically?

When it comes to customizing your pizza, the options are endless. From selecting the type of crust to choosing your favorite toppings, every detail matters. One common issue that arises is when changing the crust type affects the available sizes ...

What is the technique for creating multiple circles using CSS?

Creating multiple circles in CSS and linking them to my HTML file has posed a challenge. The code I have so far is: .circle{ height: 50px; width: 50px; background-color: red; } However, attempting to make another circle using the same 'circle' c ...

Extract CSS using PHP

Currently facing a challenge with a project where I am receiving HTML files using CURL. The issue arises when images are set as background image in the CSS, as they do not load since the images are not on my server. This requires me to adjust the CSS to co ...

What is the best way to overlay an SVG line on top of a CSS style?

Is there a way to make SVG lines appear on top of CSS-styled elements in my HTML file? I have a white background SVG created with JavaScript using d3, and I am adding CSS-styled rectangles on top of it. However, I also want SVG lines (created with JavaScri ...