Sub topic 12
Hero images without slowing the page
This chapter assumes you can write basic HTML and CSS and can open your browser's developer tools. It builds on the hero section from the earlier chapter, which has a heading, supporting text, a call to action, and an image. Here the focus is the image. It is usually the largest file on the page, and it often decides how fast the page feels.
Why the hero image matters
The hero image is usually the largest visible element when the page first loads. Browsers measure this as Largest Contentful Paint (LCP), the time until the biggest element in the viewport is drawn. A slow hero image gives a slow LCP.
A second problem is layout shift. If the browser doesn't know an image's size, it gives the image no space. When the file arrives, the text below jumps down. This jump is measured as Cumulative Layout Shift (CLS).
You can fix both problems with a few attributes and some careful choices about the file.
Reserve space with width and height
Set width and height on the img element. Browsers use these two numbers to work out the aspect ratio before the file loads, and they reserve a box of the right shape.
<img src="/images/hero-loaf.jpg"
alt="A basket of freshly baked loaves"
width="800" height="600"
fetchpriority="high">The numbers are the image's intrinsic size in pixels. They don't fix the displayed size. The CSS from the layout chapter still controls that:
.hero img {
max-width: 100%;
height: auto;
}With height: auto, the browser keeps the 4:3 ratio from the attributes and scales the image to fit its column.
Load the hero first
Images below the fold can wait, but the hero cannot. Two attributes control this:
fetchpriority="high"tells the browser to download this image ahead of other images.loading="lazy"delays loading until the image is near the viewport. Do not use it on the hero. A lazy hero starts loading late, which hurts LCP.
Use loading="lazy" on images further down the page, such as the cards in a content section.
<img src="/images/sourdough.jpg"
alt="A sliced sourdough loaf"
width="400" height="300"
loading="lazy">Serve the right file for each screen
A phone with a 400-pixel-wide screen doesn't need an 1600-pixel image. The srcset and sizes attributes let the browser pick from several versions.
<img src="/images/hero-800.jpg"
srcset="/images/hero-400.jpg 400w,
/images/hero-800.jpg 800w,
/images/hero-1600.jpg 1600w"
sizes="(min-width: 48rem) 50vw, 100vw"
alt="A basket of freshly baked loaves"
width="800" height="600"
fetchpriority="high">Each w value is the width of that file in pixels. The sizes attribute says how wide the image will be shown. Here it fills the full viewport on narrow screens and half of it from 48rem, matching the two-column hero. The browser combines this with the screen's pixel density and chooses the smallest file that looks sharp.
The src attribute stays as a fallback for browsers that don't understand srcset.
Choose a modern format
JPEG works everywhere, but newer formats such as WebP and AVIF usually give smaller files at similar quality. The picture element lets you offer them with a fallback.
<picture>
<source type="image/avif" srcset="/images/hero.avif">
<source type="image/webp" srcset="/images/hero.webp">
<img src="/images/hero.jpg"
alt="A basket of freshly baked loaves"
width="800" height="600"
fetchpriority="high">
</picture>The browser uses the first source whose type it supports. The img element is required. It carries the alt text, the size attributes, and the fallback file.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| No `width` and `height` | Content jumps when the image loads | Add both attributes |
| `loading="lazy"` on the hero | Slow LCP | Remove it, add `fetchpriority="high"` |
| One huge file for every device | Wasted data on phones | Use `srcset` and `sizes` |
| Hero set as a CSS `background-image` | The browser finds it late, and there is no `alt` text | Use an `img` element when the image carries meaning |
| Missing `alt` | Screen reader users get no description | Describe the image, or use `alt=""` if it is purely decorative |
Check it in the browser
Open the developer tools and go to the Network tab. Reload the page and find the hero image. Check three things: it starts early, its file size is reasonable, and the file matches your screen width. Then open the Performance panel to see the LCP marker and confirm it lands on the hero.
Exercise
A hero image has loading="lazy", no width or height, and a single 2400-pixel-wide JPEG. Name three problems this causes and the change that fixes each.
Show answer
(1) Lazy loading delays the download of the most important image, which slows LCP. Remove loading="lazy" and add fetchpriority="high". (2) Without width and height, the browser reserves no space, so the content below shifts when the image loads. Add both attributes with the image's intrinsic size. (3) A single 2400-pixel file wastes data on small screens. Provide several sizes with srcset and sizes, and consider WebP or AVIF through picture.