Cumulative 布局 Shift
快速摘要
元素 on 你的 page move 之后初始渲染因为它们的维度是 不 reserved — 镜像, 字体, or ads 加载 and 推送内容围绕.
为什么发生
镜像不使用宽度 and 高度 attributes
Web 字体加载 and causing 文本到 reflow
Dynamically injected 内容 (ads, banners) pushing existing 内容
CSS animations 那个更改元素维度
最小示例
✗ 错误代码
<!-- No dimensions — causes layout shift when image loads -->
<img src="hero.jpg" alt="Hero">✓ 修复代码
<!-- Width and height reserve space immediately -->
<img src="hero.jpg" alt="Hero" width="800" height="600">
<!-- Or use CSS aspect-ratio -->
<img src="hero.jpg" alt="Hero" style="aspect-ratio: 4/3; width: 100%;">不使用宽度/高度, 浏览器做 不 know 如何 much 空间到 reserve. 当镜像加载, 它 pushes 内容下. Specifying 维度 reserves 空间 immediately.
如何诊断
运行 Lighthouse and 检查 CLS (Cumulative 布局 Shift) score
Observe page 加载缓慢地 — 注意哪个元素 move
检查镜像 for missing 宽度/高度 attributes
如何修复
总是 specify 宽度 and 高度 attributes on 镜像 and videos
Use 字体-display: swap or 可选 in @字体-face
Reserve 空间 for 动态内容使用最小值-高度 or aspect-比率
Use CSS transform for animations 相反的 更改布局属性
如何预防
总是包含宽度 and 高度 on media 元素
Use aspect-比率 CSS 到 reserve 空间 for responsive media
Preload 关键字体到 reduce 字体 swap 时间