HTML Performance Basics
Every page is a shopping list. The browser reads your HTML, finds a list of files it needs (styles, scripts, images, fonts), and goes to fetch them. A fast page is one where that list is short, the files are small, the important ones are fetched first, and nothing stops the visitor from seeing the page while the rest arrive.
You do not need to be a performance expert to get most of the benefit. A handful of HTML habits, repeated on every page, remove most of the common slowdowns. This lesson teaches those habits, one by one, with a checker you can try on a real page.
What You Will Learnβ
- The four questions to ask about every file a page loads
- Why stylesheets and plain scripts can hold up the first paint
- How
deferandasyncstop scripts from blocking - Why images need
widthandheight, and when to lazy-load them - How to avoid sending more pixels than a screen can show
- How iframes and embeds add hidden weight
- Which speed jobs belong to the server, not the HTML
The Big Ideaβ
Compare two versions of the same page. They look identical to a visitor, but the browser has a very different afternoon.
- Slow Page
- Fast Page
- Page Checker
<head>
<meta charset="utf-8">
<title>The Bread Bin</title>
<link rel="stylesheet" href="/reset.css">
<link rel="stylesheet" href="/layout.css">
<link rel="stylesheet" href="/theme.css">
<link rel="stylesheet" href="/widgets.css">
<style>@import url("/fonts.css");</style>
<script src="/jquery.js"></script>
<script src="/app.js"></script>
</head>
<body>
<img src="/img/hero.jpg" alt="A golden loaf" loading="lazy">
<img src="/img/rye.jpg" alt="Rye loaves">
<iframe src="https://maps.example.com/embed"></iframe>
</body>
<head>
<meta charset="utf-8">
<title>The Bread Bin</title>
<link rel="stylesheet" href="/site.css">
<script src="/app.js" defer></script>
</head>
<body>
<img src="/img/hero.jpg" alt="A golden loaf"
width="1200" height="600" fetchpriority="high">
<img src="/img/rye.jpg" alt="Rye loaves"
width="600" height="400" loading="lazy">
<iframe src="https://maps.example.com/embed"
width="600" height="400" loading="lazy"
title="Map to the bakery"></iframe>
</body>
Loading the checker...
Load the slow page into the checker, then fix the problems one at a time and watch the list shrink. The fetchpriority attribute is a hint that the main image matters most, and it is supported in current major browsers, but check MDN for the latest support.
Four Questions for Every Fileβ
Before you add anything to a page, ask these four questions. They cover most performance decisions.
- Do I need it? The fastest file is the one you never send. Unused scripts, extra fonts, and old widgets all cost something.
- Is it small? Compress images, choose the right size, and trim what you do not use.
- Is it found early? Important files should be easy for the browser to discover, ideally right in the HTML.
- Does it block? If the visitor has to wait for the file before seeing anything, ask whether it really must come first.
Keep these in your head as you read the rest of the lesson. Every tip below answers one of them.
Stylesheets: Needed Before Paintingβ
A browser will not paint a page until it has the styles that apply to it, because painting unstyled content and then restyling it looks broken. So CSS in the head is render-blocking: it holds up the first paint until it has downloaded and been read.
That is usually the right trade, but it means your CSS is on the critical path. Keep it light:
| Habit | Why it helps | Watch out for |
|---|---|---|
| Send only the CSS the page uses | Less to download and read | Huge frameworks used for three rules |
Avoid @import inside CSS or style blocks | The browser finds the next file only after the first arrives | Chains of files three or four deep |
Use a link element in the head | The browser sees the file straight away | Putting a stylesheet link deep in the body |
| Keep the number of files reasonable | Each file is a request that can delay painting | Many tiny files, or one giant file |
You will read more about what counts as critical in Critical Resources. For now, remember that CSS is on the path to your first paint, so treat it with care.
The number of files matters less than it used to, because modern servers can send several at once. Total size and the chains between files matter more.
Scripts: Do Not Blockβ
A plain script tag with a src is the most common cause of a slow first paint. The browser stops reading the HTML, downloads the script, runs it, and only then carries on.
<!-- Blocks: the browser stops here until the script has run -->
<script src="/app.js"></script>
<!-- Better: download in the background, run after the HTML is read -->
<script src="/app.js" defer></script>
<!-- Also fine for independent scripts: run as soon as it arrives -->
<script src="/analytics.js" async></script>
A simple rule to start with: use defer for your own scripts, and async for independent ones such as analytics. Modern module scripts (type="module") are deferred by default. The next lessons, Script Loading, Defer, and Async, go into the differences in detail. The original script element lesson covers the basics of the tag itself.
Adding defer can break a script that expects to run before the page is ready, or that relies on another script running first. Test after the change. defer scripts keep their order, which helps with dependencies.
Images: The Biggest Filesβ
On most pages, images are the heaviest files, and they cause the most visible problems. Four habits cover most of them.
1. Always give width and heightβ
<img src="/img/rye.jpg" alt="Rye loaves" width="600" height="400">
These two attributes let the browser work out the shape of the image before it arrives, and reserve that space. Without them, text jumps down when the image appears. That jump is what Cumulative Layout Shift (CLS) measures. CSS can still make the image fit its container, for example with max-width: 100%; height: auto;.
2. Lazy-load images below the first screenβ
<img src="/img/gallery-3.jpg" alt="Seeded rolls" width="600" height="400" loading="lazy">
loading="lazy" tells the browser to wait until the image is near the screen. That saves data and leaves bandwidth for what matters. It is covered in detail in the Lazy Loading lesson, and the older image lazy loading lesson.
3. Never lazy-load the main imageβ
The big picture people see straight away should start loading as early as possible. Leave off loading="lazy", and consider fetchpriority="high" for it. Lazy-loading it is one of the most common ways to make a page feel slower.
4. Do not send more pixels than the screen can showβ
A 4000px photo in a 400px slot makes the visitor download a lot of data they cannot see. Try it:
The screen needs about 800px of width. About 96% of the file's pixels are never used on this screen.
File size does not track pixels exactly, because it also depends on the format and the picture, but more pixels almost always means more bytes.
The fix is to offer several sizes and let the browser choose, using srcset and sizes. You can read more in Responsive Images, and the earlier responsive images lesson. Modern formats such as WebP and AVIF are often smaller than JPEG at similar quality, and most current browsers support them, but test on the browsers your visitors use.
Iframes and Embedsβ
A single embedded video, map, or social widget can bring dozens of files of its own, from servers you do not control. You cannot easily make them smaller, but you can control when they load.
<iframe
src="https://maps.example.com/embed"
width="600" height="400"
loading="lazy"
title="Map to the bakery"></iframe>
- Add
loading="lazy"to embeds below the first screen. - Give them
widthandheight, so the page does not jump. - Ask whether you need the embed, or whether a simple link or a still image would do.
- Always give an iframe a
title, for screen reader users.
A Word About Fontsβ
Custom fonts are files too, and a page waiting for a font may show invisible text. Two cautious habits help: use as few font files as you can, and set font-display: swap in your @font-face rule so text shows in a fallback font while the real one loads. Fonts have trade-offs (a brief change of look when they swap in), so test how it feels. We will return to what to load first in Critical Resources.
Some Speed Belongs to the Serverβ
Not everything is in your HTML. These matter just as much, and are usually set up where the site is hosted:
- Compression. Text files (HTML, CSS, JavaScript) can be compressed with gzip or Brotli, often shrinking them a lot.
- Caching. Files that do not change can be stored by the browser, so a returning visitor downloads nothing.
- A CDN. A content delivery network serves files from near the visitor.
- A fast server response. If the HTML itself is slow to arrive, nothing else can start.
If you use a platform like Netlify, Vercel, or GitHub Pages, much of this is handled for you. Check your host's documentation to see what is on by default.
Spot the Slowdownβ
Time to practise. Each snippet has one problem, or one improvement waiting to happen. Pick an answer to see why.
Answered 0 of 6. Correct: 0.
<head>
<script src="/app.js"></script>
</head>
<!-- the big picture at the very top of the page -->
<img src="/img/hero.jpg" alt="A golden loaf"
width="1200" height="600" loading="lazy">
<img src="/img/cat.jpg" alt="A grey cat"><!-- the file is 4000px wide -->
<img src="/img/photo-4000.jpg" alt="Market stall"
width="400" height="300">
<style>
@import url("/fonts.css");
</style>
<!-- far down the page, below the footer links -->
<iframe src="https://video.example.com/embed"
width="560" height="315" title="Our story"></iframe>
Best Practicesβ
- Measure first. Use Lighthouse or DevTools to find the real problem before guessing.
- Keep the head light. A few small stylesheets, scripts with
deferorasync, and nothing else you do not need. - Give every image and iframe
widthandheight. - Lazy-load below the first screen, and never the main image.
- Serve images at the size they are shown, and consider modern formats.
- Remove what you do not use. Old scripts, unused styles, and spare fonts all cost something.
- Test on a slow connection and a modest phone, not only your own fast machine.
Quick Checklistβ
- No plain scripts in the head
- No
@importchains for CSS - Every image and iframe has
widthandheight - The main image is not lazy-loaded
- Images below the first screen use
loading="lazy" - No image is much larger than the space it fills
- Embeds are lazy-loaded, or replaced by a link
- Compression and caching are on at the server
Security and Privacy Notesβ
- Every script is trust. A script you load from another site runs with the full power of your page. Add only scripts you need, from providers you trust. For files that never change, the
integrityattribute lets the browser check that the file has not been altered. - Embeds can track visitors. Maps, videos, and social widgets may set cookies or collect data as soon as they load. Lazy-loading them can reduce this on pages where visitors never scroll that far, but do not rely on it for consent. Check the rules that apply to you.
- Do not cache private pages in shared caches. Pages with personal information need the right cache headers. Your host's documentation explains the options.
- Keep the page working without extras. A page that depends on a third-party script to show its main content is both slow and fragile if that script is blocked or fails.
Global Audience Notesβ
- Many visitors use slow networks and modest phones. A page that is fast for them is fast for everyone.
- Data can be expensive. Large images, autoplaying media, and heavy embeds can cost visitors real money. Lazy loading and right-sized images are kind to people on limited plans.
- Distance adds delay. A CDN is one of the easiest wins for a worldwide audience.
- Fonts for some scripts are large. Chinese, Japanese, and Korean fonts can be very big, so use subsets or system fonts where you can.
- Some third-party services are slow or blocked in some places. A page should still load if one of them does not.
Common Mistakes to Avoidβ
- Plain
scripttags in the head, which block the page. - Lazy-loading the main image.
- Images with no
widthandheight, so the layout jumps. - Huge images in small slots.
@importchains in CSS.- Loading every font weight "just in case".
- Embeds above the fold that nobody needs.
- Adding
deferwithout testing, then breaking a script that needed to run early. - Trusting one test on a fast laptop.
- Adding hints and tricks before fixing the basics.
Practice Timeβ
- Paste a page you built into the checker. How many problems does it find?
- Fix every problem it reports, one at a time.
- Use the image lab to find a file width that wastes less than 25% of its pixels on a 2x screen at 400px.
- Take the Spot the Slowdown quiz. Which one surprised you?
- Open DevTools, then the Network panel, and reload one of your pages. Which file is the biggest?
- Add
widthandheightto every image on a page, then reload with a slow connection. Does the layout still jump? - Add
deferto a script, and check that your page still works. - Bonus: run Lighthouse before and after your changes, and compare the results.
Quick Recapβ
Click a question to check your answer.
1. What four questions should you ask about every file?
Do I need it? Is it small? Is it found early? Does it block?
2. Why is a plain script in the head a problem?
The browser stops reading the HTML to download and run it, which delays everything after it. Use defer or async instead.
3. Why give images width and height?
So the browser can reserve the right space before the image arrives. That stops the layout from jumping, which is what CLS measures.
4. Which images should use loading="lazy"?
Images below the first screen. The main image should not, because lazy loading delays it.
5. Why avoid @import in CSS?
The browser only discovers the imported file after it has fetched and read the first one, which creates a chain of delays. A link element lets it find the file straight away.
6. Name two speed jobs that belong to the server.
Compression (such as gzip or Brotli) and caching. A CDN and a fast server response also matter.
Key Takeawaysβ
- A page is a shopping list of files. Make it short, small, early, and non-blocking.
- CSS in the head blocks painting, so keep it light.
- Use
deferandasyncso scripts do not stop the page. - Always size images and iframes, and lazy-load those below the first screen.
- Never lazy-load the main image.
- Send the right number of pixels, not more than the screen can show.
- Some speed is the server's job: compression, caching, and a CDN.
- Measure, change one thing, and measure again.
Next up: Resource Hints, where you will learn how to tell the browser about files before it finds them.