Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

HTML Tutorial

HTML HOME HTML Introduction HTML Editors HTML Basic HTML Elements HTML Attributes HTML Headings HTML Paragraphs HTML Styles HTML Project I HTML Formatting HTML Quotations HTML Comments HTML Colors HTML CSS HTML Project II HTML Links HTML Images HTML Favicon HTML Page Title HTML Tables HTML Lists HTML Project III HTML Block & Inline HTML Div HTML Classes HTML Id HTML Project IV HTML Buttons HTML Iframes HTML JavaScript HTML File Paths HTML Head HTML Layout HTML Responsive HTML Project V HTML Computercode HTML Semantics HTML Project VI HTML Dialog HTML Style Guide HTML Entities HTML Symbols HTML Emojis HTML Character Sets HTML URL Encoding HTML Performance HTML SEO

HTML Forms

HTML Forms HTML Form Attributes HTML Form Elements HTML Input Types HTML Input Attributes Input Form Attributes HTML Project VII

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Video HTML Audio HTML YouTube HTML Project VIII

Web Accessibility

Web Accessibility ARIA Attributes Keyboard Focus and tabindex Landmarks Skip Links Color Contrast

Web APIs

Web APIs Geolocation API Drag and Drop API Popover API Web Storage API Web Workers API Server-Sent Events API

HTML Cert

HTML Certificate

HTML Examples

HTML Examples HTML Videos HTML Editor HTML Quiz HTML Exercises HTML Challenges HTML Projects HTML Website HTML Syllabus HTML Study Plan HTML Interview Prep HTML Summary

HTML References

HTML Tag List HTML Attributes HTML Global Attributes HTML Browser Support HTML Event Attributes HTML Colors HTML Canvas HTML Audio/Video HTML Doctypes HTML Character Sets HTML URL Encode HTML Language Codes HTML Country Codes HTTP Status Codes HTTP Request Methods PX to EM Converter Keyboard Shortcuts

HTML Performance


Share

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
  • defer and async stop 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

Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.