Sub topic 12
Hero Images Without Slowing the Page
This chapter assumes you can write basic HTML and CSS, and that you know how to open your browser's developer tools. It builds on the hero section from earlier chapters and covers one narrow question: how to include a large image in the hero without making the page feel slow or jumpy.
Why the hero image matters for speed
The hero usually holds the largest visible element on the page, often an image. Browsers measure this with a metric called Largest Contentful Paint (LCP). LCP is the time from the start of navigation until the biggest element in the visible area has been drawn. A slow hero image gives a slow LCP, and visitors see a blank or half-finished top of the page.
A second metric is Cumulative Layout Shift (CLS). CLS measures how much visible content moves after it first appears. A hero image that loads late and pushes the text down is a common cause of layout shift.
Three problems account for most hero image trouble:
- The file is larger than it needs to be.
- The browser discovers the image late, or loads it with low priority.
- The browser does not know the image's size, so it cannot reserve space for it.
The sections below address each problem in turn.
Reserve space with width and height
Start with the hero from the previous chapter.
<section class="hero" aria-labelledby="hero-title">
<div class="hero-text">
<h1 id="hero-title">Fresh bread, baked every morning</h1>
<p>Order ahead and pick up warm loaves on your way to work.</p>
<a class="button" href="/menu">See the menu</a>
</div>
<img src="/images/hero-loaf.jpg" alt="A basket of freshly baked loaves" width="800" height="600" fetchpriority="high">
</section>The width and height attributes on the img element are the intrinsic size of the file in pixels. Modern browsers use them to compute an aspect ratio (here 4:3) before any image data arrives. The browser then holds a box of the correct proportions, and the text below does not jump when the image appears.
This only works well when your CSS lets the image scale. The earlier layout chapter used this rule:
.hero img {
max-width: 100%;
height: auto;
}With height: auto, the browser keeps the ratio from the attributes while max-width: 100% shrinks the image to fit its container. If you leave out height: auto and set a fixed CSS height, the image can be stretched out of proportion.
A common mistake is to set width and height to the displayed size rather than the file's natural size, or to use values with a different ratio from the file. The ratio is what matters. If the attribute ratio is 4:3 and the file is 16:9, the reserved box will be wrong and the layout will still shift when the file loads.
Load the hero early, not lazily
The loading="lazy" attribute tells the browser to delay an image until it is near the viewport. That saves bandwidth for images far down the page. It hurts a hero, because the browser waits for layout information before it even starts the download.
The rule is simple:
- Images below the fold: use
loading="lazy". - The hero image: do not use
loading="lazy".
To make the browser fetch the hero sooner, use fetchpriority="high", as the example above does. This is a hint, not a command. It raises the image's priority relative to other images and resources of similar kind. Use it on one image per page, normally the one that will be the LCP element. If you mark many things as high priority, the hint stops meaning anything.
Preloading when the image is hard to discover
Browsers find img elements while parsing the HTML, so a normal img tag is discovered early. Problems appear when the hero is a CSS background image or is inserted by JavaScript. In those cases the browser cannot see the file until the CSS or script has loaded and run.
If you must use a background image, you can add a preload hint in the head:
<link rel="preload" as="image" href="/images/hero-loaf.jpg" fetchpriority="high">For most pages the better fix is to use a real img element. It is discovered early, it can carry alt text, and it can use srcset (covered next). Reserve a CSS background for decoration that has no meaning.
Send the right size to each screen
A phone with a 400-pixel-wide viewport does not need a 2400-pixel-wide image. The srcset and sizes attributes let you offer several files and let the browser choose.
<img
src="/images/hero-loaf-800.jpg"
srcset="/images/hero-loaf-480.jpg 480w,
/images/hero-loaf-800.jpg 800w,
/images/hero-loaf-1200.jpg 1200w,
/images/hero-loaf-1600.jpg 1600w"
sizes="(min-width: 48rem) 50vw, 100vw"
alt="A basket of freshly baked loaves"
width="800"
height="600"
fetchpriority="high">Each entry in srcset is a URL followed by a width descriptor such as 800w, which is the file's actual width in pixels. The sizes attribute tells the browser how wide the image will be displayed, before the CSS has loaded. Here it says: from 48rem upward, the image takes half the viewport width, and below that it takes the full width. This matches the two-column hero from the layout chapter, which switches to two columns at min-width: 48rem.
The browser combines three things to choose a file: the display width from sizes, the screen's pixel density, and its own judgment about network conditions. A 1000-pixel-wide viewport showing a half-width image needs about 500 CSS pixels. On a screen with twice the pixel density, that is 1000 device pixels, so the browser would likely pick the 1200w file.
Two details cause trouble:
- If
sizesis missing, the browser assumes the image is100vwwide. It then downloads files that are larger than needed in a two-column layout. - The
srcattribute is the fallback for browsers that ignoresrcset. Point it at a mid-sized file.
The width and height attributes should still describe the ratio. All files in one srcset must share the same aspect ratio. If you need a different crop on small screens, use the picture element with media conditions instead (a separate technique called art direction).
Choose a modern format and a sensible quality
File format affects size a great deal. The table compares the common choices for photographs.
| Format | Typical use | Notes |
|---|---|---|
| JPEG | Photographs, universal support | Larger files than newer formats at the same quality |
| WebP | Photographs and graphics | Smaller than JPEG, supported by all current major browsers |
| AVIF | Photographs | Often the smallest, slower to encode, wide but not universal support in older browsers |
| PNG | Sharp graphics, transparency | Very large for photographs, avoid for hero photos |
| SVG | Logos, illustrations | Scales without loss, not suitable for photographs |
To offer a modern format with a safe fallback, wrap the image in a picture element:
<picture>
<source type="image/avif" srcset="/images/hero-loaf-800.avif 800w, /images/hero-loaf-1600.avif 1600w" sizes="(min-width: 48rem) 50vw, 100vw">
<source type="image/webp" srcset="/images/hero-loaf-800.webp 800w, /images/hero-loaf-1600.webp 1600w" sizes="(min-width: 48rem) 50vw, 100vw">
<img
src="/images/hero-loaf-800.jpg"
srcset="/images/hero-loaf-800.jpg 800w, /images/hero-loaf-1600.jpg 1600w"
sizes="(min-width: 48rem) 50vw, 100vw"
alt="A basket of freshly baked loaves"
width="800"
height="600"
fetchpriority="high">
</picture>The browser reads the source elements in order and uses the first one whose type it supports. The img at the end is both the fallback and the element that actually renders. All attributes that affect loading, such as alt, width, height, and fetchpriority, belong on that img.
Export at a quality level that looks fine at the displayed size. For photographs, a JPEG quality around 70 to 80 is often hard to tell apart from 100 while being much smaller. Check the result in the browser rather than trusting a number.
Keep text readable and meaningful
A hero image often sits behind or beside the headline. Two points apply:
- Write
alttext that describes what the image shows or what job it does. If the image is purely decorative, use an emptyalt=""so screen readers skip it. - If text sits on top of the image, check the contrast between text and the image area behind it, including on small screens where the crop may change.
Never place the headline inside the image file. Text in an image does not scale, cannot be searched or translated, and is invisible to screen readers.
Check the result in developer tools
You can verify all of this without extra tools.
- Open the page and open the developer tools, then the Network panel.
- Reload with the cache disabled. Find the hero image and check its size and the time it started downloading. It should start early and should not be much larger than the area it fills.
- Open the Performance panel, record a page load, and look for the LCP marker. It shows which element was the LCP element.
- Use device emulation to try a narrow viewport. Reload and confirm that a smaller file was requested.
- Throttle the network to a slow preset and watch whether the text moves when the image arrives.
Common errors at a glance
| Symptom | Likely cause | Fix |
|---|---|---|
| Text jumps down when the image appears | Missing or wrong `width` and `height` | Add attributes that match the file's ratio, keep `height: auto` in CSS |
| Hero appears late on a fast connection | `loading="lazy"` on the hero, or the image is a CSS background | Remove lazy loading, use an `img`, add `fetchpriority="high"` |
| Phone downloads a huge file | No `srcset`, or `sizes` missing | Add several widths and a correct `sizes` value |
| Image looks stretched | Fixed CSS height without `height: auto` or `object-fit` | Use `height: auto`, or `object-fit: cover` in a fixed-height box |
| Everything is marked high priority | `fetchpriority="high"` used widely | Keep it to the single LCP image |
Exercise
A page has a two-column hero from 48rem upward, where the image takes half the viewport width, and one column below that. The hero img has loading="lazy", no width or height, and no sizes attribute, though it has a srcset with 480w, 800w, and 1600w files. List the four changes you would make and say what problem each one fixes.
Show answer
1) Remove loading="lazy" and add fetchpriority="high", so the browser fetches the hero early and LCP improves. 2) Add width and height that match the file's aspect ratio, so the browser reserves space and the layout does not shift (CLS). 3) Add sizes="(min-width: 48rem) 50vw, 100vw", so the browser knows the real display width instead of assuming 100vw and does not download an oversized file. 4) Make sure the CSS has max-width: 100% and height: auto on the image, so it scales with its container and keeps its ratio.
Exercise
Why is a real img element usually a better choice for a hero photo than a CSS background-image?
Show answer
The browser finds an img while parsing the HTML, so it can start the download early. A CSS background is found only after the stylesheet has loaded and been applied. An img also supports alt text, srcset, sizes, width, and height, which a background image does not.