LayoutShiftAttribution

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The LayoutShiftAttribution interface of the Layout Instability API provides debugging information about elements which have shifted.

Instances of LayoutShiftAttribution are returned in an array by calling LayoutShift.sources.

Instance properties

LayoutShiftAttribution.Node Read only

Returns the element that has shifted (null if it has been removed).

LayoutShiftAttribution.previousRect Read only Experimental

Returns a DOMRectReadOnly object representing the position of the element before the shift.

LayoutShiftAttribution.currentRect Read only Experimental

Returns a DOMRectReadOnly object representing the position of the element after the shift.

Instance methods

LayoutShiftAttribution.toJSON() Experimental

Returns a JSON representation of the LayoutShiftAttribution object.

Examples

The following example finds the element that is causing the largest layout shift, and prints that node to the console. For more detail on this see Debug Web Vitals in the field.

function getCLSDebugTarget(entries) {
  const largestEntry = entries.reduce((a, b) => a && a.value > b.value ? a : b);
  if (largestEntry?.sources?.length) {
    const largestSource = largestEntry.sources.reduce((a, b) => {
      const area = (el) => el.previousRect.width * el.previousRect.height;
      return a.node && area(a) > area(b) ? a : b;
    });
    if (largestSource) {
      return largestSource.node;
    }
  }
}

Specifications

Specification
Layout Instability
# sec-layout-shift-attribution

Browser compatibility

BCD tables only load in the browser

See also