HTML performance: defer, preload, lazy loading and Core Web Vitals
Make pages load fast with defer and async, resource hints, lighter images and fonts, and measure with Lighthouse.
In this lesson you will
- Load scripts without blocking
- Use resource hints carefully
- Slim down images and fonts
- Measure with Lighthouse and the Network tab
About 14 minutes
Speed is part of the design
Imagine Priya’s customer standing at a bus stop, trying to open the bakery page on a patchy mobile connection. If it takes eight seconds to show anything, the bus arrives and the order is lost. Studies keep finding that a big share of visitors leave pages that take more than three seconds. Speed is not a technical luxury. It is part of the experience, and search engines use it as a ranking signal as well.
HTML itself has several tools that make pages faster, and most of them are single attributes. This lesson covers the ones that matter most.
How a browser loads a page
The browser downloads the HTML, reads it from top to bottom and discovers the other files it needs: CSS, JavaScript, images, fonts. It will not draw the page until it has the CSS, and a plain script in the middle of the page makes it stop and wait. So the goal is simple: get the important things first, and delay everything else.
Scripts: defer and async
<!-- Blocks the page while it downloads and runs. Avoid. -->
<script src="app.js"></script>
<!-- Downloads in the background, runs after the page is parsed. Best default. -->
<script src="app.js" defer></script>
<!-- Downloads in the background, runs as soon as ready. Good for analytics. -->
<script src="stats.js" async></script>
deferkeeps the order of scripts and waits for the page to finish. It is the right choice for your own site code.asyncruns the script the moment it arrives, in any order. Use it for independent scripts like analytics.- With neither, the page stops. Only use that for a tiny script that must run first.
Press Run and read the output order. Then change the script to use defer, and think about why it still works at the end of the body or in the head.
<p id="t">Waiting...</p>
<script>
document.getElementById('t').textContent = 'The script ran after this paragraph was available.';
</script>
A script placed after the element it uses can run immediately. In the head, add defer so it waits for the page.
<p id="t">Waiting...</p>
<script defer>
window.addEventListener('DOMContentLoaded', function(){
document.getElementById('t').textContent = 'The script ran after the page was ready.';
});
</script>
CSS: keep it light
Stylesheets in the head are render-blocking: the page stays blank until they arrive. That is right for the CSS the first screen needs, but wasteful for the rest. Keep stylesheets small, combine files where possible, and load print or rarely used styles with a media attribute so they never block the screen.
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="print.css" media="print">
Hints: preload, preconnect and prefetch
You can warn the browser about things it will need soon, so it starts early.
<!-- Connect early to a font host -->
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<!-- Fetch a vital file with high priority -->
<link rel="preload" href="/fonts/Sora.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" as="image" href="/images/hero.webp">
<!-- Quietly fetch the likely next page -->
<link rel="prefetch" href="/menu/">
Preload only a few critical files. Preloading everything tells the browser everything is urgent, which means nothing is, and can actually make the page slower.
Images are usually the heaviest part
- Resize before uploading. A 4000 px photo displayed at 800 px wastes most of its bytes.
- Use modern formats, WebP or AVIF, with a JPG fallback.
- Give images width and height so nothing jumps.
- Lazy-load below-the-fold images with
loading="lazy"and give the top imagefetchpriority="high". - Use
<video preload="none">or a poster so videos wait until requested.
Fonts without the flash
Web fonts are a common cause of flashes of invisible text. Reduce the problem with a few habits: use fewer font families and weights, serve WOFF2 files, preload the main font, and add font-display: swap in your CSS so readable fallback text appears straight away.
@font-face {
font-family: "Sora";
src: url("/fonts/Sora.woff2") format("woff2");
font-display: swap;
}
Measuring, not guessing
Never optimise blindly. Measure first, change one thing, measure again. Free tools make this easy.
- Lighthouse in Chrome’s developer tools scores performance and lists the biggest fixes.
- PageSpeed Insights shows both lab tests and real-user data for public pages.
- The Network tab shows every file, its size and how long it took.
- The three Core Web Vitals to watch: LCP (how fast the main content appears), INP (how quickly the page responds to clicks) and CLS (how much the layout jumps).
Open the Network tab, sort by size, and look at the top three files. Fixing those usually beats a dozen tiny tweaks.
Improve this head: add defer to the script and a media attribute so the print stylesheet does not block screens.
<head>
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="print.css">
<script src="app.js"></script>
</head>
Write media=”print” on the print link, and add defer to the script tag.
<head>
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="print.css" media="print">
<script src="app.js" defer></script>
</head>
A performance routine for every new page
Performance work feels overwhelming until you turn it into a routine. Here is a short one to run before publishing anything. It takes about ten minutes, and it prevents the slow creep that makes sites heavy over time.
- Open the page, press F12 and choose the Network tab, then reload with the cache disabled.
- Sort by size and look at the three largest files. Can any be smaller, lazy-loaded or removed?
- Look at the number of requests. Combine or delete files you do not need.
- Run Lighthouse in mobile mode and read the top three opportunities.
- Fix one thing, reload and measure again to confirm it helped.
Over months, this habit matters more than any single trick. Sites become slow because of a hundred small additions: another font, another widget, another tracking script. Each one seemed harmless. A routine catches them early.
Test on a real, modest phone
Your own computer is fast, and your Wi-Fi is fast. Many visitors have neither. Chrome’s developer tools can throttle the connection to a slow 4G or 3G speed and slow the processor to imitate a cheaper phone. Try it once and the importance of this lesson becomes very personal.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Which attribute loads a script without blocking the page and keeps order?
deferwaitblocklater
defer downloads in the background and runs after parsing, in order.
What does media="print" on a stylesheet do?
- Prints the page
- Stops it from blocking screen rendering
- Makes it bigger
- Removes it
Browsers treat it as non-blocking for screens.
What is a risk of preloading too many files?
- Nothing
- Everything seems urgent and the page can slow down
- It breaks HTML
- It disables caching
Preload only the few truly critical resources.
Which Core Web Vital measures layout jumping?
- LCP
- INP
- CLS
- TTL
Cumulative Layout Shift measures unexpected movement.
What is the best first step when optimising?
- Guess
- Measure and fix the biggest file first
- Delete the CSS
- Add more scripts
Measure first, then address the largest gains.
Summary
- Load important things first and delay the rest.
- Use defer for your scripts and media for rarely used CSS.
- Images and fonts are the heaviest parts.
- Measure before and after every change.
Questions
What is a good page weight?
Aim for under about 1 to 2 MB for most pages. Lighter is better on mobile.
Should I inline all CSS?
Only a small amount of critical CSS can help the first paint. Most sites do best with a single small stylesheet.
Does speed affect SEO?
Yes, it is a ranking signal, and it also affects how many visitors stay.
