Skip to content
Tutorials Center
My learning

Topics

Lesson 112 of 122 ยท Accessibility and Best Practices

Fast-Loading HTML

Make pages load fast with lean HTML, image dimensions, lazy loading, non-blocking scripts, a smaller DOM, compression and caching, measured by Core Web Vitals.

A fast page feels better, keeps visitors from leaving and works for people on slow phones and patchy mobile connections. Speed is also an accessibility issue: a page that takes twenty seconds on a budget phone is, for its owner, a page that does not work.

Much of web performance happens on the server and in CSS and JavaScript, but your HTML decides a surprising amount: what loads first, what waits, what is downloaded at all and whether the layout jumps around while it loads. This lesson covers the HTML habits that matter most.

How a page loads

When you open a page, the browser roughly does this:

  1. Downloads the HTML and starts parsing it as it arrives, top to bottom. It does not wait for the whole file.
  2. Discovers other resources in the markup (stylesheets, scripts, images, fonts) and requests them.
  3. Builds the DOM from the HTML and the CSSOM from the CSS.
  4. Lays out and paints the page, then keeps updating as more content arrives.

Two consequences follow. First, anything early in the HTML is discovered early, so order matters. Second, some resources make the browser stop and wait, and those are the ones to watch.

Measuring speed: Core Web Vitals

Google's Core Web Vitals are three widely used metrics, measured on real visits:

Metric What it measures "Good" target
LCP, Largest Contentful Paint When the biggest visible element (often a hero image or heading) appears 2.5 seconds or less
INP, Interaction to Next Paint How quickly the page responds to clicks, taps and key presses 200 milliseconds or less
CLS, Cumulative Layout Shift How much visible content jumps around unexpectedly 0.1 or less

You can measure them with Lighthouse in Chrome DevTools, PageSpeed Insights online, and the Performance and Network panels in any browser's DevTools. Test with network and CPU throttling turned on; your laptop on office Wi-Fi is not a typical visitor.

Keep the document light

The HTML file is the first thing downloaded, and nothing else can start until parts of it arrive. Keep it lean:

  • Avoid giant inline blobs. Large inline SVGs repeated on every page, base64 data: images and big inline JSON all add weight to the HTML. Unlike separate files, they cannot be cached on their own and are downloaded again with every page.
  • Put important things first. <meta charset>, the title, critical CSS and the preload hints for your main image should appear early in <head>.
  • Minify in production if your build tools make it easy. Removing whitespace and comments gives modest savings once the file is compressed, so it matters less than the other tips here.

A common rule of thumb is to fit the critical start of a page (the <head> and the first visible content) within roughly the first 14 KB of compressed HTML, because that is about how much a server can send in the first round trip of a new connection. Treat it as a guideline, not a hard rule.

Images: the heaviest part of most pages

Images are usually the largest bytes on a page, and they are easy to get right in HTML.

Always set width and height. The browser uses them to calculate the image's aspect ratio before the file arrives and reserves the right amount of space. Without them, the image starts at zero height and everything below jumps down when it loads. That jump is exactly what CLS measures.

HTML
<img src="harbor.jpg" alt="Boats in a harbor at sunrise" width="1200" height="800">
CSS
/* Responsive images still keep their reserved space */
img { max-width: 100%; height: auto; }

The demo below simulates an image that arrives one and a half seconds late. On the left, no space is reserved. On the right, the space is reserved in advance, just as width and height attributes do for a real <img>.

Layout shift vs reserved space

More image habits:

  • Lazy-load images below the fold with loading="lazy". The browser then waits to fetch them until the user scrolls near them. The same attribute works on <iframe>.
  • Never lazy-load the main image at the top of the page. It is usually the LCP element, and delaying it makes the page feel slower. Instead, you can give it fetchpriority="high".
  • Serve the right size and format. Use srcset and sizes so phones do not download desktop-sized files, and modern formats like WebP and AVIF. See Responsive Images.
  • Use decoding="async" as a hint that decoding a large image should not hold up other content.
HTML
<!-- Hero image at the top: load it early and with high priority -->
<img src="hero.avif" alt="A hiker on a ridge at dawn"
     width="1600" height="900" fetchpriority="high">

<!-- Gallery further down: load when needed -->
<img src="trail-03.avif" alt="A wooden bridge over a stream"
     width="800" height="600" loading="lazy" decoding="async">

The Images lesson covers these attributes in context.

Render-blocking resources

Some resources pause the browser:

  • Stylesheets in the head are render-blocking. The browser does not paint until it has the CSS, to avoid showing unstyled content. Keep CSS lean, and do not load stylesheets that the page does not use.
  • Classic scripts block parsing. When the parser meets <script src="app.js"> without extra attributes, it stops building the DOM, downloads the script, runs it and only then continues.

The fix for scripts is one attribute:

  • defer downloads the script in parallel and runs it after the document has been parsed, in order. This is the right choice for most scripts.
  • async downloads in parallel and runs as soon as it arrives, in any order. Good for independent scripts like analytics.
  • type="module" scripts are deferred automatically.
HTML
<head>
  <link rel="stylesheet" href="/css/site.css">
  <script src="/js/app.js" defer></script>
</head>

You can watch the difference in parsing order. The classic script runs the moment the parser reaches it, before the heading below it exists. The module script is deferred until the document has been parsed.

Classic vs deferred script timing

Resource hints such as preload and preconnect let you start important downloads even earlier. They have their own lesson: Loading Resources Efficiently. For script placement in general, see Adding JavaScript.

Keep the DOM small

Every element costs memory, and a large DOM makes style calculation, layout and JavaScript queries slower. That hurts INP, because each interaction may trigger work across the whole tree. Auditing tools such as Lighthouse warn when a page has well over a thousand elements.

The usual cause is unnecessary wrappers: a <div> inside a <div> inside a <div>, each added for a single style rule. Modern CSS (Flexbox, Grid, gap) rarely needs that.

Counting elements

Same content, a fraction of the elements, and the lean version is more meaningful too. For very long pages, also consider:

  • Pagination or "load more" instead of rendering thousands of items at once.
  • The CSS content-visibility: auto property, which lets the browser skip rendering work for sections that are off-screen.
  • <details> or a <template> for content that is not needed right away (see Templates and Web Components).

Compression and caching

These two are set up on the server, not in HTML, but every web developer should know them.

Compression. Text files (HTML, CSS, JavaScript, SVG, JSON) compress extremely well. Servers compress them with gzip, Brotli or, in newer setups, Zstandard, and tell the browser with a response header. A 100 KB HTML file often shrinks to 15โ€“25 KB.

HTTP
Content-Encoding: br

Images such as JPEG, WebP and AVIF are already compressed; compressing them again gains nothing.

Caching. A repeat visitor should not re-download files that have not changed. The Cache-Control header tells the browser how long it may reuse a file:

HTTP
Cache-Control: public, max-age=31536000, immutable

That says "keep this for a year and do not even check for updates". It is safe only for files whose name changes when their content changes, such as app.3f9a2c.js, which build tools generate. HTML itself usually gets a short lifetime or no-cache (reuse it, but check with the server first), so visitors always see the latest page that points at the latest file names.

A quick performance checklist for your HTML

  • width and height on every image, video and iframe.
  • loading="lazy" below the fold, never on the main image.
  • defer (or type="module") on scripts in the head.
  • No unused stylesheets or scripts.
  • Lean markup without wrapper soup.
  • Compression and long-lived caching for versioned assets, checked in the Network panel.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should every <img> have width and height attributes?

    The attributes give the browser the aspect ratio in advance. Combined with height: auto in CSS, the image stays responsive and the layout stays stable.
  2. 2. Which image should NOT get loading="lazy"?

    The hero image is usually the Largest Contentful Paint element. Lazy-loading it delays the most important content.
  3. 3. What does the defer attribute do on an external script?

    Deferred scripts never block parsing and keep their order. Module scripts behave this way by default.
  4. 4. When is Cache-Control with max-age=31536000, immutable safe to use?

    Fingerprinted file names like app.3f9a2c.js can be cached forever, because a new version gets a new name. HTML needs a short lifetime or revalidation.

Summary

  • Browsers parse HTML as it streams in; put critical resources early and keep the document lean.
  • Core Web Vitals measure loading (LCP), responsiveness (INP) and visual stability (CLS).
  • Set width and height on images, lazy-load only below the fold, and prioritize the main image.
  • Stylesheets block rendering and classic scripts block parsing; use defer, async or type="module".
  • A smaller DOM makes styling, layout and interactions faster.
  • Compression (gzip, Brotli, Zstandard) and long caching of fingerprinted files make every visit cheaper.

You have finished the Accessibility and Best Practices section. Time to put everything together: Project: Personal Profile Page.