CSS | JQuery | JavaScript | 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?