Improving HTML Performance
Webpage performance is affected by the amount of content and resources a browser must download, process, and display before a page becomes useful to the visitor.
HTML itself is usually only part of a webpage's total size, but the markup determines which images, stylesheets, scripts, fonts, media files, and embedded resources the browser loads. Writing efficient HTML and loading resources thoughtfully can make a noticeable difference in page performance.
Why Performance Matters
Visitors should not have to wait unnecessarily for a webpage to load or become usable. Slow pages can be especially frustrating on mobile devices, slower networks, or devices with limited processing power.
Good performance also improves the overall user experience by allowing content to appear quickly, reducing unexpected layout movement, and making controls responsive sooner.
Performance should be considered while a website is being built rather than treated only as a problem to fix after the site becomes slow.
Keep HTML Simple
Use the markup needed to represent the content and structure without adding unnecessary elements or deeply nested containers.
<div>
<div>
<div>
<p>Welcome to our website.</p>
</div>
</div>
</div>
If the containers provide no useful structure, styling, or scripting purpose, simpler markup is preferable.
<p>Welcome to our website.</p>
For most normal pages, excessive HTML alone is unlikely to be the largest performance problem, but unnecessary markup increases document complexity and gives the browser more elements to parse and maintain.
Reduce Unnecessary Resources
Every external resource referenced by a webpage can require additional downloading and processing. Avoid loading files that the page does not actually need.
<link rel="stylesheet" href="/css/style.css">
<script src="/js/main.js" defer></script>
A page should not load large libraries, scripts, stylesheets, fonts, media, or other resources simply because they are available elsewhere on the website.
Shared resources are useful, but they should earn their place on the page by providing functionality or presentation that is actually required.
Optimize Images
Images are often much larger than the HTML document itself, making image optimization one of the most important performance considerations on image-heavy webpages.
Choose an appropriate image format, resize images to sensible dimensions, and compress them sufficiently for web use without unnecessarily reducing visual quality.
<img src="/img/product.webp" alt="Blue ceramic coffee mug" width="800" height="600">
A webpage should not download a very large image when the visitor only needs a much smaller version. Modern formats and responsive image techniques can also reduce the amount of image data transferred.
Specify Image Dimensions
Providing an image's intrinsic dimensions with the width and height attributes allows the browser to determine its aspect ratio and reserve appropriate space before the image finishes loading.
<img src="/img/photo.jpg" alt="Mountain landscape" width="1200" height="800">
This can reduce unexpected layout movement as images load. CSS can still resize the displayed image responsively while the HTML dimensions describe its intrinsic proportions.
img {
max-width: 100%;
height: auto;
}
Use Responsive Images
Responsive image features allow the browser to select an image appropriate for the visitor's display and layout instead of always downloading the same file.
<img
src="/img/photo-800.jpg"
srcset="/img/photo-400.jpg 400w,
/img/photo-800.jpg 800w,
/img/photo-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw, 800px"
alt="Mountain landscape"
width="1200"
height="800">
The browser uses information from srcset and sizes together with the current display conditions to choose an appropriate image candidate.
This can prevent smaller screens from downloading unnecessarily large images while still providing higher-resolution versions when appropriate.
Lazy Load Offscreen Content
The loading="lazy" attribute can tell the browser that certain images and iframes do not need to load immediately when they are outside the initial viewport.
<img src="/img/gallery-12.jpg" alt="HTML image example" width="800" height="600" loading="lazy">
<iframe src="/example.html" title="HTML example" loading="lazy"></iframe>
Lazy loading can reduce the amount of data and processing required during the initial page load, particularly on long pages containing many images or embedded resources.
Content that is important to the initial view should generally not be lazy loaded because delaying it can make the most important part of the page appear later.
Load Scripts Efficiently
JavaScript can delay HTML parsing when scripts are loaded in ways that require the browser to stop and execute them before continuing through the document.
The defer attribute allows a classic external script to download while the HTML is being parsed and delays execution until the document has been parsed.
<script src="/js/main.js" defer></script>
When several deferred scripts are used, their execution order is preserved. This can be useful when one script depends on another.
The async attribute is another option for classic external scripts that can execute independently as soon as they are available.
<script src="/js/independent-script.js" async></script>
Choose the loading behavior based on what the script does rather than adding defer or async automatically to every script.
Preload Important Resources Carefully
The preload link type can tell the browser about a resource that the current page will need soon so downloading can begin earlier.
<link rel="preload" href="/fonts/example.woff2" as="font" type="font/woff2" crossorigin>
Preloading can help when an important resource would otherwise be discovered late, but it should be used selectively.
Preloading too many resources can compete with other important downloads and reduce the benefit. Only preload resources that are important to the current page and are expected to be used shortly.
Iframes and Media
Iframes, video, audio, maps, and other embedded content can add significant page weight or trigger additional network requests.
<iframe src="/example.html" title="Example content" loading="lazy"></iframe>
Load embedded content only when it provides useful value to the page. Offscreen iframes can often use lazy loading, while media files should be encoded and sized appropriately for web delivery.
For audio and video, the preload attribute can provide a hint about how much media data should be loaded before playback begins.
<video controls preload="metadata">
<source src="/video/movie.mp4" type="video/mp4">
</video>
Using preload="metadata" can allow basic media information to be retrieved without requesting the entire media file in advance.
Watch Third-Party Resources
Third-party scripts, fonts, videos, advertising systems, analytics tools, social widgets, maps, and other external resources can affect performance even when your own HTML is efficient.
Each third-party service can introduce additional network connections, downloads, processing, and dependencies that you do not completely control.
Before adding a third-party resource, consider whether its value justifies its performance cost. Periodically review existing resources because something that was useful when a site was created may no longer be necessary.
Measure Performance
Performance decisions should be based on measurements rather than assumptions. Browser developer tools can show network requests, transferred file sizes, loading times, and other information that helps identify performance problems.
Tools such as PageSpeed Insights can also analyze a webpage and identify opportunities related to loading performance, images, scripts, layout stability, and other aspects of the user experience.
Test important pages under realistic conditions. A website that appears extremely fast on a developer's computer and high-speed connection may behave very differently on a slower network or mobile device.
Common Performance Mistakes
| Mistake | Better Approach |
|---|---|
| Using oversized images | Resize, compress, and serve images appropriate for their intended display. |
| Omitting image dimensions | Provide width and height when the intrinsic dimensions are known. |
| Lazy loading important initial content | Reserve lazy loading primarily for content farther down the page. |
| Loading scripts that a page does not use | Load only resources needed by the page. |
| Using large images for every screen size | Use responsive images where different image sizes provide a meaningful benefit. |
| Preloading too many resources | Preload selectively when early discovery provides a real benefit. |
| Ignoring third-party resources | Measure their cost and remove unnecessary dependencies. |
| Testing performance only on a fast local connection | Measure important pages under more realistic network and device conditions. |
Best Practices
- Keep HTML clear and avoid unnecessary markup.
- Load only the resources required by the page.
- Resize and compress images for web use.
- Use appropriate modern image formats where they provide a benefit.
- Specify image dimensions to help reduce layout movement.
- Use responsive images when visitors can benefit from different image sizes.
- Lazy load offscreen images and iframes when appropriate.
- Avoid lazy loading important content in the initial viewport.
- Use appropriate script-loading strategies such as
deferorasync. - Preload only resources that genuinely benefit from earlier discovery.
- Review the performance cost of third-party resources.
- Measure performance instead of relying only on assumptions.
- Retest performance as the website grows and changes.
Summary
HTML performance is influenced not only by the amount of markup in a document but also by the resources that markup causes the browser to load. Efficient structure, optimized and responsive images, appropriate lazy loading, thoughtful script loading, and careful use of embedded and third-party resources can all improve webpage performance.
Performance should be measured throughout development because websites tend to become heavier as features and resources are added. Next, we will look at HTML Security Basics and several important practices for reducing common security risks in webpages.
