Understanding Image Loading and Performance
Images can account for a significant amount of the data downloaded by a webpage, making image loading and optimization an important part of website performance.
HTML provides features such as the loading attribute, responsive images, and explicit image dimensions that can work together with properly optimized image files to improve loading speed and the user experience.
The loading Attribute
The loading attribute provides a hint to the browser about when an image should be loaded. It can use the values lazy or eager.
<img src="/img/images/image-600x400.png"
alt="HTML image example"
width="600"
height="400"
loading="lazy">
Browsers ultimately control when resources are requested, but the loading attribute allows the page author to indicate whether an image may be deferred or should be loaded normally.
Lazy Loading Images
Lazy loading allows images that are initially outside the visible area of the webpage to be deferred until the browser determines they are approaching the viewport.
<img src="/img/images/image-600x400.png"
alt="HTML image example"
width="600"
height="400"
loading="lazy">
This can reduce the amount of image data downloaded during the initial page load, especially on pages containing many images below the visible portion of the page.
Eager Loading Images
The eager value tells the browser that the image should be loaded without intentionally deferring it because it is outside the viewport.
<img src="/img/images/image-600x400.png"
alt="HTML image example"
width="600"
height="400"
loading="eager">
Images that are immediately important to the visible page content should generally not be lazy loaded. In many cases, simply omitting the loading attribute is appropriate for these images.
Specify Image Dimensions
Including the intrinsic width and height attributes allows the browser to determine the image's aspect ratio before the image file finishes loading.
<img src="/img/images/image-600x400.png"
alt="HTML image example"
width="600"
height="400">
The browser can reserve the appropriate amount of space while the image loads, helping prevent surrounding content from unexpectedly shifting when the image appears.
Use Responsive Images
Responsive images can improve performance by allowing the browser to choose an appropriately sized image instead of downloading a much larger file than the display requires.
<img src="/img/images/image-600x400.png"
srcset="/img/images/image-300x200.png 300w,
/img/images/image-600x400.png 600w,
/img/images/image-900x600.png 900w,
/img/images/image-1200x800.png 1200w"
sizes="(max-width: 600px) 100vw, 600px"
alt="Responsive image example"
width="600"
height="400">
The browser evaluates the available image candidates along with factors such as the image's expected display size and device pixel density, then selects an appropriate resource.
Use Optimized Images
Image performance also depends on the files themselves. An image should generally be saved at suitable dimensions and compressed enough to reduce its file size without creating unacceptable visual degradation.
A 3000-pixel-wide photograph, for example, usually should not be downloaded when it will only be displayed a few hundred pixels wide. Providing appropriately sized image resources reduces unnecessary data transfer.
Choose an Appropriate Image Format
Different image formats provide different combinations of compression, transparency, image quality, and browser support. Choosing an appropriate format can substantially affect the amount of data a browser must download.
Formats such as WebP and AVIF can often provide smaller files than older formats for comparable visual quality, while PNG, JPEG, SVG, and other formats remain useful for particular types of images.
Image Loading Example
The following example contains several images positioned farther down the page and uses loading="lazy". Scroll through the editor preview to see the images as they approach the visible area.
Image Performance Best Practices
- Use
loading="lazy"for appropriate images that begin outside the visible portion of the page. - Avoid lazy loading an important image that is expected to appear immediately when the page opens.
- Include
widthandheightattributes to establish the image's intrinsic aspect ratio. - Use responsive images when different image sizes can reduce unnecessary downloads.
- Resize and compress image files appropriately before publishing them.
- Choose an image format suited to the content and performance requirements.
- Avoid downloading very large images only to display them at much smaller dimensions.
Summary
Efficient image loading can improve both webpage speed and visual stability. HTML features such as loading="lazy", explicit width and height attributes, and responsive image sources help browsers load images more efficiently, while appropriate dimensions, compression, and file formats reduce the amount of data that must be downloaded.
