HTML Performance
Why Page Speed Matters
A fast page keeps visitors happy. If a page takes too long to load, many people leave before they see anything. Search engines also prefer fast pages.
You do not need advanced tools to make a page faster. A few HTML attributes can make a big difference:
-
loading="lazy"waits to load images until they are needed -
deferandasyncstop scripts from blocking the page -
rel="preload"loads important files early
Lazy Loading
By default, the browser downloads every image on a page right away, even images far down that the user may never scroll to.
Lazy loading tells the browser to wait. An image is only downloaded when the user scrolls close to it.
Lazy loading on <img>:
Example
<img src="photo.jpg" alt="Mountain lake" width="600" height="400"
loading="lazy">
The loading attribute has two values:
-
lazy: wait until the image is near the screen -
eager: load right away (this is the default)
Lazy loading also works on <iframe>, like embedded videos and maps:
Example
<iframe src="https://www.youtube.com/embed/video_id" title="Intro video"
loading="lazy"></iframe>
When not to use it
Do not lazy-load images that are visible as soon as the page opens, like a logo or a large banner at the top. They would show up later than they should, and the page would feel slower.
Rule of thumb: Lazy-load images further down the page. Load images at the top normally.
Tip: Always set width and
height on images. The browser can then save the right amount of space, so the page
does not jump around when the image appears.
Loading Scripts with defer and async
When the browser finds a normal <script> tag, it stops building the page. It downloads the script, runs it, and only then continues. A big script in the
<head> section can leave users looking at a blank screen.
The defer and
async attributes let the browser download the script while it keeps building the page.
defer
Use the defer attribute for most of your own scripts, especially ones that work with the page content or depend on each other.
Example
<script src="app.js" defer></script>
- Downloads in the background
- Runs after the whole page is built
- Several deferred scripts run in order, top to bottom
async
Use the async attribute for scripts that work on their own, like analytics or ads.
Example
<script src="analytics.js" async></script>
- Downloads in the background
- Runs as soon as it is downloaded, even if the page is not finished
- Several async scripts can run in any order
Comparison
| Normal | defer | async | |
|---|---|---|---|
| Blocks the page while downloading | Yes | No | No |
| When it runs | Right away | After the page is built | As soon as it is downloaded |
| Keeps the order of scripts | Yes | Yes | No |
| Good for | Small scripts that must run first | Most of your own scripts | Independent scripts, like analytics |
Preload
Some important files are found late. A font, for example, is found after the browser has downloaded and read the CSS file that uses it.
<link rel="preload"> tells the browser about a file early, so it can start downloading it right away:
Example
<head>
<link rel="preload" href="hero.jpg" as="image">
<link rel="preload" href="myfont.woff2" as="font" type="font/woff2"
crossorigin>
<link rel="stylesheet" href="styles.css">
</head>
The as Attribute
The as attribute tells the browser what kind of file it is. This is required.
| Value | File type |
|---|---|
| image | Images |
| font | Fonts (also add crossorigin) |
| style | CSS files |
| script | JavaScript files |
Note: Preload only the few files the page needs right away, like the main image at the top or the main font! If you preload too much, the files compete with each other, and the page gets slower, not faster.
Tip: The browser warns you in the console if you preload a file and do not use it.
Preconnect
If your page uses files from another website, like Google Fonts, preconnect sets up the connection early:
Example
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
The difference:
<link rel="preload">downloads a file<link rel="preconnect">only opens the connection to a website, so the files from it arrive faster later