Treehouse: Grow your CSS skills. Land your dream job.

Last updated on:

Display Loading Graphic Until Page Fully Loaded

<!DOCTYPE html>
<html class="no-js">

	<meta charset='UTF-8'>
	<title>Simple Loader</title>
		/* This only works with JavaScript, 
		   if it's not present, don't show loader */
		.no-js #loader { display: none;  }
		.js #loader { display: block; position: absolute; left: 100px; top: 0; }
	<script src=""></script>
	<script src=""></script>
		// Wait for window load
		$(window).load(function() {
			// Animate loader off screen
				top: -200
			}, 1500);

	<img src="download.png" id="loader">
	<img src="">


Reference URL


  1. Avkash
    Permalink to comment#

    Thanks for sharing….. Actually I’m finding the same thing. Very helpful to me….

  2. Seems really good, just; I’d change the
    <img src=”download.png” id=”loader”>
    <img src=”download.gif” id=”loader”>
    Otherwise it wouldn’t be animated.

  3. Been trying to find a way without JavaScript, but until then, your solution will do the trick. Thanks.

  4. This doesn’t seem to work with swf content embedded on the page.

    If you look here, the loading symbol stays even when the content is fully loaded. Any way to work on this ?
    Test (online 08/01/2012)

  5. Yogesh
    Permalink to comment#

    thanks a lot.. :)

  6. sam
    Permalink to comment#

    hi.. thanks for the tutorial.. i applied this loader to the home page.. and when i go to other page and come back to home page the loader play’s again.. how to make the loader work once.. and not the when coming back from other pages.

    • Mark Calma
      Permalink to comment#

      Hello, about your question, you need to create session to trap when your homepage loaded already like example below,

      //this will not be seen when page load due to css
          $(window).load(function() {
                          url: 'trap.php',
                              type: 'get',
                              success: function(visited){
                                  if (visited==0){//when not yet visited


      if (!isset($_SESSION['trap'])||empty($_SESSION['tap'])){
          echo '1';
          echo '1';
  7. Nina
    Permalink to comment#

    Very nice !
    It helps me so much, thanks :D !

  8. Thanks a lot for this snippet … I’m using it on my chrome extension for fonts … ;)

  9. lakshman
    Permalink to comment#

    this snippet is not working for me properly…
    the download gif image is not loading properly….

    • Mark Calma
      Permalink to comment#

      hello, kindly setup the style of the image just like width=”50″ height=”50″ or make some css if your familiar with it. if you want some example just pm me.

  10. Permalink to comment#

    Hi, just as Mr. Laksman says, the snippet isn’t working, probably.
    Could you check on it please?

    • Ebrahim

      remove .js and .no-js from the css part. Keep only onllowing:

           #loader { display: none; background:#000; }
           #loader { display: block; position: absolute; left: 100px; top: 0;background:#000; }

      Thus it’s working …

  11. Not working for me in Google Chrome.

  12. Hi, the link to modernizr.js seems to be broken and what does download.png looks like? I wish There was demo for this.

  13. Ebrahim

    How can i show a black background while the loader is spinning ? Currently It’s in white background.

    • @Ebrahim,
      You could do:

      <body class="black">

      And then, once the page is loaded:

      $(window).load(function() {

      Don’t forget the css styles for the black class: { background-color: black; }

      And if you want the transition to be animated from black to whatever the background color would be after load:

      body {
      -webkit-transition: background-color 0.3s ease-in-out;
      -o-transition: background-color 0.3s ease-in-out;
      transition: background-color 0.3s ease-in-out;
  14. ali
    Permalink to comment#

    thank you, but this won’t work. see page load documentations.

  15. nicolas
    Permalink to comment#

    load event only fires when the page finished loading. Tested it with alert(“loading”) already.

  16. Ricardo
    Permalink to comment#

    It does not work in chrome also in safari too. It’s not working :(

  17. Rajesh
    Permalink to comment#

    This displays loader when page starts loading, but never hides after finished loading..

  18. Slapul
    Permalink to comment#

    I made it work with

    $(window).load(function() {

    after body i display an image

    <img src="" id="loader">

    then everything till end of body is in a div with id=”page”

    This way until the page fully loads the only thing that is displayed is an gif animation

    • JayPaul
      Permalink to comment#

      Slapul – would you mind posting entire code so that we may see for use. Having issues trying to replicate … Thanks in advance!

    • Carrie

      Thank you! That works for me. :-)

    • Slapul
      Permalink to comment#

      I tried many solutions, every one works different.
      This one is the fastest. The full code would be :
      i head you have

      $(window).load(function() {

      When the page is fully loaded the animated gif is swapped with the page div.

      Pace script works too and is more customizable very nicely, but it is slow and is showing even after the page is loaded. I use this one when working with very large queries and fits perfectly.

  19. Permalink to comment#

    if someone wants a no-js solution
    displays a loading gif until the image loads and hides behing the image as it loads. quite easy but a bit different from the js solution above.

    img {background:url('loading.gif');background-repeat:no-repeat;background-position:center}
    <img src="image.png" width="300" height="300"/>
  20. Not working in chrome 33. Just shows the<img> element!

  21. Razzbee

    You can use pacejs @ its a very good plugin, simple to use and no setup required

    • Sparky

      Using my own copy of ‘modernizr-latest.js’ (link above), my own copy of ‘jquery.min.js’ and my own .gif, it works PERFECT in IE8, Chrome, and Firefox!! Very sweet code!! I never link to other sites for .js files. You never know when/if they are gonna load :)

  22. Vini
    Permalink to comment#

    This works like a charm for me. simple light and easy :)

  23. Emi
    Permalink to comment#

    I have a problem here, the loader is beeing shown over the page content, instead of beeing shown on a black background ’till the page is fully loaded… What did I do wrong ?

  24. Hi,

    This piece of code is working, the only thing you have to change is

    and use this as your download image

    I hope this helps … :)

  25. I am not using Modernizer.js on my project yet so can you please tell me that which modernizr modules are used to achieve this so that i dont end up using the whole library when a shelf would suffice

  26. Marcelo Brito
    Permalink to comment#

    Thanks man, this is so useful

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";