Creating fancy CSS3 fade in animations on page load

Oh hi there, have you ever wanted to create fade in like animations on page load? Think Google homepage, or even our site has plenty of them. This is different than having your animations come in as you scroll. This will all be done using CSS3 so this will work on all modern browsers except of course IE7, Ie8 and 9. IE10 should support them though, so now is a good time to start practicing. This is a  super quick tutorial so sorry, there won’t be any body html code etc (ask in the comments if you have any questions).

UPDATE: in a hurry?  Add this CSS to yours to have instant fade in effects.

We will cover how to achieve this:

See the Pen gaWMqm by Alex (@fabriceleven)

What we will do

We will create 3 boxes and they will fade in one after another. Here are our steps to accomplish this:

  1. Create a div in our html that we want to animate
  2. Create keyframes in our css file (these basically will define how things change )
  3. Create div tag in our css, define our animation (duration, start delay etc) and link it to our keyframes

So let’s get started

 This CSS3 code will work on Firefox, Chrome, Safari and IE9+) 

Ok let’s make some basic boxes

<body>
<div class="box fade-in one">
       look at me fade in
</div>
 
<div class="box fade-in two">
       Oh hi! i can fade too!
</div>
 
<div class="box fade-in three">
       Oh hi! i can fade three!
</div>
 
</body>

Ok so the above basically makes 3 boxes, we named them box , the fade-in is going to be our animation class and the number after is just there so we can have them load in an order we want. And now for the magic code.

/* make keyframes that tell the start state and the end state of our object */

@-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

.fade-in {
	opacity:0;  /* make things invisible upon start */
	-webkit-animation:fadeIn ease-in 1;  /* call our keyframe named fadeIn, use animattion ease-in and repeat it only 1 time */
	-moz-animation:fadeIn ease-in 1;
	animation:fadeIn ease-in 1;

	-webkit-animation-fill-mode:forwards;  /* this makes sure that after animation is done we remain at the last keyframe value (opacity: 1)*/
	-moz-animation-fill-mode:forwards;
	animation-fill-mode:forwards;

	-webkit-animation-duration:1s;
	-moz-animation-duration:1s;
	animation-duration:1s;
}

.fade-in.one {
-webkit-animation-delay: 0.7s;
-moz-animation-delay: 0.7s;
animation-delay: 0.7s;
}

.fade-in.two {
-webkit-animation-delay: 1.2s;
-moz-animation-delay:1.2s;
animation-delay: 1.2s;
}

.fade-in.three {
-webkit-animation-delay: 1.6s;
-moz-animation-delay: 1.6s;
animation-delay: 1.6s;
}

/*---make a basic box ---*/
.box{
width: 200px;
height: 200px;
position: relative;
margin: 10px;
float: left;
border: 1px solid #333;
background: #999;
}

That’s all the code we will need.

Here is how it works

The keyframes (the reason there are 3 of them is to support webkit, firefox and future browsers) basically say the start state of our box and then the end state. Since we want the box to fade we start at opacity 0 and by the time we are done we end at opacity 1.

You can also put other parameters in there as well. For example you can start with width:100px; and end with width: 500px; etc. The fade-in class tells what kind of animation we will perform.  Which is basically: go do keyframes called fadeIn, use ease-in animation and only do the animation once. Then stay at the last keyframe (-webkit-animation-fill-mode:forwards😉 to make sure our boxes don’t disappear and do all this in 1s: (-webkit-animation-duration: 1s).

The next 3 classes just give our animations different delays so they start one after another upon page load. And that’s it really.

Since IE9 doesn’t support css3 animations but does support opacity: 0; property you will have to have ie9 load a separate ie9 css where you have all your fade classes set to opacity: 1. You can use inline CSS browser selection by doing: opacity: 1\9; /* IE9 only */

You can have the duration in different classes as well, like:  .one, .two etc that’s how we achieved the boxes loading right after another. You can see our chat icon slide out from bottom right corner on our design blog index page. We use fade along with a few other properties in the keyframe.

That’s about it, if you want a quick CSS you can just add to yours with a few prebuilt animations like fade down, up, left, right etc then see hereLet us know how you used it and if you have any questions or comments.

UPDATE:
Here is quick tutorial on how to make animations come in on page scroll 

 

98 responses

  1. how would i loop though this infinitely or javascript reload at the end of the animation? (ie: keep starting over?)

      1. I tried putting that in both the .fade-in and .fade-in.###. unfortunately, it just makes the first image loop, not the rest.

  2. how would i loop though this infinitely or javascript reload at the end of the animation? (ie: keep starting over?)

      1. I tried putting that in both the .fade-in and .fade-in.###. unfortunately, it just makes the first image loop, not the rest.

    1. You would just add another level into your keyframe, so first it starts hidden, then it would show up and end hidden again, like:
      @keyframes FadeIn {
      0% { opacity: 0; }
      50% { opacity: 1; }
      100% { opacity: 0; }
      }

      1. Hi Alex,

        I replaced your original code:

        @-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

        @-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

        @keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

        with this code:

        @-webkit-keyframes FadeIn {

        0% { opacity: 0; }

        50% { opacity: 1; }

        100% { opacity: 0; }

        }

        @-moz-keyframes FadeIn {

        0% { opacity: 0; }

        50% { opacity: 1; }

        100% { opacity: 0; }

        }

        @keyframes FadeIn {

        0% { opacity: 0; }

        50% { opacity: 1; }

        100% { opacity: 0; }

        }

        No change. What am I doing wrong?

        1. Oops there was a typo, the name of the keyframe needs to match exactly so it should be

          @-webkit-keyframes fadeIn {
          0% { opacity: 0; }
          50% { opacity: 1; }
          100% { opacity: 0; }
          }

          @-moz-keyframes fadeIn {
          0% { opacity: 0; }
          50% { opacity: 1; }
          100% { opacity: 0; }
          }

          @keyframes fadeIn {
          0% { opacity: 0; }
          50% { opacity: 1; }
          100% { opacity: 0; }
          }

          Then modify the animation duration value to change how long you want it visible

          1. Still not working. Fades in, but won’t fade out.

            Here is my code:

            @-webkit-keyframes fadeIn {
            0% { opacity: 0; }
            50% { opacity: 1; }
            100% { opacity: 0; }
            }
            @-moz-keyframes fadeIn {
            0% { opacity: 0; }
            50% { opacity: 1; }
            100% { opacity: 0; }
            }
            @-o-keyframes fadeIn {
            0% { opacity: 0; }
            50% { opacity: 1; }
            100% { opacity: 0; }
            }
            @keyframes fadeIn {
            0% { opacity: 0; }
            50% { opacity: 1; }
            100% { opacity: 0; }
            }

            .big {
            opacity:0;
            -webkit-animation:fadeIn ease-in 1;
            -moz-animation:fadeIn ease-in 1;
            -o-animation:fadeIn ease-in 1;
            animation:fadeIn ease-in 1;

            -webkit-animation-fill-mode:forwards;
            -moz-animation-fill-mode:forwards;
            -o-animation-fill-mode:forwards;
            animation-fill-mode:forwards;

            -webkit-animation-duration:3s;
            -moz-animation-duration:3s;
            -o-animation-duration:3s;
            animation-duration:3s;

            -webkit-animation-delay: 0s;
            -moz-animation-delay: 0s;
            -o-animation-delay: 0s;
            animation-delay: 0s;

          2. You are missing a closing tag and you don’t have proper style declaration . I suggest you create a new codepen and test things there. The duration is how long the whole thing takes adjust the values in codepen and see how it effects it. For your case you might need to have 2 separate keyframes, one to fade it in and then start another keyframe (from opacity 1 to opacity 0) after some start delay to fade it out

          3. Regarding animation duration—what does that parameter specifically refer to? I see two parts: How long it takes for the animation to go from nothing to full fade in; how long the animation stays in the full fade in mode until it begins to fade out.

            Are there separate adjustments for each of these?

            Thanks.

    1. You would just add another level into your keyframe, so first it starts hidden, then it would show up and end hidden again, like:
      @keyframes FadeIn {
      0% { opacity: 0; }
      50% { opacity: 1; }
      100% { opacity: 0; }
      }

      1. Hi Alex,

        I replaced your original code:

        @-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

        @-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

        @keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

        with this code:

        @-webkit-keyframes FadeIn {

        0% { opacity: 0; }

        50% { opacity: 1; }

        100% { opacity: 0; }

        }

        @-moz-keyframes FadeIn {

        0% { opacity: 0; }

        50% { opacity: 1; }

        100% { opacity: 0; }

        }

        @keyframes FadeIn {

        0% { opacity: 0; }

        50% { opacity: 1; }

        100% { opacity: 0; }

        }

        No change. What am I doing wrong?

        1. Oops there was a typo, the name of the keyframe needs to match exactly so it should be

          @-webkit-keyframes fadeIn {
          0% { opacity: 0; }
          50% { opacity: 1; }
          100% { opacity: 0; }
          }

          @-moz-keyframes fadeIn {
          0% { opacity: 0; }
          50% { opacity: 1; }
          100% { opacity: 0; }
          }

          @keyframes fadeIn {
          0% { opacity: 0; }
          50% { opacity: 1; }
          100% { opacity: 0; }
          }

          Then modify the animation duration value to change how long you want it visible

          1. Still not working. Fades in, but won’t fade out.

            Here is my code:

            @-webkit-keyframes fadeIn {
            0% { opacity: 0; }
            50% { opacity: 1; }
            100% { opacity: 0; }
            }
            @-moz-keyframes fadeIn {
            0% { opacity: 0; }
            50% { opacity: 1; }
            100% { opacity: 0; }
            }
            @-o-keyframes fadeIn {
            0% { opacity: 0; }
            50% { opacity: 1; }
            100% { opacity: 0; }
            }
            @keyframes fadeIn {
            0% { opacity: 0; }
            50% { opacity: 1; }
            100% { opacity: 0; }
            }

            .big {
            opacity:0;
            -webkit-animation:fadeIn ease-in 1;
            -moz-animation:fadeIn ease-in 1;
            -o-animation:fadeIn ease-in 1;
            animation:fadeIn ease-in 1;

            -webkit-animation-fill-mode:forwards;
            -moz-animation-fill-mode:forwards;
            -o-animation-fill-mode:forwards;
            animation-fill-mode:forwards;

            -webkit-animation-duration:3s;
            -moz-animation-duration:3s;
            -o-animation-duration:3s;
            animation-duration:3s;

            -webkit-animation-delay: 0s;
            -moz-animation-delay: 0s;
            -o-animation-delay: 0s;
            animation-delay: 0s;

          2. You are missing a closing tag and you don’t have proper style declaration . I suggest you create a new codepen and test things there. The duration is how long the whole thing takes adjust the values in codepen and see how it effects it. For your case you might need to have 2 separate keyframes, one to fade it in and then start another keyframe (from opacity 1 to opacity 0) after some start delay to fade it out

          3. Regarding animation duration—what does that parameter specifically refer to? I see two parts: How long it takes for the animation to go from nothing to full fade in; how long the animation stays in the full fade in mode until it begins to fade out.

            Are there separate adjustments for each of these?

            Thanks.

    1. It’s just how the CSS3 animation feature works. If you set an animation on an element, it’ll start after whatever delay you have set.

    1. It’s just how the CSS3 animation feature works. If you set an animation on an element, it’ll start after whatever delay you have set.

  3. I’m having trouble getting this to work properly. If I set opacity: 0; on the element to begin with, it won’t fade in.

  4. I’m having trouble getting this to work properly. If I set opacity: 0; on the element to begin with, it won’t fade in.

  5. Hi, everything works fine and suits my needs, but I have trouble to get
    it to work on IE. I have tried with your recommendations with loading a
    separate file for IE9. Can you please help?

  6. Hi, everything works fine and suits my needs, but I have trouble to get
    it to work on IE. I have tried with your recommendations with loading a
    separate file for IE9. Can you please help?

  7. It’s taken me days to find this great tutorial and I thought you had answered my prayers. Except. I’m guessing maybe this technique only works for graphics or images? Is it possible to use this technique to fade in ? I’ve tried every combination of syntax I can think of and nothing works. Is it just not possible? Thanks.

  8. It’s taken me days to find this great tutorial and I thought you had answered my prayers. Except. I’m guessing maybe this technique only works for graphics or images? Is it possible to use this technique to fade in ? I’ve tried every combination of syntax I can think of and nothing works. Is it just not possible? Thanks.

    1. IE9 actually doesn’t support animations. You have to add “opacity: 19; ” to the initial keyframes so that those items always show up on IE9. I have updated the codepen as well.

      1. Thanks a lot! I found another issue. Opacity doesnt work with IE11. Thats why it didnt work. How can you make it work on IE11?

    1. IE9 actually doesn’t support animations. You have to add “opacity: 19; ” to the initial keyframes so that those items always show up on IE9. I have updated the codepen as well.

      1. Thanks a lot! I found another issue. Opacity doesnt work with IE11. Thats why it didnt work. How can you make it work on IE11?

  9. Very nice animation! Thanks a Lot. But how can i make the animation after 10 seconds run again without clicking? Thanks for an answer!

      1. Hi Alex, unfortenateky nothing worked for me. The first two demos doesnt work in Firefox. Maybe you canpost a code that will work for your example above? So that the Animation reloads after 10 seconds? That would be great, thanks!

  10. Very nice animation! Thanks a Lot. But how can i make the animation after 10 seconds run again without clicking? Thanks for an answer!

      1. Hi Alex, unfortenateky nothing worked for me. The first two demos doesnt work in Firefox. Maybe you canpost a code that will work for your example above? So that the Animation reloads after 10 seconds? That would be great, thanks!

  11. I wanted to test this on mobile but the site seems to hang on mobile Safari. Scaling the browser window down still shows the header but otherwise on mobile the heading image doesn’t load at all.

      1. Hey Alex,

        It was the actual blog page. Seems like the header image is trying to load but just hangs (you can see a sliver of the content underneath as you scroll but the header is kind of forced into place and never loads beyond there). The codepen works fine though, just tried it out.

        1. Yeah you are right, we had our site cached so we didn’t see it. Thanks for reporting it, its all fixed now 🙂

  12. I wanted to test this on mobile but the site seems to hang on mobile Safari. Scaling the browser window down still shows the header but otherwise on mobile the heading image doesn’t load at all.

      1. Hey Alex,

        It was the actual blog page. Seems like the header image is trying to load but just hangs (you can see a sliver of the content underneath as you scroll but the header is kind of forced into place and never loads beyond there). The codepen works fine though, just tried it out.

        1. Yeah you are right, we had our site cached so we didn’t see it. Thanks for reporting it, its all fixed now 🙂

  13. Are these delayed animations help with loading page, if someone has very slow internet connection? If not, what will he see, if element doesn’t load on time, when animation should trigger?

    1. They are delayed based on a timer. They are loaded with your css file so it’s one of the first things that’s loaded so don’t worry about them not showing up.

  14. Are these delayed animations help with loading page, if someone has very slow internet connection? If not, what will he see, if element doesn’t load on time, when animation should trigger?

    1. They are delayed based on a timer. They are loaded with your css file so it’s one of the first things that’s loaded so don’t worry about them not showing up.

  15. Hi.How can I give different animation to each image with infinite iteration??? Like first one will zoom in, second one will fade in and third one will slide in from right.
    And infinite iteration means when all images finish loading, it should start from beginning again.

  16. Hi.How can I give different animation to each image with infinite iteration??? Like first one will zoom in, second one will fade in and third one will slide in from right.
    And infinite iteration means when all images finish loading, it should start from beginning again.

  17. Hi there,

    i’m interested in how you perform you animation similar to the one that your homepage demonstrates.

  18. Hi there,

    i’m interested in how you perform you animation similar to the one that your homepage demonstrates.

Leave a Reply