Performance and best practices
A one-second delay can cut conversions by 7%, and Google uses page speed in rankings. Most performance wins come from how you write and load your HTML: what you load, in what order, and how much.
How a browser loads a page
- Download the HTML and start parsing it top to bottom.
- Find
<link rel="stylesheet">: download the CSS. Rendering is blocked until it arrives. - Find a plain
<script>: parsing stops until the script downloads and runs. - Build the DOM + CSSOM → layout → paint pixels.
The goal is to get the important content painted as soon as possible.
Core Web Vitals
Google measures three user-centred metrics:
| Metric | Measures | Good |
|---|---|---|
| LCP: Largest Contentful Paint | When the main content (often the hero image) appears | ≤ 2.5 s |
| INP: Interaction to Next Paint | How quickly the page responds to clicks and taps | ≤ 200 ms |
| CLS: Cumulative Layout Shift | How much content jumps around while loading | ≤ 0.1 |
Check them in Chrome DevTools → Lighthouse, or with PageSpeed Insights.
Load scripts without blocking
<!-- ✗ Blocks HTML parsing -->
<script src="app.js"></script>
<!-- ✓ defer: downloads in parallel, runs after HTML is parsed, in order -->
<script src="app.js" defer></script>
<!-- ✓ async: runs as soon as it downloads, in any order — good for independent scripts like analytics -->
<script src="analytics.js" async></script>
<!-- ✓ ES modules are deferred by default -->
<script type="module" src="main.js"></script>Rule of thumb: use defer for your own scripts and async for independent third-party scripts.
Prevent layout shift (CLS)
<!-- ✓ width/height let the browser reserve space before the image loads -->
<img src="hero.webp" alt="…" width="1200" height="600">- Always set
widthandheight(or CSSaspect-ratio) on images, videos and iframes. - Reserve space for ads and embeds.
- Don't insert banners above existing content after the page loads.
Speed up the most important content (LCP)
<head>
<!-- Connect early to critical third-party origins -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://cdn.example.com" crossorigin>
<!-- Preload the hero image or a critical font -->
<link rel="preload" as="image" href="/hero.avif" fetchpriority="high">
</head>
<body>
<!-- Mark the hero image as high priority; never lazy-load it -->
<img src="/hero.avif" alt="…" width="1200" height="600" fetchpriority="high">
<!-- Lazy-load everything below the fold -->
<img src="/gallery-1.webp" alt="…" width="400" height="300" loading="lazy">
</body>Send less
- Compress images: use AVIF/WebP, the right dimensions, and
srcset. Images are usually most of a page's weight. - Minify HTML, CSS and JS (build tools like Vite do this automatically).
- Compress text responses with gzip or Brotli on the server.
- Cache static files with long cache lifetimes and fingerprinted file names (
app.3f9c2a.js). - Audit third-party scripts. Chat widgets, trackers and A/B testing tools are often the slowest thing on a page.
- Use a CDN to serve files from close to the user.
Best-practice checklist for every page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <!-- first, within 1024 bytes -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Unique, descriptive title</title>
<meta name="description" content="…">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="/styles.css">
<script src="/app.js" defer></script>
</head>
<body>
<a class="skip-link" href="#main">Skip to content</a>
<header>…</header>
<main id="main">…</main>
<footer>…</footer>
</body>
</html>- Valid HTML (check with the W3C validator)
- Semantic landmarks, one
<h1>, logical headings - Every image has
alt,widthandheight - Every input has a
<label> - Scripts use
deferorasync - Below-the-fold media uses
loading="lazy" - Lighthouse scores of 90+ for Performance, Accessibility, Best Practices and SEO
🎓 HTML track complete!
You can now write well-structured, accessible, fast, search-friendly HTML. Next up: the CSS track, to make it beautiful.
Practice
Take any page you've built in this track (or a real website you like). Run Lighthouse on it, write down the top three issues, fix them, and run it again to compare scores.
1.Which attribute should most of your own scripts use to avoid blocking HTML parsing?
2.What reduces Cumulative Layout Shift for images?