Fast background images

A large hero background is often the Largest Contentful Paint element. Here is how to get it on screen fast without giving up the design.

6 min read
Fast background images
In short
  • Good LCP is 2.5 seconds or less, measured at the 75th percentile of page loads.
  • The preload scanner cannot see URLs inside CSS, so hero backgrounds start downloading late.
  • Preload the hero, or move it into an img element with object-fit: cover.
  • Serve AVIF or WebP with a JPEG fallback, sized for each breakpoint. Never lazy load the hero.
≤ 2.5 sGood Largest Contentful Paint
> 4 sPoor Largest Contentful Paint
75thPercentile used to assess LCP

Why background images count for LCP

Largest Contentful Paint measures when the largest image or text block in the viewport finishes rendering. It is one of the Core Web Vitals. The thresholds are fixed:

LCPRating
2.5 s or lessGood
Over 2.5 s up to 4 sNeeds improvement
Over 4 sPoor

The rating uses the 75th percentile of real page loads, split by mobile and desktop. So the slow quarter of your visitors sets the score.

An element with a background image loaded through url() is an LCP candidate. A full width hero with a photo is usually the largest thing above the fold. In practice, the hero background often is the LCP element. Gradients do not count, because they are not loaded images.

The discovery problem

Browsers run a preload scanner. It reads raw HTML ahead of the main parser and starts fetching images, scripts and stylesheets it finds in the markup. This is a large part of why pages load fast.

The scanner does not read CSS. A URL inside background-image stays hidden until a longer chain completes:

  1. The HTML arrives and the stylesheet link is found.
  2. The stylesheet downloads and is parsed.
  3. The browser matches the rule to an element in the DOM and computes its style.
  4. Only then does the image request start.

Every step adds delay. If the stylesheet is imported from another stylesheet, or the class is added by a script, the delay grows. An <img> in the HTML can start downloading almost immediately. The same image as a CSS background can start hundreds of milliseconds later on a slow connection.

Render blocking makes it worse. The page cannot paint until its stylesheets arrive, and the background request cannot start until they are parsed. Keep the critical CSS small, avoid @import chains, and put the hero rule in the first stylesheet the page loads.

Abstract blue background with wavy lines, original variant

Two fixes: preload, or use an img

Option one: preload the background. Add a preload hint in the document head. It tells the browser about the file before the CSS is parsed.

<link rel="preload" as="image" href="/img/hero-1600.jpg"
      imagesrcset="/img/hero-800.jpg 800w, /img/hero-1600.jpg 1600w"
      imagesizes="100vw" fetchpriority="high">

The preloaded URL must match the URL the CSS asks for. If they differ, the browser downloads twice and the preload wastes bandwidth. If your CSS switches files in media queries, use the media attribute on separate preload links so each viewport fetches only its own file.

Option two: use an <img> that behaves like a background. Position it behind the content and let object-fit do the work of background-size.

.hero { position: relative; }
.hero > img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  z-index: -1;
}

The image is now visible to the preload scanner. It also gets srcset, sizes, <picture> sources and fetchpriority="high" with no extra hints. Give it alt="" if it is purely decorative. This option is usually more robust than a preload, because there is only one place to keep in sync.

Formats and image-set()

Modern formats cut bytes at the same visual quality. AVIF usually produces the smallest files. WebP is close and encodes faster. JPEG remains the universal fallback. For soft, photographic backgrounds such as the bokeh backgrounds, the savings from modern formats are often large, because smooth areas compress well.

In CSS, image-set() lets the browser choose a format or a resolution. Write a plain fallback first, so older browsers that do not understand the second declaration still get an image.

.hero {
  background-image: url("hero.jpg");
  background-image: image-set(
    url("hero.avif") type("image/avif"),
    url("hero.webp") type("image/webp"),
    url("hero.jpg") type("image/jpeg")
  );
}

The browser takes the first option whose type it supports. You can also use resolution descriptors such as 1x and 2x to serve sharper files to high density screens. Keep in mind that a preload hint cannot express the same type choice, so with image-set() the <picture> route is easier to optimize.

9 bokeh backgrounds, coral variant

Size per breakpoint and compression targets

Every file on this site is 1600×1200 px. That suits a desktop hero. A phone with a 390 px wide viewport needs far less, even at 2x density. Export smaller versions and swap them by width.

.hero { background-image: url("hero-800.webp"); }
@media (min-width: 900px) {
  .hero { background-image: url("hero-1600.webp"); }
}

With an <img>, srcset with width descriptors and sizes="100vw" does this automatically.

There is no official byte budget, but practical targets help:

  • Aim for a hero background under roughly 200 KB on desktop and under 100 KB on mobile.
  • Start JPEG and WebP exports around quality 70 to 80, and lower until you see artifacts.
  • Images under text can go lower still, since a scrim or blur hides detail.
  • Strip metadata. Camera and editor data adds bytes and no pixels.

Dark, low contrast images like the blue wavy lines background compress well. Busy images with fine detail, like grunge or foliage, need more bytes for the same quality. Judge each file on its own.

Lazy loading tradeoffs

Native lazy loading, loading="lazy", works only on <img> and <iframe>. CSS backgrounds have no equivalent attribute. Browsers request a background image once the element is rendered, which for a normal block means on page load, even far below the fold.

That gives you a set of rules:

  • Never lazy load the hero. A lazy LCP image waits for layout before it starts, which delays LCP directly.
  • Lazy load images further down. Use loading="lazy" on below the fold <img> elements.
  • Defer heavy CSS backgrounds with a class. An observer can add a class when a section nears the viewport, and the class sets the image.
  • Always set a background color. It fills the box while the image loads and prevents a white flash.

A decorative band like the synthwave grid background in a footer is a good candidate for deferral. Explore other options in abstract backgrounds and test each one with your real layout.

Measure before and after

Optimization without measurement is guesswork. Check three things before you change anything, and again after.

  1. Which element is LCP. Your browser's developer tools show the LCP element in the performance panel. Confirm it is the hero before you tune the hero.
  2. When the image request starts. In the network waterfall, look at the gap between the HTML response and the image request. A long gap is the discovery problem.
  3. How long the download takes. A late start points to discovery. A long download points to file size.

Test on a throttled mobile profile, not only on a fast desktop connection. The 75th percentile is usually a mid range phone on a mobile network. Lab tests show you the mechanics. Field data from real visitors shows whether the score moved.

Fix discovery first, then bytes. A small file that starts late can still miss the 2.5 second mark.

Questions

Do CSS background images count toward LCP?

Yes. An element whose background image is loaded with url() is an LCP candidate. Gradients are not, because nothing is downloaded. If a hero photo is the largest visible element, its load time sets your Largest Contentful Paint.

Is preload or an img element better for a hero?

Both work. An img with object-fit: cover is usually simpler, since srcset, sizes, picture sources and fetchpriority stay in one place. A preload hint is the right choice when the background must stay in CSS, but its URL has to match exactly.

Should I lazy load background images?

Never lazy load anything that appears in the first viewport, and never the LCP image. Below the fold, lazy loading saves bandwidth. For CSS backgrounds, set the image through a class that a script adds when the section approaches the viewport.

Keep reading

Built for the web.
Free to download.