Sub topic 11
Keep the hero fast to load
This chapter assumes you can write basic HTML and CSS and know what a browser's loading sequence looks like at a high level. The hero is usually the first thing a visitor sees. If it loads slowly or jumps around, the offer you wrote is not readable when it matters.
What to measure
Two metrics matter most for a hero:
- Largest Contentful Paint (LCP) is the time until the largest visible element is drawn. In a hero this is usually the headline or the hero image. A good LCP is 2.5 seconds or less.
- Cumulative Layout Shift (CLS) measures how much visible content moves while the page loads. A good CLS is 0.1 or less.
The headline is text, so it paints as soon as the HTML and CSS arrive. The image is the usual cause of a slow LCP.
Give the image its size
The browser needs to know how much space to reserve before the image arrives. Without that, the text below it jumps down when the image loads, which raises CLS.
Set width and height attributes to the image's real pixel size. Then let CSS scale it:
.hero__image {
width: 100%;
height: auto;
}The browser uses the two attributes to compute an aspect ratio, so the space is reserved even when CSS makes the image narrower.
Tell the browser the image is important
Browsers lazy-load or delay many images. The hero image should be the opposite. Use fetchpriority="high" on it, and do not add loading="lazy".
<img class="hero__image"
src="/img/invoice-editor.webp"
width="640" height="400"
fetchpriority="high"
alt="Ledgerly invoice editor showing a draft invoice with a Send button">Lazy-loading an above-the-fold image is a common mistake. The browser waits until layout is done before it requests the file, so the LCP gets later.
Choose the right file
- Use a modern format such as WebP or AVIF. They are usually much smaller than JPEG or PNG at similar quality.
- Export at the size you display. A 640-pixel-wide slot does not need a 3000-pixel-wide file.
- Offer a larger version for high-density screens with
srcsetandsizes, so phones do not download the desktop file.
<img class="hero__image"
src="/img/invoice-editor-640.webp"
srcset="/img/invoice-editor-640.webp 640w,
/img/invoice-editor-1280.webp 1280w"
sizes="(min-width: 48rem) 50vw, 100vw"
width="640" height="400"
fetchpriority="high"
alt="Ledgerly invoice editor showing a draft invoice with a Send button">The sizes value matches the layout from the earlier chapter: the image fills the full width on small screens and about half of it once the hero has two columns.
Keep text fast too
- Avoid hiding the headline until a script runs. Text that appears only after JavaScript loads delays the LCP.
- If you use a custom web font, add
font-display: swapso the headline shows in a fallback font first. Keep the number of font files small. - Do not put a large autoplaying video in the hero unless the offer needs it. If you do, supply a lightweight poster image.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| `loading="lazy"` on the hero image | Later LCP | Remove it and add `fetchpriority="high"` |
| Missing `width` and `height` | Layout shift | Add the real pixel dimensions |
| Oversized source file | Slow download on mobile | Resize and use `srcset` |
| Headline rendered by JavaScript | Text appears late | Put it in the HTML |
Check your result
Run Lighthouse in Chrome DevTools, or use PageSpeed Insights, on a throttled mobile profile. Look at the LCP element it reports. If it names your hero image, the changes above apply directly. If it names the headline, check fonts and render-blocking scripts first.
Exercise
A hero image has loading="lazy", no width or height, and is a 3000-pixel JPEG shown in a 640-pixel slot. Name three changes that improve loading, and say which metric each one helps.
Show answer
Remove loading="lazy" and add fetchpriority="high" (improves LCP). Add width="640" and height="400", or the real dimensions, so space is reserved (improves CLS). Export a smaller WebP or AVIF at about the display size, with srcset for larger screens (improves LCP by shortening the download).