files/en-us/web/api/layoutshift/index.md
{{APIRef("Performance API")}}{{SeeCompatTable}}
The LayoutShift interface of the Performance API provides insights into the layout stability of web pages based on movements of the elements on the page.
A layout shift happens when any element that is visible in the viewport changes its position between two frames. These elements are described as being unstable, indicating a lack of visual stability.
The Layout Instability API provides a way to measure and report on these layout shifts. All tools for debugging layout shifts, including those in the browser's developer tools, use this API. The API can also be used to observe and debug layout shifts by logging the information to the console, to send the data to a server endpoint, or to web page analytics.
Performance tools can use this API to calculate a {{glossary("CLS")}} score.
{{InheritanceDiagram}}
This interface extends the following {{domxref("PerformanceEntry")}} properties by qualifying them as follows:
0 (the concept of duration does not apply to layout shifts)."layout-shift"."layout-shift".This interface also supports the following properties:
true if {{domxref("LayoutShift.lastInputTime", "lastInputTime")}} is less than 500 milliseconds in the past.0 if no excluding input has occurred.The following example shows how to capture layout shifts and log them to the console.
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
// Count layout shifts without recent user input only
if (!entry.hadRecentInput) {
console.log("LayoutShift value:", entry.value);
if (entry.sources) {
for (const { node, currentRect, previousRect } of entry.sources)
console.log("LayoutShift source:", node, {
currentRect,
previousRect,
});
}
}
}
});
observer.observe({ type: "layout-shift", buffered: true });
{{Specifications}}
{{Compat}}