You are currently viewing How to Improve Largest Contentful Paint (LCP)

How to Improve Largest Contentful Paint (LCP)

Largest Contentful Paint (LCP) is the measurement of how long a browser takes to render the largest text or image block in the viewport. More simply, it estimates how soon users can see above-the-fold content.

In this article, we look at techniques that improve LCP render times.

Quickly jump to a topic:
Critical CSS and Asynchronous Asset Loading
Optimize images
Reducing server response time

Critical CSS and Asynchronous Asset Loading

Critical CSS is the set of styles required to display the pageโ€™s structure, look, and feel displayed in the viewport or above-the-fold. These are the only styles that should be loaded synchronously on any given page, embedded into the HTML output, not a separate file.

Critical CSS example for portent.com

There are plenty of tools to generate a pageโ€™s critical CSS. Typically, building critical CSS generation into a pre-processor system for the web application makes the most sense. Node.js libraries like critical and critical-css are popular solid options. Some online apps and tools can generate it for you.

The rest of the CSS for the page should be loaded asynchronously after page load. That eliminates CSS as a render-blocking resource and streamlines the browserโ€™s ability to download and render the necessary assets to show the above-the-fold content as quickly as possible. That is Largest Contentful Paint.

Portent utilizes Autoptimize to handle our asynchronous CSS loading, which implements the loadCSS library by the Filament Group.

It is also important to asynchronously load (or defer) JavaScript. That eliminates JavaScript as a render-blocking resource. Eliminating render-blocking resources is an essential foundational topic for most of the Core Web Vitals metrics. We discuss it in further detail in our article about improving First Input Delay (FID) and First Contentful Paint (FCP).

Optimize Images

First time optimizing your site for speed? Start with images. Quite often, images will be the first, biggest bandwidth hogs. Theyโ€™re also some of the most significant, easiest speed improvements you can make because the fewer assets there are to download, the faster.

Images are almost always found in above-the-fold content, so it is important to optimize them for the web. Using the right type, compressing, and properly sized images will reduce extra bloat your browser will have to download, which will aid in a faster LCP score.

#1: Use the Right File Format

There are two widely-used image file formats:

JPEG or JPG (Joint Photographic Experts Group, pronounced jay-peg) refer to the same image format. Weโ€™ll use โ€˜JPGโ€™ here. JPG files should have the file extension .jpeg or .jpg. JPG is usually best for images with thousands or millions of colors, like photographs. Those tend to be the biggest files, and JPG is a compression powerhouse.

Use JPG for photographs and photographic images. JPG is known as lossy compression. It reduces file size by removing data. Thatโ€™s why JPG compression is so effective for humungous, many-colored images. It deletes data. That can reduce quality, but done right, JPG compression is nearly undetectable.

PNG (Portable Network Graphics, pronounced ping or pee-en-gee) is ideal for line-art and other images with fewer colors.

All of these file types refer to the way the image files are compressed.

Use PNG for line art or images where your design requires transparent backgrounds. PNG is a lossless file format. It works by rearranging data to more efficiently use file space. It doesnโ€™t reduce the photographic image file size that much. But for line art and images with few colors, itโ€™s fantastic. And it preserves transparency.

Choosing the right format makes a huge difference.

Hereโ€™s an entirely uncompressed photograph:

An uncompressed image: 2.7 megabytes

An uncompressed image: 2.7 megabytes

Hereโ€™s a photograph compressed using PNG. The file size is actually larger:

The same image, losslessly compressed: 4.1 megabytes

The same image, losslessly compressed: 4.1 megabytes

Clearly, JPG is the best format for photographs like this.

PNG, though, excels with simpler images and icons. Hereโ€™s the Portent logo as a transparent PNG. Itโ€™s a 12kb file:

32-bit PNG, transparent: 12kb

And here it is as a JPG. Itโ€™s almost 2x larger, at 22kb:

JPG file: 22kb

JPG file: 22kb

Choosing the right format has an immediate impact.

#2: Compress Your Images

Choosing the right format is the first step. Use that formatโ€™s compression algorithm, and you can see even bigger savings.

You can increase or decrease the โ€˜lossinessโ€™ of jpg compression. Remember the previous example? That image was 2.7 megabytes in JPG format.

Compress it 20%, though, and you can get the file size down to 400kb.

Lightly compressed JPG format: 400kb

Hard to tell the difference, but itโ€™s less than one-sixth the size. Thatโ€™s a huge savings.

You can go too far with JPG, though. The more you compress, the more data JPG removes, and the more the image starts to look like this:

Super compressed JPG format: 111kb - but is it worth it?

Itโ€™s a balancing act. Compress as much as you can but always be aware of context. Itโ€™s not just about speed, after all. Images are there to deliver a message. A pixelated image may not deliver the message you want.

You can also change PNG 32 to PNG 24 to PNG 8. Each removes colors from the image. Hereโ€™s the Portent logo again, still transparent but converted from a PNG 32 to a PNG 8. Itโ€™s now 7kb:

8-bit PNG, transparent: 7kb

A caveat: PNG may be the better format for really small photos.

Testing and Compressing Images

You can use Google Page Speed to test images on your page. Run Page Speed Insights. Then look for โ€˜Efficiently encode images.โ€™

If โ€˜efficiently encode imagesโ€™ does not appear under โ€˜opportunities,โ€™ nice! You donโ€™t have to worry about your images. If youโ€™re like the rest of us, though, youโ€™ll see a few:

PageSpeed Insights Encode Images

You can see all of the compressed images you need on the page. Handle these and see your performance scores improve.

PageSpeed Insights: Passed Encoding Images

Image Compression Tools

If you want more effective compression than Google Page Speed Insights will provide, use one of these tools:

  1. Adobe Photoshop. Professional image software.
  2. Squoosh. Popular free and web-based compression app.
  3. EWWW Image Optimizer. A quality (and free) image optimization plugin for WordPress.
  4. Imageoptim on the Mac. Far and away the best easy-to-use image compressor youโ€™ll find. Itโ€™s what we used for the examples above.
  5. Caesium on the PC. Not quite as easy, but still awfully good.

We ran Imageoptim PNG optimization on our 8-bit PNG logo, and it reduced it by 2kb to 5kb:

8-bit PNG optimized, transparent: 5kb. Much better for page speed.

#3: Resize Images

Images on a web page have dimensions: Width and height. You can set those dimensions two ways:

  • Using your photo editing software
  • Using the HTML IMG tagโ€™s height and width attributes

If you use the latter to squash an image down to size, youโ€™re wasting bandwidth. Say youโ€™re displaying a 400 x 400 pixel image on your page, and the original is 1200 x 1200 pixels.

If you deliver that file at 1200 x 1200 pixels and then set the IMG height and width attributes to 400 x 400, youโ€™re still delivering the larger file and using all that bandwidth.

Resize it in your photo editor, though, and you deliver the 400 x 400 image.

The difference? Hereโ€™s our example image, resized in a photo editor to 400 pixels wide:

80% jpg, resized: 51kb. This image will load faster.

Only deliver what you need.

Reducing Server Response Time

Server response time, often known by time to first byte (TTFB), is the time it takes the web server to respond to a browserโ€™s request, which includes the HTML, CSS, JavaScript, font files, and any other assets requested. Reducing the amount of time this takes is a fundamental principle of improving page speed. It will improve all facets of page speed, including Core Web Vitals metrics โ€” Largest Contentful Paint (LCP), First Input Delay (FID), and First Contentful Paint (FCP).

You can make many improvements to your server response times, including page caching, enabling keep-alive, and compressing transferred data. We do a deep dive into server optimizations in our article on how to improve server response times.

Conclusion

Implementing the topics discussed in this article will improve LCP scores, enhance the user experience, and contribute to site conversion goals. When combined with best practices for quickening server response time, improving First Input Delay (FID) and First Contentful Paint (FCP), and reducing Cumulative Layout Shift (CLS), the performance results will be enterprise level.

The post How to Improve Largest Contentful Paint (LCP) appeared first on Portent.