简体   繁体   English

累积布局移位问题 - 如何修复元素水平移位?

[英]Cumulative Layout Shift problem - how to fix an element from horizontally shifting?

I have a basic header that has a max-width and margin applied (standard stuff) in order to position it horizontally in the middle of the page.我有一个基本页眉,它应用了最大宽度和边距(标准内容),以便将其水平放置在页面中间。

However, when I run a lighthouse report, it shows a horizontal shift.但是,当我运行灯塔报告时,它会显示水平偏移。

Below is my HTML, CSS and the screenshot of the shift.下面是我的 HTML、CSS 和班次的截图。

I would appreciate it if anyone else experienced this problem before and know how to combat this horizontal shifting.如果其他人以前遇到过这个问题并且知道如何应对这种水平移动,我将不胜感激。 Thanks.谢谢。

Screenshot:截屏: CLS 水平移位

HTML: HTML:

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

CSS: CSS:

.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;
  }
}

I found the answer to my own question.我找到了自己问题的答案。

The horizontal shift happens because the page is still rendering the remaining parts of the page which is below the fold, causing the scroll bar to appear later on.发生水平移动是因为页面仍在渲染页面下方的剩余部分,导致滚动条稍后出现。

To combat this situation, I simply added html { overflow-y: scroll; }为了解决这种情况,我简单地添加了html { overflow-y: scroll; } html { overflow-y: scroll; } to enforce the scrollbar at first paint. html { overflow-y: scroll; }在第一次绘制时强制滚动条。 Since then, my lighthouse report came back with no major CLS issues.从那以后,我的灯塔报告没有出现重大的 CLS 问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM