CSS | JavaScript | JQuery | Tutorial
How to add a webpage fade-in effect using JQuery
THE PROBLEM: HTML pages that have numerous and complex nested <div> tags and CSS can sometimes have a jumbled “assembling” appearance that happens as the page is loading and the various elements are being rendered by the browser. The appearance of this process can look quite unrefined and detract from professional appearance of the site.
A SOLUTION: JQuery includes a function to fade-in a hidden tag. If the content of the page is initially hidden using CSS, it then can be faded-in using JQuery once the whole page has been processed and is ready for display.
Step 1
Inside the page’s <body> tag, enclose the content in a <div> tag with a unique CSS id (see this post for the difference between CSS ids and classes).
<body> <div id="content-wrapper"> The page content goes here! </div><!-- content-wrapper --> </body>
Step 2
Inside the page’s <head> tag, import the JQuery library (download it here) and create a fade-in effect on the <div> tag surrounding the page’s content. Key to this solution is that the effect gets placed inside JQuery’s $(document).ready() function, ensuring the fade begins after the document’s DOM has been completely parsed.
<script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { // fade in content. $( '#content-wrapper' ).fadeIn("slow"); }); </script>
Step 3
At this point the fade-in effect will not occur, because the content is not hidden to begin with. To correct this add some CSS styling underneath the JavaScript above to hide the page content initially.
<style type="text/css"> #content-wrapper { display:none; } </style>
Step 4
Now the fade-in effect will work, however, a problem exists. If someone visits the page and has JavaScript turned off in their browser, the CSS to hide the content will be applied, but the JQuery to fade it back into visibility will not run, leaving the page effectively blank. This is a serious accessibility issue. To correct this, instead of hardcoding the <div> tag that is wrapping the content, write the tag using JavaScript. That way if JavaScript is turned off, the tag will not be rendered and won’t hide its containing content. Change what was written in Step 1 to the following:
<body> <script type="text/javascript"> <!--//--><![CDATA[//><!-- document.write( '<div id="content-wrapper">' ); //--><!]]> </script> The page content goes here! <script type="text/javascript"> <!--//--><![CDATA[//><!-- document.write( '</div><!-- content-wrapper -->' ); //--><!]]> </script> </body>
Putting it all together you end up with this (remember to include the JQuery library in the same folder as this HTML page):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Fade-in effect example</title> <script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { // fade in content. $( '#content-wrapper' ).fadeIn("slow"); }); </script> <style type="text/css"> #content-wrapper { display:none; } </style> </head> <body> <script type="text/javascript"> <!--//--><![CDATA[//><!-- document.write( '<div id="content-wrapper">' ); //--><!]]> </script> The page content goes here! <script type="text/javascript"> <!--//--><![CDATA[//><!-- document.write( '</div><!-- content-wrapper -->' ); //--><!]]> </script> </body> </html>

February 13th, 2010 at 10:32 pm
Works great! Thanks for the tutorial!
February 17th, 2010 at 5:26 am
Is this the same effect that mashable been using. Notice that their content fade into view as i scroll further down.
February 17th, 2010 at 6:20 am
From my brief look at the source, it looks like they are not using JQuery, but it could be hidden somewhere.
February 23rd, 2010 at 10:44 pm
Hi,
Excellent tutorial, got it to work on a webpage i am trying to get a contract for and i know nothing of jquery or even much about java…….One thing though… is it possible to get it to fade in over the old page..if you look at my example above…if you click on the left link..the screen goes white before fading in the new page…i need it to just fade in….or cross fade…. how would i accomplish this?
as you see from the example…every page is built like this…each page has a full screen picture behind it..and they want seamless…but i dont want to do it in flash…..
Thanks for your excellent tutorial again and look forward to hearing from you….
Cheers
Paul
February 24th, 2010 at 2:33 am
Hi Paul!
Thanks for your comment and the kind words. What you need to do is have another div around the “content-wrapper” div and move your background image to this outer div. As it is now, the thing that is getting faded—the content-wrapper div—also has your background image inside of it, so as it fades in, so too does your background image.
March 1st, 2010 at 11:08 pm
Mashable is using Jquery too!
Use this Firefox addon to see what JScript framework’s being used https://addons.mozilla.org/en-US/firefox/addon/10083
March 1st, 2010 at 11:20 pm
Hi Nem, thanks for the link and correction!
March 9th, 2010 at 4:23 pm
Hi – this is just what I was looking for and all works well in Firefox and Chrome but can’t get it to work in Internet Explorer 8. I’ve used the ‘content-wrapper’ div around a section of page content – not the entire page. Thanks.
Dave.
March 9th, 2010 at 4:27 pm
Hi Dave,
What does it do?
March 10th, 2010 at 2:24 am
Hi Dave,
It appears to not like
position:absoluteCSS attributes being set inside a div that is being faded. Possibly this is a JQuery issue? At any rate keep to the default positioning, you should be able to layout your page without using absolute positioning.Cheers!
March 18th, 2010 at 3:40 pm
Great tut!
Ans — Make sure you set a high z-index for your absolute layers that fade. That fixes the unexpected fade problem.
March 18th, 2010 at 4:04 pm
For step 4, you can also do as below:
Insert this in HEAD:
This way you don’t have to write the in javascript.
Cheers,
DrDiv
March 18th, 2010 at 4:04 pm
Sorry, my code didn’t appear right:
March 18th, 2010 at 4:06 pm
Could you please wrap my code with a noscript tag. That is what I meant to write but this wsywig isn’t pulling it right.
Thanks,
DrDiv
March 18th, 2010 at 5:52 pm
Hi DrDiv,
Thanks for the tips (also fixed your code as requested)
March 18th, 2010 at 5:57 pm
DrDiv,
I’m confused on your step 4 tweak? Is the first or second post what you meant to write? I haven’t tried it yet, but wouldn’t block display show the content initially?
March 22nd, 2010 at 10:11 am
HI Ans– Sorry about the previous attempts of trying to write script into this wsywig. I removed the tag brackets. Here you go, this goes into the body:
When you put this internal style in noscript tag, your browser reads in and overrides the external style which is set to display:none.
I find this way safer and cleaner. My 2 cents.
Let me know if this still doesn’t make sense.
DrDiv
March 22nd, 2010 at 10:12 am
Another error. I meant to say, this code goes into the “HEAD”. Please feel free to delete all my posts except the last one. I’m sorry for adding all these posts.
March 26th, 2010 at 9:10 am
Oh! Yes, I understand, and I realize I formatted your other comment incorrectly from what you were asking. Hmm… noscript tag, good idea. I like it!
April 1st, 2010 at 5:46 pm
Hi Ans,
you were helping me earlier with the fade in effect outside of microsoft ie…
my question is this…is there a way to reverse the fade in..ie, click on a button..have the page fade out…this would make for a smoother non-ie fade for my client btween pages?
Thanks
Paul
April 1st, 2010 at 5:49 pm
(con’t from previous post)
without having to do the cookie thing we were discussing…..
i really wish though that there was a simple bit of code that would do this over every browser:)
Thanls
Paul
April 3rd, 2010 at 1:49 am
Hey Paul,
You can have a “callback” function that runs when a JQuery function has finished, which redirects the page. For example:
I’d imagine the link that was clicked could be picked out too so that the redirect wouldn’t be hardcoded.
April 3rd, 2010 at 12:13 pm
Hey Paul,
Check out this page: http://docs.jquery.com/How_jQuery_Works and see the section on links. You could have something like:
In your HEAD section of your HTML:
April 8th, 2010 at 3:32 am
great! Thanks for the tutorial!
April 14th, 2010 at 4:07 am
Hi!
Instead of hiding the content with display: none; you can hide it using jQuery .hide() which results in the content not being hidden when the user haven’t got javascript activated.
April 14th, 2010 at 7:25 am
Hi Max,
Good tip! And it keeps it all within the realm of JQuery.
May 11th, 2010 at 6:13 am
Best tutorial i could find for this jquery fade effect !! Thanks a lot
Works perfect
June 8th, 2010 at 3:01 am
I’ve problem with IE and elements that use css position: relative and position:absolute declaration, and I can’t find a way to fix it…
I’ve tried z-index changes, no effect…
I’m using slideshow and I cant escape relative and absolute positioning.
Is there a way to run this?
December 15th, 2010 at 6:52 am
Lovely effect. Will implement it on my taxi transfers related website. It gives that extra touch we just needed. Thank you Ans for making an effort to put it in writing. If any trouble with implementing I will post here. Rgds, Slawek
January 12th, 2011 at 3:05 pm
How can I make the “slow” fade effect to go slower ???
January 12th, 2011 at 4:50 pm
Hi Doron,
You can use a numerical value instead of the text ‘slow’ that will correspond to the number of milliseconds the fade should occur over. See the last example on http://api.jquery.com/fadeTo/ (it’s a different effect, but the same syntax)
January 25th, 2011 at 4:04 am
Thanks for the tutorial! I can use with my website so easy .
February 26th, 2011 at 12:53 pm
Hi, awesome effect. Love it.
One question though, is there anyway I can make it pause for a few seconds before executing the effect?
Cheers
February 26th, 2011 at 3:58 pm
Hi King,
Sure… see the example on http://api.jquery.com/delay/
You should be able to have:
$( '#content-wrapper' ).delay(1000).fadeIn("slow");Also, see this answer on stackoverflow: http://stackoverflow.com/questions/251204/delay-jquery-effects
February 26th, 2011 at 4:05 pm
Thankyou so much
February 27th, 2011 at 7:51 pm
This seems to work great in FF, Chrome, and Safari but I am having a heck of a time with it in IE-8. It just seems to immediately display the new page with no fade in effect whatsoever. Did I miss a fix for this in the threads above?
February 28th, 2011 at 12:15 pm
You should use
$(“body”).hide().fadeIn(500);
What happen if javascript is disable, nothing on the screen…..
You should use JS for both hidding and fadding.
February 28th, 2011 at 5:33 pm
Hi JF,
The provided example does not hide the content when JavaScript is disabled, the div tags that are hidden are written in the document using JavaScript, so if JavaScript is turned off the tags won’t be written and the document won’t be hidden. Hiding the body tag means no background can be rendered on which the content then fades in upon. However, if the desired effect is to fade in from a white screen (as opposed to a background pattern), I believe your method may do the trick.
February 28th, 2011 at 5:35 pm
Hi TB,
Someone else had an issue with this further up in the comments and it had to do with their CSS using
position:absolute. Are you using this perhaps?March 27th, 2011 at 12:48 pm
Hi Anselm,
Thanks so much for the tutorial. I’ve added the fade in effect on all the pages of my website and it works great when testing it in Dreamweaver. However, when I loaded the pages to the server and tested them there, I discovered that the pages with images don’t necessarily fade in at first——they still load with the jumbled “assembling” appearance. Once loaded, if I reclick the link, the fade in effect works fine. The pages with just text, fade in immediately, so I’m assuming the problem is I have to somehow preload the images. I already preload the rollover images, so I added the non rollover images to the preload, but that didn’t seem to change anything. I understand you are not a help center and doing this only out of the goodness of your heart, but I was wondering if you could take a look at my site and hopefully point me in the right direction for a solution? http://www.joeleih.com/test Pages with the fade in problem are index, commercials, films, and hype.
Thanks so much,
Joe
March 27th, 2011 at 3:34 pm
Hi Joe,
Yeah, I would say the problem looks like what you have surmised. You’re preloading the images, but place that code before the jQuery code for fading in the page. So instead of having the preload code in the body onload event, place it like so:
$(document).ready(function()
{
// preload images
MM_preloadImages('images/nav-contact-up.gif','images/nav-links-up.gif','images/nav-hype-up.gif','images/nav-news-up.gif','images/nav-bio-up.gif','images/nav-films-up.gif','images/nav-comm-up.gif');
// fade in content.
$( '#content-wrapper' ).fadeIn("slow");
});
March 28th, 2011 at 11:38 am
Hi Ans,
Thanks so much, I tried the above on my index page but unfortunately it still doesn’t seem to be working.
http://joeleih.com/test/indextest.html
Do you think the problem might be with the fact that my background images are in my CSS, homecontent and nav?
thanks,
Joe
[style removed]
May 24th, 2011 at 6:05 am
I tried using this in wordpress, but the posts pages come up empty (tags were placed in header and footer around a div wrap). Any Ideas?
May 24th, 2011 at 8:03 am
Check the page using Firebug (http://getfirebug.com) or other web developer plugin that allows you to edit the page and see if you can adjust the output HTML and/or CSS to see what the problem may be.
June 1st, 2011 at 9:00 pm
Hi, many thanks, realy great work! But it does not wok with IE8. It means the loading is between 5 and 8 seconds and than apears the side. Maybe you can help me?
June 2nd, 2011 at 12:24 am
Hi Kathrin,
Perhaps you are using absolute positioning? See earlier comment about that.
September 2nd, 2011 at 5:24 pm
Thanks for the tut, really simple and gives a great look. Not so static, how can I add easing? Thanks!
October 16th, 2011 at 6:39 am
Thanks for this. This is the only possible way I have found so far to have all the elements hidden before the page is actually loading while having a solution for users who have JS toggles off.
To show an example of how one could also try and target “JS on” – users only:
$(document).ready(function() {
$(“body”).css(“display”, “none”);
$(“body”).fadeIn(2000);
});
Problem with this is that the “display:none” only comes up after the page was initially loaded (thus showing a flicker of the full page before going blank and fading in again).
So thanks alot for this solution
Will used it over here: http://www.belgianhiphop.be/ for some extra smooth browsing
October 16th, 2011 at 11:20 am
Re: CiNiTriQs
You could use
$(“body”).css(“display”, “none”);
$(document).ready(function() {
$(“body”).fadeIn(2000);
});
instead and you shouldn’t have the flickering.
October 24th, 2011 at 2:40 pm
@ Max Malm
I have tried that one before, but it still seems to load the page first and for a split second I still see the whole page before it vanishes and comes back again.
I know this would look like a cleaner solution but in the end it’s the smoothness that counts… until I find better solutions (or new tech) I will use the previous method.