Performance: smooth on a real phone.
A scroll site is judged in the first second and on every frame after it. Measure what visitors feel, set a budget, and keep the work off the screen when nobody is looking.
What you will learn
By the end of this chapter- 01Explain LCP, INP and CLS and the good threshold for each.
- 02Set and follow a budget for images, video, fonts and scripts.
- 03Keep scrolling smooth: transform and opacity, batched reads, nothing off screen.
- 04Measure with Lighthouse, the Performance panel and a real phone, and fix the biggest item first.
01 · What to measure
Core Web VitalsLargest Contentful Paint
How long until the biggest thing on screen, usually the hero image or heading, has appeared. Good is 2.5 seconds or less.
Interaction to Next Paint
How quickly the page responds to a tap, click or key. Good is 200 milliseconds or less. Long scroll handlers and heavy loops make it worse.
Cumulative Layout Shift
How much the layout jumps while it loads. Good is 0.1 or less. Give every image and video a width and height, and reserve space for fonts and embeds.
02 · Performance and access
Smooth scrollingAnimate transform and opacity
These can be handled without recalculating layout. Animating width, height, top or left makes the browser redo layout on every frame.
Read, then write
Read all measurements first, then apply all styles. Mixing them in a loop forces repeated layout work.
Use will-change sparingly
Apply it to the few elements that move constantly, such as parallax layers. On every element it costs memory.
Do nothing off screen
Pause loops and skip updates for sections that are not visible. The example stops the parallax once the hero has left the screen.
Respect reduced motion
Check prefers-reduced-motion in CSS and in JavaScript. Provide a static layout that reads well on its own.
Keep the keyboard working
Tab order, visible focus and anchor links must behave the same as without effects. Pinned sections should not trap focus.
03 · Only work on screen
IntersectionObserverStop what nobody can see
The loop below draws only while its canvas is visible. Scroll it away and the frame counter stops: no battery spent on a scene off screen.
Browsers already pause requestAnimationFrame in a hidden tab. Off-screen sections on a visible tab are your job.
<p class="lead">The animation below only runs while you can see it.</p>
<figure class="stage"><canvas width="600" height="300" aria-hidden="true"></canvas></figure>
<p class="lead tall">Keep scrolling until it leaves the screen, and watch the counter stop.</p>
<p class="meter">Frames drawn: <b>0</b> · <span>paused</span></p>04 · A budget
Images, video, fonts, scripts| Asset | Rule | Why |
|---|---|---|
| Hero image | AVIF or WebP, sized with srcset, fetchpriority="high" | It is usually the LCP element |
| Other images | loading="lazy", width and height set | Nothing loads before it is needed, nothing jumps |
| Video | Muted, playsinline, a poster, under a few MB, preload="metadata" | Autoplay is only allowed muted; the poster covers the wait |
| Fonts | Two families at most, woff2, font-display: swap | Text appears at once instead of after the download |
| Scripts | Libraries only on pages that use them, loaded with defer or as modules | The page can respond while scripts load |
Images and video in HTML
<!-- the hero image: sized, modern formats, loaded first -->
<picture>
<source srcset="hero-1600.avif 1600w, hero-800.avif 800w" type="image/avif" sizes="100vw">
<img src="hero-1600.jpg" srcset="hero-1600.jpg 1600w, hero-800.jpg 800w" sizes="100vw"
width="1600" height="900" alt="…" fetchpriority="high">
</picture>
<!-- images further down: loaded only when they come near the screen -->
<img src="studio.jpg" width="1200" height="800" alt="…" loading="lazy" decoding="async">
<!-- background video: silent, inline on phones, a poster while it loads -->
<video src="loop.mp4" poster="loop.jpg" autoplay muted loop playsinline preload="metadata"></video>One variable font, preloaded
<link rel="preload" href="/fonts/inter-var.woff2" as="font" type="font/woff2" crossorigin>
<style>
@font-face {
font-family: Inter;
src: url(/fonts/inter-var.woff2) format('woff2');
font-weight: 100 900;
font-display: swap; /* text shows at once in a fallback font */
}
</style>05 · Measure
Lab and fieldLighthouse
In Chrome DevTools, open the Lighthouse tab and run a mobile report. Fix the largest item first and run it again.
The Performance panel
Record while you scroll, with the CPU slowed down four times. Long yellow blocks are JavaScript on the main thread; purple ones are layout. Both cause dropped frames.
A real phone
A mid-range Android phone on mobile data tells you more than any score. Scroll the whole page twice and note every stutter.
Ask for evidence
Run Lighthouse in mobile mode and give me LCP, INP or TBT, and CLS. List the three biggest opportunities with the file and line each one comes from. Do not change anything yet.Practice · Work with AI
The same task in your toolAsk the AI to measure before it optimises. Optimising without numbers usually moves work around without removing any.
Open a terminal in the project folder and run claude. Press Shift+Tab until plan mode is on, paste the prompt, read the plan, then approve it. Project rules go in CLAUDE.md.
Baseline
Run Lighthouse in mobile mode on the site for [subject] and give me LCP, CLS and Total Blocking Time, plus the three biggest opportunities with the file and line each one comes from. Do not change anything yet.Fix one thing
Read CLAUDE.md. Fix only the first opportunity from the baseline. Then run Lighthouse again with the same settings and show me the before and after numbers side by side.Open a terminal in the project folder and run gemini; the first time, it asks whether you trust the folder, so say yes for your project. Paste the prompt. Gemini CLI asks before it edits a file or runs a command: read each request before you accept it. Project rules go in GEMINI.md.
Baseline
Run Lighthouse in mobile mode on the site for [subject] and give me LCP, CLS and Total Blocking Time, plus the three biggest opportunities with the file and line each one comes from. Do not change anything yet.Fix one thing
Read GEMINI.md. Fix only the first opportunity from the baseline. Then run Lighthouse again with the same settings and show me the before and after numbers side by side.Open a terminal in the project folder and run codex, then paste the prompt and approve each change. In the ChatGPT app without Codex, attach the files, paste the prompt and copy the changes back yourself. Project rules go in AGENTS.md.
Baseline
Run Lighthouse in mobile mode on the site for [subject] and give me LCP, CLS and Total Blocking Time, plus the three biggest opportunities with the file and line each one comes from. Do not change anything yet.Fix one thing
Read AGENTS.md. Fix only the first opportunity from the baseline. Then run Lighthouse again with the same settings and show me the before and after numbers side by side.Practice · Exercises
Try first, then compareFind the LCP element
In the Lighthouse report or the Performance panel, find which element is your Largest Contentful Paint.
Hint: it is usually the hero image or the hero heading.
Show the solution
If it is an image: serve AVIF or WebP at the right size, add width and height, and fetchpriority="high". If it is text: preload the font it uses and keep font-display: swap.
Stop the layout shift
Your CLS is 0.25. Find what moves while the page loads.
Hint: images without dimensions and fonts that swap are the usual causes.
Show the solution
Give every image and video width and height (or an aspect-ratio), reserve space for embeds, and use a fallback font with similar metrics. Measure again: good is 0.1 or less.
Pause off screen
Make one looping effect in your site stop when it is not visible.
Hint: the preview in section 03 does exactly this.
Show the solution
Watch the element with an IntersectionObserver; start the loop when it enters and cancel the frame when it leaves.
new IntersectionObserver(([e]) => {
if (e.isIntersecting && !raf) raf = requestAnimationFrame(loop);
if (!e.isIntersecting && raf) { cancelAnimationFrame(raf); raf = 0; }
}).observe(el);Practice · Course project
One site, built across the courseMake the site for [subject] fast on a mid-range phone, with numbers to prove it.
- Run “Baseline” and save the numbers in
notes.md. - Go through the budget table: images, video, fonts, scripts.
- Run “Fix one thing” three times, for the three biggest opportunities.
- Pause every off-screen loop, then record the final numbers.
Practice · Test it
Check the result- Lighthouse mobile: LCP 2.5 s or less and CLS 0.1 or less.
- Every image has width and height; those below the first screen load lazily.
- The Performance panel shows no long tasks while scrolling, with the CPU slowed four times.
- On a real phone, the whole page scrolls without stutter.
Practice · Assessment
Acceptance criteria0 of 4 criteria met