Largest Contentful Paint (LCP)

Browser Support

  • 77
  • 79
  • 122
  • x

Source

Historically, it's been a challenge for web developers to measure how quickly the main content of a web page loads and is visible to users. Older metrics likeloadorDOMContentLoadeddon't work well because they don't necessarily correspond to what the user sees on their screen. And newer, user-centric performance metrics likeFirst Contentful Paint (FCP)only capture the very beginning of the loading experience. If a page shows a splash screen or displays a loading indicator, this moment isn't very relevant to the user.

In the past, we've recommended performance metrics likeFirst Meaningful Paint (FMP)andSpeed Index (SI)(both available in Lighthouse) to help capture more of the loading experience after the initial paint, but these metrics are complex, hard to explain, and often wrong—meaning they still don't identify when the main content of the page has loaded.

Based on discussions in theW3C Web Performance Working Groupand research done at Google, we've found that a more accurate way to measure when the main content of a page is loaded is to look at when the largest element is rendered.

What is LCP?

LCP reports the render time of the largestimage or text blockvisible in the viewport, relative to when the user first navigated to the page.

What is a good LCP score?

To provide a good user experience, sites should strive to have Largest Contentful Paint of2.5 secondsor less. To ensure you're hitting this target for most of your users, a good threshold to measure is the75th percentileof page loads, segmented across mobile and desktop devices.

Good LCP values are 2.5 seconds or less, poor values are greater than 4.0 seconds, and anything in between needs improvement
A good LCP value is 2.5 seconds or less.

What elements are considered?

As currently specified in theLargest Contentful Paint API,the types of elements considered for Largest Contentful Paint are:

  • <img>elements (thefirst frame presentation timeis used for animated content such as GIFs or animated PNGs)
  • <image>elements inside an<svg>element
  • <video>elements (the poster image load time orfirst frame presentation timefor videos is used—whichever is earlier)
  • An element with a background image loaded using theurl()function, (as opposed to aCSS gradient)
  • Block-levelelements containing text nodes or other inline-level text element children.

Note that restricting the elements to this limited set was intentional in order to keep things simple in the beginning. Additional elements (like the full<svg>support) may be added in the future as more research is conducted.

As well as only considering some elements, LCP measurements use heuristics to exclude certain elements that users are likely to see as "non-contentful". For Chromium-based browsers, these include:

  • Elements with an opacity of 0, that are invisible to the user
  • Elements that cover the full viewport, that are likely considered as background rather than content
  • Placeholder images or other images with a low entropy, that likely don't reflect the true content of the page

Browsers are likely to continue to improve these heuristics to ensure we match user expectations of what the largestcontentfulelement is.

These "contentful" heuristics may differ from those used byFirst Contentful Paint (FCP),which may consider some of these elements, such as placeholder images or full viewport images, even if they are ineligible to be LCP candidates. Despite both using "contentful" in their name, the aim of these metrics is different. FCP measures whenany contentis painted to screen and LCP when themain contentis painted so LCP is intented to be more selective.

How is an element's size determined?

The size of the element reported for LCP is typically the size that's visible to the user within the viewport. If the element extends outside of the viewport, or if any of the element is clipped or has non-visibleoverflow,those portions don't count toward the element's size.

For image elements that have been resized from theirintrinsic size,the size that gets reported is either the visible size or the intrinsic size, whichever is smaller.

For text elements, LCP considers only the smallest rectangle that can contain all text nodes.

For all elements, LCP doesn't consider margins, paddings, or borders applied using CSS.

When is LCP reported?

Web pages often load in stages, and as a result, it's possible that the largest element on the page might change.

To handle this potential for change, the browser dispatches aPerformanceEntryof typelargest-contentful-paintidentifying the largest contentful element as soon as the browser has painted the first frame. But then, after rendering subsequent frames, it will dispatch anotherPerformanceEntryany time the largest contentful element changes.

For example, on a page with text and a hero image the browser may initially just render the text—at which point the browser would dispatch alargest-contentful-paintentry whoseelementproperty would likely reference a<p>or<h1>.Later, once the hero image finishes loading, a secondlargest-contentful-paintentry would be dispatched and itselementproperty would reference the<img>.

An element can only be considered the largest contentful element after it has rendered and is visible to the user. Images that haven't yet loaded aren't considered "rendered". Neither are text nodes using web fonts during thefont block period.In such cases, a smaller element might be reported as the largest contentful element, but as soon as the larger element finishes rendering, anotherPerformanceEntryis created.

In addition to late-loading images and fonts, a page may add new elements to the DOM as new content becomes available. If any of these new elements is larger than the previous largest contentful element, a newPerformanceEntrywill also be reported.

If the largest contentful element is removed from the viewport, or even from the DOM, it remains the largest contentful element unless a larger element is rendered.

The browser will stop reporting new entries as soon as the user interacts with the page (via a tap, scroll, or keypress), as user interaction often changes what's visible to the user (which is especially true with scrolling).

For analysis purposes, you should only report the most recently dispatchedPerformanceEntryto your analytics service.

Load time versus render time

For security reasons, the render timestamp of images is not exposed for cross-origin images that lack theTiming-Allow-Originheader. Instead, only their load time is exposed (since this is already exposed through many other web APIs).

This can lead to the seemingly impossible situation where LCP is reported by web APIs as earlier than FCP. This is not the case but only appears so due to this security restriction.

When possible, it's always recommended to set theTiming-Allow-Originheader, so your metrics will be more accurate.

How are element layout and size changes handled?

To keep the performance overhead of calculating and dispatching new performance entries low, changes to an element's size or position don't generate new LCP candidates. Only the element's initial size and position in the viewport is considered.

This means images that are initially rendered off-screen and then transition on-screen may not be reported. It also means elements initially rendered in the viewport that then get pushed down, out of view will still report their initial, in-viewport size.

Examples

Here are some examples of when the Largest Contentful Paint occurs on a few popular websites:

Largest Contentful Paint timeline from cnn.com
An LCP timeline from cnn.com.
Largest Contentful Paint timeline from techcrunch.com
An LCP timeline from techcrunch.com.

In both of the timelines above, the largest element changes as content loads. In the first example, new content is added to the DOM and that changes what element is the largest. In the second example, the layout changes and content that was previously the largest is removed from the viewport.

While it's often the case that late-loading content is larger than content already on the page, that's not necessarily the case. The next two examples show the LCP occurring before the page fully loads.

Largest Contentful Paint timeline from instagram.com
An LCP timeline from instagram.com.
Largest Contentful Paint timeline from google.com
An LCP timeline from google.com.

In the first example, the Instagram logo is loaded relatively early and it remains the largest element even as other content is progressively shown. In the Google Search results page example, the largest element is a paragraph of text that is displayed before any of the images or logo finish loading. Since all the individual images are smaller than this paragraph, it remains the largest element throughout the load process.

How to measure LCP

LCP can be measuredin the laborin the field,and it's available in the following tools:

Field tools

Lab tools

Measure LCP in JavaScript

To measure LCP in JavaScript, you can use theLargest Contentful Paint API.The following example shows how to create aPerformanceObserverthat listens forlargest-contentful-paintentries and logs them to the console.

new PerformanceObserver((entryList) => {
for (const entry of entryList.getEntries()) {
console.log('LCP candidate:', entry.startTime, entry);
}
}).observe({type: 'largest-contentful-paint', buffered: true});

In the above example, each loggedlargest-contentful-paintentry represents the current LCP candidate. In general, thestartTimevalue of the last entry emitted is the LCP value—however, that is not always the case. Not alllargest-contentful-paintentries are valid for measuring LCP.

The following section lists the differences between what the API reports and how the metric is calculated.

Differences between the metric and the API

  • The API will dispatchlargest-contentful-paintentries for pages loaded in a background tab, but those pages should be ignored when calculating LCP.
  • The API will continue to dispatchlargest-contentful-paintentries after a page has been backgrounded, but those entries should be ignored when calculating LCP (elements may only be considered if the page was in the foreground the entire time).
  • The API does not reportlargest-contentful-paintentries when the page is restored from theback/forward cache,but LCP should be measured in these cases since users experience them as distinct page visits.
  • The API does not consider elements within iframes but the metric does as they are part of the user experience of the page. In pages with an LCP within an iframe—for example a poster image on an embedded video—this willshow as a difference between CrUX and RUM.To properly measure LCP you should consider them. Sub-frames can use the API to report theirlargest-contentful-paintentries to the parent frame for aggregation.
  • The API measures LCP from navigation start, but forprerendered pagesLCP should be measured fromactivationStartsince that corresponds to the LCP time as experienced by the user.

Rather than memorizing all these subtle differences, developers can use theweb-vitalsJavaScript libraryto measure LCP, which handles these differences for you (where possible—note the iframe issue is not covered):

import {onLCP} from 'web-vitals';

// Measure and log LCP as soon as it's available.
onLCP(console.log);

Refer tothe source code foronLCP()for a complete example of how to measure LCP in JavaScript.

What if the largest element isn't the most important?

In some cases the most important element (or elements) on the page is not the same as the largest element, and developers may be more interested in measuring the render times of these other elements instead. This is possible using theElement Timing API,as described in the article oncustom metrics.

How to improve LCP

A full guide onoptimizing LCPis available to guide you through the process of identifying LCP timings in the field and using lab data to drill down and optimize them.

Additional resources

Changelog

Occasionally, bugs are discovered in the APIs used to measure metrics, and sometimes in the definitions of the metrics themselves. As a result, changes must sometimes be made, and these changes can show up as improvements or regressions in your internal reports and dashboards.

To help you manage this, all changes to either the implementation or definition of these metrics will be surfaced in thisChangelog.

If you have feedback for these metrics, you can provide it in theweb-vitals-feedback Google group.