Javascript

How to show Page Loading div until the page has finished loading

27 September 2026 · 9 min read

How to show Page Loading div until the page has finished loading

Have you ever visited a website and been met with a blank screen, unsure if anything is actually happening? It’s a frustrating experience, and it can lead users to abandon your site before it even loads. One effective solution to this problem is to implement a “page loading” indicator. This visually communicates to your visitors that your website is working and content is on its way. In this article, we’ll explore exactly how to show a page loading div until the page has finished loading. We will cover the technical aspects, best practices, and why this small addition can significantly improve your user experience and reduce bounce rates. A well-implemented loading indicator keeps users engaged, provides feedback, and ultimately increases the likelihood that they’ll stick around and explore what your site has to offer.

Why Use a Page Loading Div?

Implementing a page loading div offers numerous benefits beyond just aesthetics. First and foremost, it enhances the user experience. Users are more patient when they know something is happening in the background. Without a visual cue, they might assume the site is broken and leave. According to a study by Google, 53% of mobile site visitors leave a page that takes longer than three seconds to load [1]. A loading indicator can buy you those precious seconds. Secondly, it can improve your site’s perceived performance. Even if the actual loading time remains the same, the perception of speed increases when users see a progress bar or spinner. This is because the loading div provides immediate feedback, setting expectations and preventing frustration. By keeping users informed, you manage their expectations and reduce the chance of them abandoning your site.

Furthermore, implementing a loading div is crucial for websites with heavy content, such as those featuring numerous images, videos, or complex scripts. These elements can take time to load, especially on slower internet connections. A loading indicator bridges this gap, ensuring a smoother transition for the user. For example, imagine an e-commerce site with high-resolution product images. Without a loading indicator, users might see a jumbled mess of unloaded images initially. A loading div, however, masks this issue and presents a polished, professional experience. In essence, a page loading div is an essential component of a user-friendly website, contributing to a more positive and engaging experience. It’s a simple yet effective way to communicate with your users and keep them on your site.

Here are key reasons to use a page loading div:

  • Improved user experience by providing visual feedback.
  • Reduced bounce rates by managing user expectations.
  • Enhanced perceived performance, even if actual loading time is the same.

How to Implement a Basic Page Loading Div

Implementing a basic page loading div involves a combination of HTML, CSS, and JavaScript. First, you’ll need to create the HTML structure for the loading div. This is typically a simple <div> element with a unique ID, such as "loading". Within this div, you can include a spinner, progress bar, or any other visual element to indicate loading. Next, you’ll use CSS to style the loading div and position it appropriately on the page. You’ll likely want to center it and make it cover the entire viewport to prevent users from interacting with the page before it’s fully loaded. The CSS should also include instructions to hide the loading div by default.

The final step involves using JavaScript to show the loading div when the page starts loading and hide it once the page has finished loading. This is typically done using the window.onload event. This event fires when all resources on the page, including images, scripts, and stylesheets, have been loaded. Within the window.onload function, you’ll retrieve the loading div element using its ID and set its display property to "none", effectively hiding it. This ensures that the loading div is only visible while the page is actively loading and disappears once everything is ready. This basic implementation provides a functional loading indicator that can significantly improve the user experience on your website. For a more polished look, consider using CSS animations to create a dynamic spinner or progress bar. Javascript libraries like jQuery can also simplify the DOM manipulation involved.

Here’s a general outline of the implementation process:

  1. Create the HTML structure for the loading div.
  2. Style the loading div using CSS.
  3. Use JavaScript to show the loading div on page load and hide it when loading is complete.

Advanced Techniques and Customization

Beyond the basic implementation, there are several advanced techniques you can use to enhance your page loading div. One common approach is to use a more sophisticated loading animation. Instead of a simple spinner, you could create a custom animation using CSS or JavaScript. Libraries like Lottie [2] allow you to integrate complex vector animations into your website with minimal code. Another technique is to use a progress bar that reflects the actual loading progress. This requires tracking the loading status of individual resources and updating the progress bar accordingly. While more complex to implement, it provides users with a more accurate indication of how much longer they need to wait.

Customization is also key to ensuring that your loading div aligns with your brand identity. You can customize the colors, fonts, and overall design to match your website’s aesthetic. This creates a more cohesive and professional experience. Furthermore, consider adding a brief message or tip to the loading div. This can help engage users and provide them with valuable information while they wait. For example, you could include a random tip related to your website’s content or a brief introduction to your company’s mission. According to Nielsen Norman Group, personalized experiences increase user engagement [3]. This level of customization can transform a simple loading indicator into a branding opportunity and help to improve your site’s bounce rate.

This paragraph is optimized as a featured snippet: To enhance your page loading div, consider using custom CSS animations, implementing a progress bar that reflects actual loading progress, and personalizing the design to align with your brand. You can also add helpful messages or tips to engage users while they wait, turning a simple loading indicator into a branding opportunity. This approach enhances user experience and reduces bounce rates.

Best Practices for Page Loading Divs

When implementing a page loading div, it’s important to follow certain best practices to ensure optimal performance and user experience. First, ensure that the loading div itself loads quickly. Ironically, a slow-loading loading div defeats its own purpose. Use lightweight images or CSS animations to minimize its impact on initial load time. Additionally, avoid using overly complex animations or resource-intensive effects that could slow down the loading process. Keep it simple and efficient. It is important to consider the user’s perspective during this process.

Secondly, be mindful of accessibility. Ensure that the loading div is accessible to users with disabilities, such as those who use screen readers. Provide alternative text for images and use ARIA attributes to convey the loading status. This ensures that all users, regardless of their abilities, have a positive experience. For instance, provide aria-busy=“true” to signal to assistive technologies that the page is loading. Finally, test your loading div on various devices and browsers to ensure that it works consistently across different platforms. This includes testing on mobile devices, tablets, and desktop computers, as well as different browsers like Chrome, Firefox, and Safari. Thorough testing helps identify and resolve any compatibility issues, ensuring a smooth and seamless experience for all users. Regular testing is a key component in ensuring a website works well across all devices.

  • Ensure the loading div loads quickly and efficiently.
  • Prioritize accessibility for users with disabilities.
  • Test the loading div on various devices and browsers.
Infographic here showing the steps of implementing a page loading div
FAQ About Page Loading Indicators ---------------------------------
Why is a page loading div important for user experience?
A page loading div provides visual feedback to users, assuring them that the website is working and content is loading. This reduces frustration and prevents them from leaving the site prematurely.
What are the key elements of a good page loading div?
A good page loading div should be visually appealing, informative, and load quickly. It should also be accessible to users with disabilities and tested on various devices and browsers.
How can I customize my page loading div to match my brand?
You can customize the colors, fonts, animations, and overall design of your loading div to align with your brand identity. Consider adding a brief message or tip to engage users while they wait.
What are some common mistakes to avoid when implementing a page loading div?
Avoid using overly complex animations or resource-intensive effects that could slow down the loading process. Also, ensure that the loading div itself loads quickly and is accessible to all users.
Showing a page loading div until the page has finished loading is more than just a cosmetic enhancement; it's a crucial element of good web design that significantly impacts user experience. By providing visual feedback and managing expectations, you can keep users engaged and reduce bounce rates. Remember to prioritize simplicity, accessibility, and brand consistency when implementing your loading div. A well-designed loading indicator not only improves the user experience but also reflects positively on your brand's attention to detail. Whether you choose a simple spinner or a complex animation, the key is to keep users informed and engaged during the loading process. Ready to take your website's user experience to the next level? Explore further resources and tutorials to master the art of creating seamless and engaging loading experiences. If you're interested in other ways to improve your website’s performance, check out [our guide to optimizing website images](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c).

Question & Answer :
I have a section on our website that loads quite slowly as it’s doing some intensive calls.

Any idea how I can get a div to say something similar to “loading” to show while the page prepares itself and then vanish when everything is ready?

Original Answer

I’ve needed this and after some research I came up with this (jQuery needed):

First, right after the <body> tag add this:

<div id="loading"> <img id="loading-image" src="path/to/ajax-loader.gif" alt="Loading..." /> </div> 

Then add the style class for the div and image to your CSS:

#loading { position: fixed; display: block; width: 100%; height: 100%; top: 0; left: 0; text-align: center; opacity: 0.7; background-color: #fff; z-index: 99; } #loading-image { position: absolute; top: 100px; left: 240px; z-index: 100; } 

Then, add this javascript to your page (preferably at the end of your page, before your closing </body> tag, of course):

<script> $(window).load(function() { $('#loading').hide(); }); </script> 

Finally, adjust the position of the loading image and the background-color of the loading div with the style class.

This is it, should work just fine. But of course you should have an ajax-loader.gif somewhere or use base64 url for image’s src value. Freebies here. (Right-click > Save Image As…)

Update

For jQuery 3.0 and above you can use:

<script> $(window).on('load', function () { $('#loading').hide(); }) </script> 

Update

The original answer is from jQuery and before flexbox era. You can use many view management libraries / frameworks now like Angular, React and Vue.js. And for CSS you have flexbox option. Below is CSS alternative:

#loading { position: fixed; display: flex; justify-content: center; align-items: center; width: 100%; height: 100%; top: 0; left: 0; opacity: 0.7; background-color: #fff; z-index: 99; } #loading-image { z-index: 100; }