Improve Your Lighthouse Score: Critical CSS, CLS and Images

Critical CSS, a layout shift fix, preconnect hints, smaller images and cache busting raised this site's Lighthouse performance score from 65 to 83. Each section lists the change and what it saved.

By
Brennan Kenneth Brown
Published
Reading time
7 minutes
Level
Intermediate
You need
  • A website you can edit
  • Chrome DevTools or Lighthouse
Lighthouse report showing a performance score of 83
The final Lighthouse score after optimization, better than the initial 65!

To improve your Lighthouse score, start from a measured baseline and change one thing at a time. I ran a PageSpeed Insights audit on my static Eleventy site and got a mobile performance score of 65, which is low for pre-built HTML on a CDN. My site loads three Google Fonts (PT Serif, Public Sans and JetBrains Mono, about 750ms to first paint), FontAwesome icons (about 340ms), 13 separate CSS files and some optional JavaScript (tooltips, a clickable post graph, a scroll progress bar, theme toggling). I chose to keep those design features, so this guide covers what I could fix without removing them. You can read more about those choices in my colophon.

Seven changes, each tested individually, took the score from 65 to 83.

The Starting Line

Here's what Lighthouse initially told me:

  • Performance: 65
  • First Contentful Paint (FCP): 4.2s
  • Largest Contentful Paint (LCP): 4.8s
  • Cumulative Layout Shift (CLS): 0.142
  • Estimated savings from render-blocking resources: 1,050ms
  • Estimated savings from image delivery: 71 KiB
  • Forced reflows: 74ms each, happening twice

The audit identified several key issues: render-blocking CSS, layout shifts, missing image dimensions, and some JavaScript performance problems.

1. Critical CSS: The Biggest Win (1,050ms savings)

The biggest culprit was render-blocking CSS. Since I split my CSS up, my site was now loading 13 CSS files synchronously, blocking the entire page render.

The fix is critical CSS: inline only the styles needed for above-the-fold content in the page, then load the rest asynchronously.

<!-- Critical CSS, inlined for instant rendering -->
<style>
/* Only the essential styles for header, hero, and initial layout */
</style>

<!-- Non-critical CSS, deferred -->
<link rel="stylesheet" href="/assets/css/01-variables.css" media="print" onload="this.media='all'">
<link rel="stylesheet" href="/assets/css/02-base.css" media="print" onload="this.media='all'">
<!-- ...and so on for the rest -->

I personally cringe a little at having styles directly in the <head>, but this shaved over a second off the initial render time. The page content appears instantly, with the full styles loading in shortly after.

2. Fix Layout Shift With Image Dimensions (CLS: 0.142 → 0.02)

Cumulative Layout Shift was my second biggest problem. The main culprit was missing dimensions on images.

When browsers don't know an image's dimensions ahead of time, they reserve zero space, then reflow the entire layout when the image loads. According to the audit, the images which could be optimized the most was the hero doodle and a couple 88x31 badges.

The fix was simple:

<!-- Before -->
<img src="/assets/images/improve-lighthouse-score/hero-doodle.png" alt="Lucky cat doodle" loading="lazy">

<!-- After -->
<img src="/assets/images/improve-lighthouse-score/hero-doodle.png" alt="Lucky cat doodle" loading="lazy" width="120" height="141">

I also fixed the Creative Commons icons in the footer, since they were also causing shifts:

<!-- Added explicit dimensions -->
<img src="https://mirrors.creativecommons.org/presskit/icons/cc.svg" alt="" width="16" height="16">

This dropped my CLS from 0.142 to around 0.02, a good improvement.

3. Preconnect Hints (310ms LCP savings)

Lighthouse suggested adding preconnect hints for external resources. I was already doing this for Google Fonts, but not for my analytics:

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preconnect" href="https://cloud.umami.is">

This addition saves 310ms on the Largest Contentful Paint, establishing early connections to the analytics domain.

4. Image Optimization (20KB reduction)

My images were larger than necessary. I didn't realize when I downloaded (stole) these badges that some were 10x larger than their display size.

I resized and optimized in the terminal with ImageMagick:

# Resize a badge from 880x310 to 88x31
convert this-site-kills-fascists.gif -resize 88x31 -strip this-site-kills-fascists.gif

# Optimize the hero image
convert hero-doodle.png -resize 120x141 -strip hero-doodle.png

Results:

  • this-site-kills-fascists.gif: 12.5KB → 2.1KB (83% reduction)
  • hero-doodle.png: 12KB → 5.7KB (52% reduction)

5. Fixing Forced Reflows (74ms × 2 savings)

My JavaScript for hover tooltips was causing forced reflows because it interleaved DOM reads and writes:

// Before: Causes layout thrashing
const rect = img.getBoundingClientRect();
const tooltipRect = tooltip.getBoundingClientRect();
tooltip.style.top = `${top}px`;
tooltip.style.left = `${left}px`;

// After: Batch reads and writes using requestAnimationFrame
requestAnimationFrame(() => {
  // Batch all DOM reads first
  const rect = img.getBoundingClientRect();
  const tooltipRect = tooltip.getBoundingClientRect();
  const scrollY = window.scrollY;
  
  // Calculate position
  let top = rect.top - tooltipRect.height - 10;
  let left = rect.left + (rect.width - tooltipRect.width) / 2;
  
  // Batch all DOM writes
  tooltip.style.top = `${top}px`;
  tooltip.style.left = `${left}px`;
});

The changes to this eliminate the 74ms delays happening on tooltip interactions.

6. FontAwesome Migration

I was loading FontAwesome from a CDN, which is an extra external dependency. I moved it to a local npm package:

npm install @fortawesome/fontawesome-free

Then created a local CSS file to import it:

/* src/assets/css/fontawesome.css */
@import url("fontawesome-all.css");

This gives more control over the loading and ensures it works with the new critical CSS strategy.

7. Cache Busting for All Assets

I implemented more cache busting using Eleventy's built-in filtering:

// .eleventy.js
eleventyConfig.addFilter("assetHash", (assetPath) => {
  const fileContents = fs.readFileSync(fullPath, 'utf8');
  const hash = crypto.createHash('md5').update(fileContents).digest('hex').substring(0, 8);
  return `${assetPath}?v=${hash}`;
});

Now, all assets get a hash-based query string:

<link rel="stylesheet" href="/assets/css/main.css?v=a1b2c3d4">
<script src="/assets/js/main.js?v=e5f6g7h8"></script>

The Results

Results after all of the above:

  • Performance score: 65 → 83
  • First Contentful Paint: 4.2s → 2.6s
  • Largest Contentful Paint: 4.8s → 2.8s
  • Cumulative Layout Shift: 0.142 → 0.192 (still room for improvement)
  • Total Blocking Time: 0ms → 30ms (a little worse)
  • Speed Index: 4.9s → 3.0s
  • Page weight: Reduced by ~20KB (with more savings possible)

The performance score went from 65 to 83. The First Contentful Paint and Largest Contentful Paint times are now under 3 seconds, which is a great improvement. While CLS increased slightly to 0.192 (still in the "good" range), the overall experience is faster and more responsive.

Here are the updated PageSpeed Insights results.

What did I learn about improving a Lighthouse score?

Critical CSS is worth it for the initial rendering improvement.

Always specify image dimensions, since CLS is a major UX factor.

Batch DOM operations when you can, requestAnimationFrame is your friend.

Test each change individually. A previous attempt changed my design in ways I didn't want, so this time I verified each step.

What else could improve the score?

These are the remaining opportunities Lighthouse reported:

Immediate Wins (2,870ms estimated savings)

  • FontAwesome is still render-blocking: FontAwesome CSS is still blocking initial render. The audit shows 2,870ms of potential savings by deferring it. (Update 1 below replaces it with inline SVGs.)
  • Font display optimization: Adding font-display: swap to FontAwesome fonts could save another 180ms.

Image Optimizations (48KB savings)

  • Convert brennan-day-alt.gif to video: The animated GIF is 103.6KB and could be reduced by 47.7KB by converting to MP4/WebM, but I prefer not to use .webm and left it as-is.
  • Minify FontAwesome CSS: Could save 4.8KB, but minifying CSS is a bit of an anti-pattern on the IndieWeb.
  • Reduce unused FontAwesome CSS: 20.4KB of FontAwesome CSS goes unused.

Other Considerations

  • Cache external assets: Several external badges (from aaronparecki.com, indieweb.org) have no cache headers, I need to download these instead of hotlinking.
  • DOM size optimization: The audit also suggests reducing the DOM size, which I haven't investigated.

That said, I am thinking about implementing more resource hints like prefetch and preload for critical resources, and maybe employing service workers for caching strategies.

My takeaway is that I will accept some bloat for a more fun user experience, and that each trade-off should be measured.

Update 1: How do I replace FontAwesome web fonts with inline SVGs?

After publishing, I migrated from FontAwesome web fonts, on a recommendation from Tyler Sticka, to the official @11ty/font-awesome plugin.

The difference is significant:

  • Before: Loading FontAwesome CSS (~22KB) + font files (~200KB) as render-blocking resources
  • After: Inline SVG sprites with only the icons you actually use

The plugin converts <i class="fa-solid fa-..."></i> markup into optimized SVG sprites that are:

  • Tree-shaken per page (only includes icons used on that page)
  • Rendered as inline SVGs (no font loading delay)
  • Smaller total payload (no unused icons)
  • Better accessibility (SVGs can be more easily made accessible)

This change eliminates render-blocking font files and reduces the total payload by over 200KB while maintaining the same visual appearance and functionality. The icons now appear instantly with the rest of the content, improving perceived performance.

Thanks to Tyler for the tip and to Zach Leatherman and the Eleventy team for the plugin.

Update 2: How do I restore icon colors after switching to SVG icons?

After migrating to SVG icons, I discovered that the rainbow-colored icons on my slash pages and accounts pages had lost their colors. This was because the SVG symbols use fill="currentColor", which required a different CSS approach than the old font icons.

Instead of trying to override fill on the SVG elements, I needed to set the color property on the parent SVG containers:

.verify-card h2 svg,
.verify-card .verify-links svg {
  color: var(--card-accent) !important;
}

SVG icons inherit color through currentColor rather than always accepting fill overrides, so set color on the parent.

Update 3: Why did desktop performance get worse, and how did I fix it?

After publishing, I discovered that while mobile performance improved significantly, desktop performance actually worsened. The critical CSS strategy that helped mobile was causing massive layout shifts on desktop, with a CLS score of 1.438!

Critical CSS included the grid layout change but not the actual sidebar styles. On fast desktop connections:

  1. Content rendered immediately in single-column layout
  2. When deferred CSS loaded, the sidebar appeared, shifting the main content dramatically
  3. This created a worse user experience on desktop than before optimization

The fix was to add essential sidebar styles to the critical CSS block:

  • Added .sidebar, .sidebar .module, and related styles inline
  • Added font-display: swap for all fonts to reduce font-loading shift
  • Added preconnect hint for status.lol to reduce network latency

Optimizations that help mobile can hurt desktop, so test across devices and network conditions. For related work, see Fix Common SEO and Accessibility Issues and Speed Up Eleventy Builds for build time.