简体   繁体   English

Flex Box - CLS(累积布局偏移)

[英]Flex Box - CLS (cumulative layout shift)

I am having issues with CLS shifting (.28) with Flex Box order or I need a way to reorder the divs.我在使用 Flex Box 顺序进行 CLS 移位 (.28) 时遇到问题,或者我需要一种方法来重新排序 div。 The heights are not known so can't use margin, etc. The content is large (images, etc) in each block.高度未知,因此不能使用边距等。每个块中的内容都很大(图像等)。 This code does not product a CLS issue because it is just a small example.此代码不会产生 CLS 问题,因为它只是一个小示例。 I have also tested grids but that has even higher CLS.我也测试过网格,但它的 CLS 更高。 Is there a different way to flip the order and not use flex box?有没有不同的方法来翻转订单而不使用弹性框?

Here is the code:这是代码:

<style>
    .layoutflex {
       display: -webkit-box;
       display: -ms-flexbox;
       display: flex;
       -webkit-box-orient: vertical;
       -webkit-box-direction: normal;
       -ms-flex-direction: column;
       flex-direction: column;
       flex-shrink: 0;
    }

    .layoutflex .topflex {
        order: 1;
        -ms-flex-order: 1;
        -webkit-order: 1;
    }

    .layoutflex .bottomflex {
        order: 0;
        -ms-flex-order: 0;
        -webkit-order: 0;
    }

</style>

<div class="layoutflex">
<div class="topflex">Row 1</div>
<div class="bottomflex">Row 2</div>
</div>

There are many causes of Layout shifts in a web application. web 应用程序中布局偏移的原因有很多。 Some of them are very easy to detect and resolve, while others are not.其中一些非常容易检测和解决,而另一些则不然。 Below is a list of the causes of Cumulative Layout Shift:以下是累积布局偏移的原因列表:

  1. The display of images, videos and UI objects without reserved dimensions (without appropriate width and height values)图片、视频和 UI 对象的显示没有保留尺寸(没有适当的宽度和高度值)

  2. The dynamic or asynchronous addition or removal of DOM Elements without a placeholder width and height values动态或异步添加或删除没有占位符宽度和高度值的 DOM 元素

  3. Dynamic resizing of UI elements without a user action无需用户操作即可动态调整 UI 元素的大小

  4. Third-party scripts or ad providers inserting ad banners without a reserved placeholder with appropriate dimensioning.第三方脚本或广告提供商插入的广告横幅没有带有适当尺寸的预留占位符。

  5. The use of certain CSS Flexbox properties使用某些 CSS Flexbox 属性

  6. Animating or transitioning CSS properties that cause UI reflows, such as Padding, Margin, Height, Width, Top, Left, etc动画或过渡 CSS 导致 UI 回流的属性,例如 Padding、Margin、Height、Width、Top、Left 等

Unless if explicitly specified, when web browsers paint flexbox items, it does not know the exact dimension of the items as they have to be computed because of the nature of Flexbox.除非明确指定,否则当 web 浏览器绘制 flexbox 项目时,它不知道项目的确切维度,因为由于 Flexbox 的性质,它们必须被计算。

Flex Items can shrink or flex (grow) depending on the available space.弹性项目可以根据可用空间收缩或弯曲(增长)。 This is where the problem lies.这就是问题所在。 Browsers draw DOM elements bit by bit in frames.浏览器在框架中一点一点地绘制 DOM 元素。 Hence, the accurate dimension of a Flex item is known until all items have been rendered.因此,在所有项目都被渲染之前,Flex 项目的准确尺寸是已知的。

Certain flex properties cause Layout Shifts if misused.如果使用不当,某些 flex 属性会导致布局偏移。 they include:他们包括:

  1. Align-items property which alters the flow of flex items along the cross axis Align-items属性改变弹性项目沿交叉轴的流动

  2. Justify-content property, which alters the flow of flex items along the main axis Align-self Justify-content属性,它改变了弹性项目沿主轴的流动

  3. flex which causes a flex item to grow based on the leftover space flex导致弹性项目根据剩余空间增长

  4. Use of Margins to push flex items horizontally or vertically使用边距水平或垂直推动弹性项目

The simplest method to fix layout shifts caused by flexbox items is to give the items width values.修复由 flexbox 项目引起的布局偏移的最简单方法是为项目提供宽度值。 This will help the browser in decision making.这将有助于浏览器做出决策。 By having specified widths, flexbox items can be positioned appropriately across frames.通过指定宽度,flexbox 项可以跨帧适当定位。

It's not flexbox necessarily that's the problem, it's the images loading in with dynamic heights.问题不一定是 flexbox,而是以动态高度加载的图像。 Also, make sure to add this CSS in-line instead of an external file, it will load with the page and not cause a layout shift.此外,请确保添加此 CSS 内联而不是外部文件,它将与页面一起加载并且不会导致布局偏移。

If you have images the only way to not have layout shifts is to set them to fixed heights.如果您有图像,唯一没有布局变化的方法是将它们设置为固定高度。

This example shouldn't cause a layout shift, it loads the images inside a 16:9 container that can be calculated by the browser before any images load in. You can use the same principle, adjusting the padding-bottom on the sixteen-nine class to whatever propotions you need.此示例不应导致布局偏移,它将图像加载到 16:9 容器中,浏览器可以在加载任何图像之前计算该容器。您可以使用相同的原理,在 16-9 上调整 padding-bottom class 到您需要的任何比例。

<div style="max-width:500px;margin:0 auto;">
  <div class="layoutflex">
    <div class="topflex">
      <div class="sixteen-nine">
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/29841/dog.jpg" title="doggo" />
      </div>
    </div>
    <div class="bottomflex">
    <div class="sixteen-nine">
        <img src="https://i.imgur.com/Y8WHhk2.jpg" title="bear" />
      </div>
    </div>
  </div>
</div>

<style>
.layoutflex {
       display: flex;
       flex-direction: column;
       flex-shrink: 0;
    }

.layoutflex .topflex {
  order: 1;
  -ms-flex-order: 1;
  -webkit-order: 1;
}

.layoutflex .bottomflex {
  order: 0;
  -ms-flex-order: 0;
  -webkit-order: 0;
}

.sixteen-nine {
  padding-bottom:56.25%;position: relative
}
.sixteen-nine img {
  position: absolute;
  top:0;left: 0;
  width: 100%;height: 100%;
  object-position: center;
  object-fit: cover
}
</style>

Codepen: https://codepen.io/nonsintetic/pen/QWqNENd代码笔: https://codepen.io/nonsintetic/pen/QWqNENd

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

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