The LayoutShift
interface of the Layout Instability API provides insights into the stability of web pages based on movements of the elements on the page.
Properties
LayoutShift.value
- Returns the
impact fraction
(fraction of the viewport that was shifted) times thedistance fraction
(distance moved as a fraction of viewport). LayoutShift.hadRecentInput
- Returns
true
if there was a user input in the past 500 milliseconds. LayoutShift.lastInputTime
- Returns the time of the most recent user input.
LayoutShift.sources
- Returns an array of
LayoutShiftAttribution
objects with information on the elements that were shifted.
Methods
LayoutShift.toJSON()
- Converts the properties to JSON.
Examples
The following example shows how to capture layout shifts and log them to the console.
Note that in this example data is only sent to the server when the user leaves the tab.
// Catch errors since some browsers throw when using the new `type` option. // https://bugs.webkit.org/show_bug.cgi?id=209216 try { let cumulativeLayoutShiftScore = 0; const observer = new PerformanceObserver((list) => { for (const entry of list.getEntries()) { // Only count layout shifts without recent user input. if (!entry.hadRecentInput) { cumulativeLayoutShiftScore += entry.value; } } }); observer.observe({type: 'layout-shift', buffered: true}); document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'hidden') { // Force any pending records to be dispatched. observer.takeRecords(); observer.disconnect(); console.log('CLS:', cumulativeLayoutShiftScore); } }); } catch (e) { // Do nothing if the browser doesn't support this API. }
Specifications
Specification | Status | Comment |
---|---|---|
Layout Instability API The definition of 'LayoutShift' in that specification. |
Editor's Draft | Initial definition. |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
LayoutShift | Chrome Full support 77 | Edge Full support 80 | Firefox No support No | IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 77 | Chrome Android Full support 77 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support 12.0 |
hadRecentInput | Chrome Full support 77 | Edge Full support 80 | Firefox No support No | IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 77 | Chrome Android Full support 77 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support 12.0 |
lastInputTime | Chrome Full support 77 | Edge Full support 80 | Firefox No support No | IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 77 | Chrome Android Full support 77 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support 12.0 |
toJSON | Chrome Full support 77 | Edge Full support 80 | Firefox No support No | IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 77 | Chrome Android Full support 77 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support 12.0 |
value | Chrome Full support 77 | Edge Full support 80 | Firefox No support No | IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 77 | Chrome Android Full support 77 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support 12.0 |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.