1. Web Design
  2. JavaScript

Best Ways to Preload Images Using JavaScript, CSS, and HTML

Scroll to top
Read Time: 6 min

One of the most important things that you can do to improve the user experience on your website is to make sure that people don't spend their time waiting for an image or other element to load.

How quickly a webpage and all its contents load depends on a large number of factors, and some of them will be beyond your control. However, we should try our best as web developers to make the browsing experience as seamless as possible.

In this tutorial, I will show you different techniques to preload images on a webpage for a smooth user experience.

The Need for Preloading Images

We will begin the tutorial by first discussing why you might need to preload images.

Let's say you are building a portfolio website for a real estate agent where they can showcase houses for sale. The agent wants you to show a list of houses with an image of the exterior of the houses. They also want you to design the page in such a way that hovering over a house image loads another image of the interior of the house, with a link to see all other images.

The problem here is that the image of the interior of the house will only start loading when users hover over the image. This means that they will not see any image for a few moments after the initial hover event, depending on their internet speed. You can see this problem in the following CodePen demo:

Preloading the images will avoid this delay in image load on hover. Also, some large images might take a while to load, so it is better to preload them for a better user experience.

Preloading Images Using HTML

You're probably already familiar with the link tag in HTML. We generally use it to load an external CSS stylesheet, but you can also use it to load other types of resources as well.

There are two important attributes of the link tag:

  1. the href attribute, which is used to provide the path to the resource that we want to fetch
  2. the rel attribute, which specifies the relationship of the resource with the containing document

With a CSS stylesheet, the link tag looks like this:

<link rel="stylesheet" href="navigation.css" />

The rel attribute can take a lot of valid values. One of them is preload, which we will use to preload our images. The preload attribute tells the browser to preemptively fetch and cache the linked resource as it will be needed on the current page.

You also need the as attribute when the value of the rel attribute is set to preload. This will specify the type of content that is being loaded by the link tag. This attribute serves many important purposes, such as applying the correct content security policy, prioritization of the request, etc. Skipping it could prevent your image from being preloaded.

We will load our image in the following div element:

<div class="hover-me"></div>

Then apply the following CSS to the div element. As you can see, the background image URL changes whenever someone hovers over the div element.

div.hover-me {
  width: 640px;
  height: 360px;
  background: url("");
  background-size: contain;
  cursor: pointer;
  margin: 0 auto;
  position: relative;
div.hover-me::before {
  content: "Lake";
  background: black;
  color: white;
  position: absolute;
  top: 0.75rem;
  left: 1rem;
  padding: 0.5rem;
  font-size: 1.5rem;
  border-radius: 5px;
div.hover-me:hover {
  background: url("");
  background-size: contain;
div.hover-me:hover::before {
  content: "Mountains";

The image that shows up when we hover over the div element is preloaded by using the following markup. You should ideally place the link tag inside the head tag of your webpage.

<link rel="preload" as="image" href="" />

The following CodePen demo shows the image preloading in action:

Preloading Images Using CSS

You might have noticed in the previous section that both the images we used were actually applied as a background to the div element. However, only one of them was downloaded by the browser. The image that was applied as background on hover was downloaded only after the hover event occurred.

In the previous section, we forced the hover image to download with the help of HTML. However, we could also trick the browser into downloading the hover image by applying it as a background image to another element on the webpage. Another option involves setting the image URL as a value of the content property.

I prefer to use the body element along with the ::before or ::after pseudo-elements. The URLs that I want to download will be set as a value of the content property of any of the pseudo-elements.

One important thing to keep in mind here is that we need to push the pseudo-elements far off the screen to prevent their contents from accidentally appearing on the screen. The following CSS takes care of all this for us:

body::before {
  content: url("");
  position: absolute;
  top: -9999rem;
  left: -9999rem;
  opacity: 0;

I have also set the opacity to 0 as a precautionary measure. Do keep in mind that you shouldn't set the display property to none to hide the element. In that case, the browser is much more likely not to download the image at all.

You can see that the image we need on hover is preloaded in the following CodePen demo:

Preloading Images Using JavaScript

It is also possible to preload images using JavaScript. This method gives you the most control over the way you preload the images. Preloading images using JavaScript is also more convenient in situations where you have to load a large number of images. However, it will not work if JavaScript execution is disabled in the browser.

The following function can help us preload any image in JavaScript.

function preload_image(im_url) {
  let img = new Image();
  img.src = im_url;

The function accepts the path to an image you want to preload as a parameter. Inside the function, we use the image constructor to create a new instance of HTMLImageElement. After creating the image element instance, we set the value of its src property to the path of the image we want to preload.

All that's needed now is a call to the preload_image() function, as shown below:


You can see the JavaScript image preloading in action in the following CodePen demo:

Final Thoughts

In this tutorial, we learned about three different techniques to preload images. Using the link tag in HTML allows us to start loading images as early as possible. On the other hand, it is much more convenient to use JavaScript when you want to preload multiple images. You can also control the order in which images are preloaded with JavaScript. This way, we can make sure that image preloading doesn't block the main content from loading first.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.