Sub topic 11
Keep the hero fast to load
This chapter assumes you can write HTML and CSS, know what a browser's network panel shows, and have built the hero from the earlier chapters: one h1, a subheadline, a call to action (CTA) and a product image. Here you make that hero appear quickly without changing what it says.
Why hero speed matters
The hero is the first thing a visitor sees. If the headline or image shows up late, the visitor waits and the offer is not communicated. The hero image is also usually the largest element in the first screen, so it often decides how fast the page feels.
Three measurements, from Google's Core Web Vitals, describe this:
| Metric | What it measures | Common "good" target |
|---|---|---|
| Largest Contentful Paint (LCP) | Time until the largest visible element is drawn. On a hero this is usually the image or the `h1`. | 2.5 seconds or less |
| Cumulative Layout Shift (CLS) | How much visible content jumps around while the page loads. | 0.1 or less |
| Interaction to Next Paint (INP) | How quickly the page responds after a click or tap. | 200 milliseconds or less |
LCP and CLS are the two the hero controls most. INP matters when you add scripts, such as a carousel or a video player, to the hero.
What the browser does before it paints the hero
The browser cannot draw the hero until it has done several things in order. Each one is a place to lose time.
Three points follow from this flow:
- CSS blocks painting. The browser will not draw anything until the CSS it needs has loaded. Large stylesheets delay the headline.
- The image is only downloaded after the browser finds it. If it is found late, the download starts late.
- Text can paint before the image. This is why a text-only offer reaches the visitor faster than a hero that depends on a picture.
Make the hero image load early
Use a normal image element for the hero
The hero image in the earlier chapters is an img element in the HTML. This lets the browser find it while it scans the page. If you hide the image inside a CSS background-image, the browser only learns about it after it has downloaded and processed the CSS. That adds a delay.
Use img when the picture carries meaning, such as a product screenshot. Use a CSS background only for decoration.
Tell the browser the image is important
The fetchpriority="high" attribute asks the browser to download this image before other images. The hero example already uses it:
<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">Do not add loading="lazy" to the hero image. Lazy loading tells the browser to wait until the image is near the visible area. The hero is already visible, so lazy loading only adds a delay. Reserve loading="lazy" for images further down the page.
| Image position | `loading` | `fetchpriority` |
|---|---|---|
| Hero, above the fold | Leave unset (eager is the default) | `high` |
| Logo strip just below the hero | `lazy` is usually fine | Leave unset |
| Far down the page | `lazy` | Leave unset |
Preload only when the image is hidden from the scanner
If the hero image must come from CSS, or the page builds it with JavaScript, you can announce it in the head:
<link rel="preload" as="image" href="/img/invoice-editor.webp" fetchpriority="high">If the image is already a plain img in the HTML, a preload adds little. It can also cause the file to be requested twice when the URL differs, for example when srcset selects a different file from the one you preloaded.
Reduce the size of the image
A faster start does not help if the file is large. Three choices make the biggest difference.
Pick a modern format
WebP and AVIF usually produce smaller files than JPEG or PNG at similar quality. For a screenshot with text, check the result at full size, because heavy compression blurs small type. Keep a JPEG or PNG fallback if you need to support old browsers, using the picture element:
<picture>
<source type="image/avif" srcset="/img/invoice-editor.avif">
<source type="image/webp" srcset="/img/invoice-editor.webp">
<img class="hero__image" src="/img/invoice-editor.png" width="640" height="400" fetchpriority="high" alt="Ledgerly invoice editor showing a draft invoice with a Send button">
</picture>The browser picks the first source it supports. The img inside is the fallback and also carries the attributes, including alt.
Serve sizes that match the screen
A phone with a 390 px wide viewport does not need a 1600 px wide image. Provide several widths with srcset and tell the browser how wide the image will be with sizes:
<img
class="hero__image"
src="/img/invoice-editor-640.webp"
srcset="/img/invoice-editor-480.webp 480w,
/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">Here sizes matches the CSS from the layout chapter. Below 48rem the image fills the width of the screen (100vw). At 48rem and above, the hero uses two columns, so the image takes about half the viewport (50vw). The 1280w file serves high-density screens, which need roughly twice the pixels.
If sizes does not match the real layout, the browser may pick a file that is too large or too small. When the image looks soft or the download is larger than expected, check sizes first.
Compress with a target in mind
Set a rough budget for the hero image, for example under 100 KB on mobile for a screenshot, and check it in the network panel. A budget turns "make it smaller" into a pass or fail check.
Prevent layout shift
Layout shift happens when the browser does not know how much room to reserve for an element. The headline paints, then the image arrives and pushes the text down. The visitor may be about to tap the CTA when it moves.
Two habits prevent this:
- Always set
widthandheighton theimg. Modern browsers use these two numbers to compute an aspect ratio and reserve the space before the file loads. - Keep
height: autoin the CSS so the image still scales with its container.
The hero CSS from the earlier chapter does both:
.hero__image {
width: 100%;
height: auto;
}If the image is placed in a container with a fixed shape, aspect-ratio does the same job:
.hero__media {
aspect-ratio: 16 / 10;
background: #eef2ff;
}The background color shows while the image loads, so the space does not look empty.
Make text appear fast
The headline and CTA carry the offer, so they should not wait for the image or for a custom font.
Handle web fonts
A custom font file must download before the browser can use it. The font-display descriptor controls what the visitor sees meanwhile:
@font-face {
font-family: "Inter";
src: url("/fonts/inter-var.woff2") format("woff2");
font-display: swap;
font-weight: 100 900;
}With swap, the browser shows the text at once in a fallback font, then switches when the custom font arrives. The switch can shift the text slightly if the two fonts have different widths. To reduce that, choose a fallback with similar proportions, load only the weights the hero uses, and serve the font as woff2 from your own domain.
Keep the critical CSS small
Because CSS blocks painting, a 300 KB stylesheet delays the headline. For a marketing page you can:
- Remove unused rules.
- Inline the small amount of CSS the hero needs in a
styleelement in thehead, and load the rest after. - Avoid
@importin CSS, because each import is fetched only after the file that contains it.
Inlining adds complexity to your build. Do it when measurement shows the stylesheet is a real cost, not by default.
Be careful with scripts, video and sliders
A hero that autoplays a background video or rotates through slides can look impressive. It also tends to be the slowest hero.
- A video file is far larger than an image. If you use one, set a
posterimage so something appears at once, and keep the offer in text over the top rather than inside the video. - A carousel loads several images and a script before it works. Only the first slide counts for LCP, and splitting attention across slides weakens the single offer you defined earlier.
- Third-party scripts such as chat widgets and tag managers compete with your hero for the network and the main thread. Load them with
deferorasync, or after the page becomes interactive.
A static image or a single clear screenshot is usually the fastest and the clearest choice.
Measure instead of guessing
Change one thing, then measure.
- Open Lighthouse in Chrome DevTools and run it on a mobile profile. Read the LCP value and the "LCP element" it identifies.
- In the Network panel, throttle to a "Slow 4G" profile and reload. Check when the hero image starts and ends, and how large it is.
- In the Performance panel, look for layout shift markers around the hero.
- Where possible, compare with field data, which comes from real visitors, for example in Chrome's CrUX report or PageSpeed Insights. Lab tests on your fast laptop can look better than what visitors experience.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| `loading="lazy"` on the hero image | The image waits to start downloading | Remove it from above-the-fold images |
| Hero image set as CSS `background-image` | The browser finds it late | Use an `img`, or preload it |
| Missing `width` and `height` | Text jumps when the image arrives | Add both attributes |
| One 2400 px image for every device | Phones download far more than needed | Use `srcset` and `sizes` |
| Wrong `sizes` value | Browser picks a poor candidate | Match `sizes` to the CSS layout |
| Preloading a different URL than the one `srcset` selects | Two downloads | Preload the same file, or skip the preload |
| Autoplay video with no poster | Empty area at first paint | Add a `poster` and keep text as HTML |
Summary of the workflow
- Run Lighthouse and note the LCP element.
- Make sure that element is an
imgin the HTML, not a late-discovered background. - Add
fetchpriority="high"and removeloading="lazy". - Export in WebP or AVIF with
srcsetwidths and a correctsizes. - Set
widthandheightto prevent shifts. - Load fonts with
font-display: swapand trim the CSS. - Re-measure on a throttled mobile profile.
Exercise
Your hero image is declared in CSS as .hero { background-image: url(/img/hero.jpg); }. Lighthouse reports an LCP of 4.1 seconds and names this image as the LCP element. List three changes you would make, in order, and say what each one fixes.
Show answer
1) Replace the background with an img element in the HTML (with a meaningful alt), so the browser discovers the image while scanning the HTML instead of after the CSS is downloaded and processed. This fixes the late start. 2) Add fetchpriority="high", and make sure there is no loading="lazy", so the browser downloads the image ahead of other resources. This fixes the priority. 3) Convert the file to WebP or AVIF and add srcset and sizes, so phones download a smaller file. This fixes the download time. Add width and height as well to avoid layout shift. Then re-run Lighthouse to confirm LCP improved.
Exercise
A hero uses srcset with 480w, 640w and 1280w files and sizes="100vw". At widths of 48rem and above, the CSS places the image in a two-column grid, so it occupies about half the viewport. What problem does this cause, and how do you fix it?
Show answer
The sizes value tells the browser the image will be as wide as the whole viewport, but it is actually about half that wide. The browser therefore selects a larger file than needed on desktop screens, wasting bandwidth and slowing LCP. Fix it by making sizes match the layout: sizes="(min-width: 48rem) 50vw, 100vw".
Exercise
The headline jumps down about 250 px when the hero image finishes loading. Name the cause and two ways to fix it.
Show answer
The browser did not know the image's dimensions, so it reserved no space and the image pushed the text when it arrived (layout shift). Fixes: set width and height attributes on the img with height: auto in CSS so the browser computes the aspect ratio in advance, or give the image container an aspect-ratio in CSS.