Guest Una Kravets, Web DevRel @googlechrome, discusses layout shift in CSS, including different types of shifts, mitigating image load layout shift, effects of JavaScript and lazy loading, and combating layout shift using CSS.
Cumulative layout shift (CLS) measures the instability of a webpage's content caused by various factors like images, iframes, animations, and fonts, and can be mitigated by specifying sizes for elements, lazy loading, and using CSS grids.
There are three main types of layout shifts: load layout shift, post load layout shift, and expected layout shift, each impacting content stability and user experience, and field testing on actual devices can help in identifying and reducing CLS.
Deep dives
Understanding Cumulative Layout Shift (CLS)
Cumulative layout shift (CLS) is a key performance metric that measures the instability of a webpage's content. It penalizes experiences where content shifts, jumps, or moves while users are interacting with it. This can be frustrating, especially when it causes buttons to shift and gets clicked accidentally. CLS can be caused by various factors such as images, iframes/ads without dimensions, late-loaded third-party content, animations, and fonts. To mitigate CLS, it is important to specify sizes for images and iframes, lazy load CSS and fonts properly, and use CSS grids to define and allocate layout space. Interactions like focus or hover can also trigger layout shifts. Testing and debugging CLS can be done using tools like Lighthouse, which identifies offending elements and provides recommendations to reduce CLS. Performance Panel in DevTools can also highlight layout shifts on the page and show timing data.
Different Types of Layout Shifts
There are three main types of layout shifts: load layout shift, post load layout shift, and expected layout shift. Load layout shift occurs within the first five seconds after page load and is measured for CLS. Post load layout shift happens after five seconds, and expected layout shift occurs when users actively trigger layout changes like scrolling or changing tabs. Each type of layout shift can lead to content instability and impact the user experience.
Tips to Combat Cumulative Layout Shift (CLS)
To combat CLS, consider field testing on actual devices as network speed can affect timing and CLS results. Tools like Lighthouse can help identify offending elements and provide guidance on reducing CLS and improving other core web vitals metrics. Lighthouse offers screenshots of offending elements, but additional inspection might be needed to determine the cause of the shift. The Performance Panel in DevTools can highlight layout shifts on the actual page and provide insights in the timings area of the audits. Sharing the podcast and leaving reviews helps spread the knowledge and supports the creation of more content.
In this episode we're shifting the topic to layout shift, that moment where you watch the page adapt to something lazily loaded which has impacted the layout in some way where you see content shift around.