Pure CSS3 Image Fade In after Page Load


Pure CSS3 Image Fade In after Page Load
Demo

Now with support for Opera Browsers

Pure CSS3 Image Fade In after Page Load

I was recently proposed with the question: Is it possible to have images fade in shortly after the page has loaded and stay faded in and can the images fade in at different rates and times? This automatically made me think of the jQuery fadeIn feature. Does this mean jQuery is soon to be replaced by CSS as well!

Is it Possible?

I have not actually seen anyone try to make this using pure CSS, most just revert to jQuery. So I immediately decided I wasn’t even going to try and Google this and see what I could come up with.

Game Plan

Alright so we want to make this a dynamic as possible, so what do we need for a basic setup? I decided three classes would be perfect for this. One for the generic class to identify whichΒ  images to fade in, One to decide how long for the image to fade in, and lastly how long should it take before the image starts to fade in.

So lets setup the basic CSS required. First things first let us set up the keyframes for future use.

CSS3 Keyframes

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

This is as simple as keyframes could be, a nice and simple animation from 0 percent opacity all the way to 100 percent opacity. I named each keyframe to be similar to what jQuery uses (fadeIn).

.fadeIn

	/* Styling for Demo
	---------------------------------------------------- */
	border:5px solid #c9c9c9;
	margin:15px;
	-webkit-box-shadow: 0px 0px 5px #333;
	-moz-box-shadow: 0px 0px 5px #333;
	-ms-box-shadow: 0px 0px 5px #333;
	-o-box-shadow: 0px 0px 5px #333;
	box-shadow: 0px 0px 5px #333;

	/* CSS needed for animation
	---------------------------------------------------- */
	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;

Let us start by ignoring everything “Styling for Demo” and lets break down the “CSS needed for animation

opacity:0;

This hides the image for being displayed on page load

/* The following sets up the animation for the most popular browsers */
-webkit-animation:fadeIn ease-in 1;
-moz-animation:fadeIn ease-in 1;
animation:fadeIn ease-in 1;

For those who may not know CSS3 all the well, Let me explain how this animation statement is working. Animation is used to tell the class or ID which keyframe to play. I’ll explain which each element of this particular animation statement is doing. animation: [KEYFRAME NAME] [TIMING FUNCTION] [ITERATION COUNT];

KEYFRAME NAME: Specifies the name of the @keyframes animation
TIMING FUNCTION: Describes how the animation will progress over one cycle of its duration
ITERATION COUNT: Specifies the number of times an animation is played.

This animation statement is telling the “fadeIn” class that is should play the “fadeIn” keyframe useing a ease-in animation and to only do it once.

animation-fill-mode:forwards;

This statement is used to tell the animation not to reset to 0 once it is complete. It well hold the “100%” or “to” status of the keyframe. This way once the the image is faded in the opacity is not reset to 0 again.

Now let us go over the css classes we are going to use for the duration it takes for each image to fade in.

.fadeIn-Xs

	-webkit-animation-duration: Xs;
	-moz-animation-duration: Xs;
	-o-animation-duration: Xs;
	animation-duration: Xs;

This is pretty clear without out being described, but this simply tells the animation to play for X seconds. Simply create this class and change the “X“‘s to how many seconds you would like the animation to play for. An example would be the following classes.

.fadeIn-3s {
	-webkit-animation-duration:3s;
	-moz-animation-duration:3s;
	-o-animation-duration:3s;
	animation-duration:3s;
}

.fadeIn-5s {
	-webkit-animation-duration:5s;
	-moz-animation-duration:5s;
	-o-animation-duration:5s;
	animation-duration:5s;
}

We are are the last part of CSS code well need now. We need some CSS classes that delay the image animation so it wont start playing immediately once the page loads but X seconds afterwords.

.fadeIn-Delay-Xs

	-webkit-animation-delay: Xs;
	-moz-animation-delay: Xs;
	-o-animation-delay: Xs;
	animation-delay: Xs;

This is similar to the “fadeIn-Xs” class except it is delaying the animation for starting for “X” seconds. Simply replace the “X“‘s with the amount of seconds you would like to delay the animation from playing.

As before, here are two examples of what I chose to use.

.fadeIn-Delay-3s {
	-webkit-animation-delay:3s;
	-moz-animation-delay:3s;
	-o-animation-delay:3s;
	animation-delay:3s;
}

.fadeIn-Delay-5s {
	-webkit-animation-delay:5s;
	-moz-animation-delay:5s;
	-o-animation-delay:5s;
	animation-delay:5s;
}

Lets Look at it all Together Now

.fadeIn {
	/* Styling for Demo
	---------------------------------------------------- */
	border:5px solid #c9c9c9;
	margin:15px;
	-webkit-box-shadow: 0px 0px 5px #333;
	-moz-box-shadow: 0px 0px 5px #333;
	-ms-box-shadow: 0px 0px 5px #333;
	-o-box-shadow: 0px 0px 5px #333;
	box-shadow: 0px 0px 5px #333;

	/* CSS needed for animation
	---------------------------------------------------- */
	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;
}

/* Animation Times - Time for image to fade in
---------------------------------------------------- */
.fadeIn-3s {
	-webkit-animation-duration:3s;
	-moz-animation-duration:3s;
	-o-animation-duration:3s;
	animation-duration:3s;
}

.fadeIn-5s {
	-webkit-animation-duration:5s;
	-moz-animation-duration:5s;
	-o-animation-duration:5s;
	animation-duration:5s;
}

/* Animation Delay - Time for image to be delayed
---------------------------------------------------- */
.fadeIn-Delay-3s {
	-webkit-animation-delay:3s;
	-moz-animation-delay:3s;
	-o-animation-delay:3s;
	animation-delay:3s;
}

.fadeIn-Delay-5s {
	-webkit-animation-delay:5s;
	-moz-animation-delay:5s;
	-o-animation-delay:5s;
	animation-delay:5s;
}

/* Key Frames
---------------------------------------------------- */
@-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@-o-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

Alright now I now that seems like an incridiablly long amount of code, but are you ready to see what all of this beautiful CSS3 code can do in a nice and simple image tag.

HTML Markup

<img class="fadeIn fadeIn-3s fadeIn-Delay-3s" src="path/to/image.jpg" alt="" />

Alright, now lets explain what this is doing. First we need to link the img tag to an image to use. Next we need to include the classes that tell the image what to do. First we add the class “fadeIn” this tells the image that it well be faded in. Secondly “fadeIn-3s” tells the image that it well take 3 seconds to fade in. Lastly “fadeIn-Delay-3s” tells the image that it well not fade in until 3 seconds after the page has been loaded.

CSS3 jQuery-Like fadeIn

Incoming search terms:


css3 fadein,  html5 fade in image,  css3 fade in,  css3 fade in onload,  css3 fade in on load,  css3 image fade in,  
Robert McIntosh

Robert McIntosh

Hello, I am a website developer and computer programmer from Erie, Pa. of the United States. I currently work for The School District of the City of Erie, Pa. and in my free time I am a website developer to help my local community.

I am highly skilled in HTML/HTML5, CSS/CSS3, JavaScript, and PHP. This knownledge of both Client and Server side scripting language allows me to create stunning designs that come with a CMS(Content Management System) to allow clients to update and manage their own site.

If you would like to see how I can help you, I would love to work with you and see what we can accomplish.
Robert McIntosh

Robert McIntosh

Hello, I am a website developer and computer programmer from Erie, Pa. of the United States. I currently work for The School District of the City of Erie, Pa. and in my free time I am a website developer to help my local community. I am highly skilled in HTML/HTML5, CSS/CSS3, JavaScript, and PHP. This knownledge of both Client and Server side scripting language allows me to create stunning designs that come with a CMS(Content Management System) to allow clients to update and manage their own site. If you would like to see how I can help you, I would love to work with you and see what we can accomplish.

  • zabi

    Nice Work, Thanks a lot

  • http://css3.bradshawenterprises.com/demos/speed.php

    According to him, CSS3 a better way to animate and transition than jQuery, because it uses less resources.

    • Not quite sure the point of you comment no offense, but I would love to comment on that. CSS3 is a more web efficient way to animate and transition within a page the jQuery. It is naturally support by the browser making it something that does not be to loaded externally. So it is a much better then jQuery. CSS3 however has one major downfall to jQuery, it does not support IE6, IE7, IE8, and IE9 does not support all CSS3 feature.

      • Jaco

        Hi,

        Thanks so much for this tutorial. I do have a question though. What can I do to atleast display the images in IE6 – 9. I don’t care if the image does not fade in IE, but I do need them to display.

    • Rich

      Yep, transitions and animations work better than jquery at the moment, making this Robert’s technique a good one. For things like this, it’s ideal, as if in older browsers there isn’t a fade, it doesn’t really matter.

  • Tom

    I’ve found that this works well in current versions of Chrome, Safari, Firefox, even WebOS, but does not work in IE or Opera. So, I there are techniques for hiding css properties from IE, and there are ways to detect the user agent and serve the appropriate code. Any suggestion for the best way to go to address this issue with these browsers?

    • This should work in Opera, but I screwed up and didn’t included any of the Opera identifiers… you see where it says “-webkit-” and “-moz-” if you were to add a “-o-” version of each of those it should work in Opera just fine. You would also need to add an Opera version of the keyframes “@-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }”

      As for the IE. this wont work without JavaScript. IE is well, just behind the curve, Microsoft and all of their infinite wisdom constantly decides to not support a lot of newer CSS code. Why you may ask? That is a question that I think well forever haunt a website developer, I believe Microsoft just wants us to work really really really hard trying to support there browsers.

      I am sorry that I can’t really help you with the IE problem, but I can update this to included Opera.. just check back tomorrow and this well be updated for Opera. I can see if I can try to find some way to get IE to play nice, but it probably wont happen. sorry.

      • This feature doesn’t work at all in Opera <12. Images doesn't show up at all. How can I fix this for Opera <12?

    • if you want to find out which browser the user is on you can use https://github.com/rafaelp/css_browser_selector

      alternatively you could use modernizr http://modernizr.com/ to figure out what is supported by the browser

      • Personally I choose to use something like

        < !DOCTYPE html>
        <!–[if lt IE 7 ]><html class=”ie ie6″ lang=”en”> < ![endif]–>
        <!–[if IE 7 ]></html><html class=”ie ie7″ lang=”en”> < ![endif]–>
        <!–[if IE 8 ]></html><html class=”ie ie8″ lang=”en”> < ![endif]–>
        <!–[if (gte IE 9)|!(IE)]><!–></html><html lang=”en”> <!–<![endif]–>
        <head>

        because most browsers don’t need special class except certain version of IE. This is a wonderful and non-JavaScript method for IE detection.

        I try to avoid JavaScript lately… Only use it to mostly to fix IE problems. I generally even put my JavaScript in IE conditional statements, no reason to load JavaScript if you are only using it in certain versions of IE, right?

  • Tom

    The update for Opera works, thanks Robert!

    Re: Rich said “…in older browsers there isn’t a fade, it doesn’t really matter.”

    It’s not just that IE doesn’t provide the fade, it doesn’t show the image at all and there is an empty place on the page. So, it’s necessary to disable the effect for IE users.

    I’m using this in the of my page:

    <!–[if !IE]> <!–>
    This does the trick. All browsers except IE display the fade. IE just displays the image.

  • Konrad

    Didn’t work in Opera 11.60. Had to update to version 12, and now it works. Thanks πŸ™‚

    • Sorry, I always forget to say that I only check the status of the most updated versions of browsers. Glad you noticed that and updated. If you come across any other problems feel free to let me know and Ill look into correcting them.

  • Graham Irwin

    Great script. Unfortunately IE9 doesn’t show the images at all. Earlier versions show the image without fading it which is okay but displaying the page without the image is a bit of a bummer.

    • Sorry about that, give me a day to fix this… world be faster but you got me on a really busy week.

  • prue

    Hello!

    What great code – thanks so much fore making it available!

    I’m a bit of a CSS newbie, do you know if there’s a way to automatically set all images on a site to be in a certain class? I’d like them all to fade it (at the same rate is fine), without having to add the html markup for each image.

  • Renato

    Nice tutorial. Thank you very much. I applied this effect to text and was very good

    I just have a problem. In the android browser, the text disappears shortly after appearing. I added one more style in CSS (fadeIn-loaded) and assigns an opacity = 1. Now the text appears, then disappears and appears after fade effect and is being displayed. I just need to make the text not appear initially. Can you help me?

    Thanks

    • I never tried this on text, but if you could give me a link to see what is happening or send me a zipped file of what you are working on so I can look at the source (email:rem.mcintosh(at)gmail.com) I would be more than happy to see what solutions I can come up with.

  • Achim

    This looks promising. But the HTML Markup is gone and the demo does not work.

    • WOW, I am so sorry. I updated my domain name. I completely forgot to come back here and re-point those to my new name. They have been fixed. Thank you for pointing this out.

      • Anna

        I still can’t see the HTML markup.. ?

        Great work btw!

  • John

    This is really nice.. I does work on anything you want to point it at i.e. a anything

    does anyone have a suggestion for a option for Explorer? jD

    • You can always program a jQuery fallback… This should work on IE though just without the Animation. If you want to look into a jQuery Fallback you can always contact me at rmcintosh(a)remabledesigns.com and Ill be happy to work with you on making an IE friendly version.

  • marvin

    thanks

  • toke

    This is really cool.

    Is it possible to make a transitions-effect on a div, so on page load it will move from one position to another. Just like this site: http://1508.dk/projekter

    • You are looking for this: http://isotope.metafizzy.co/
      The projekter-page also uses it.

      If you only need one animation onLoad you can write it yourself by simply changing the positions during the different keyframes.

  • Elancore

    That is super cool, thank a lot.

  • I’m designing a new site for a small company and I used this fade in feature here – http://www.coolpenguin.co.uk/dc/ (this is the test area).

    Did anyone solve, or tell me how to make this compatible with ie9?

    Will it work in 10?

    • Anna

      It does work in IE10.
      Nice work!

  • Drazen

    Great tutorial. Thank you so much. I love CSS and I knew it was possible πŸ™‚
    Best regards πŸ™‚

  • Miguel

    Great code, it works great. Thank you very much. Could you please tell me how to fade the pictures out again? I would like to fade them in and then out. Thank you again

  • Dirkjan

    Love this piece of code. Javascript/jQuery failed in my site, but this is working perfectly! Development site is on: http://www.webcollega.nl/afmi/

  • Daniel

    Thank you for the styles.

    Did you find a fix for Android browser? I’m using images which load using the fade-in then disappear after loading. It would be kind of nice if they would stay visible.

  • Lawrence Black

    My div I applied this to fades in then dissappears .. ?

    • Philipp Daub

      Same here – any ideas…?

  • Have you been able to get the images to display in IE9 yet Robert? IE6-8 display the images without fading (as expected) but IE9 doesn’t display the images at all.

  • Llama Barbaroussa

    Hey, thank you for this tutorial πŸ™‚

  • This is no good and I’ll tell you why: You have no idea when the image has loaded since it is an async event. Hence you’re very likely fading in too early ruining the smoothness – or alternatively letting the user wait to see an image that has already loaded. You’ll need to detect when the image has loaded and then apply a little JavaScript. This can’t be done without JavaScript I’m afraid. http://chrisrich.io

    • I’m assuming you didn’t try this as I just did and it works fine. It worked for the image, though I applied the transitions to the body instead, which works fine.

  • Thanks for this. It works fine for me. Really appreciate it.