Documente Academic
Documente Profesional
Documente Cultură
Home
Using CSS for a fade-in effect on page load Ask Question
PUBLIC Asked 7 years, 2 months ago Active 19 days ago Viewed 1.1m times
Stack Overflow
Can CSS transitions be used to allow a text paragraph to fade-in on page load?
Tags Blog
I really like how it looked on http://dotmailapp.com/ and would love to use a similar effect using CSS.
Users 404 The domain has since been purchased and no longer has the effect mentioned. An archived copy can Iterating on Inclusion
Jobs be viewed on the Wayback Machine.
C++ Creator Bjarne Stroustrup Answers
Illustration Our Top Five C++ Questions
TEAMS What’s this?
share improve this question edited Sep 25 at 17:20 asked Jul 26 '12 at 23:36
Peter Mortensen user1556266
14.5k 19 89 118 2,023 3 10 5
6 You may want to read this: bavotasan.com/2011/a-simple-fade-with-css3 – Edwin Bautista Jul 26 '12 at 23:40
829 If you are looking for a self-invoking transition then you should use CSS 3 Animations. They aren't Free Lunch and snacks
supported either, but this is exactly the kind of thing they were made for.
Work with state-of-the-art technology and
equipment
CSS
+ 5 more benefits
text-align: center;
+50 Learn more
-webkit-animation: fadein 2s; /* Safari, Chrome and Opera > 12.1 */
-moz-animation: fadein 2s; /* Firefox < 16 */
-ms-animation: fadein 2s; /* Internet Explorer */
-o-animation: fadein 2s; /* Opera < 12.1 */
animation: fadein 2s;
}
71 people chatting
Method 2: Related
Alternatively, you can use jQuery (or plain JavaScript; see the third code block) to change the class 3185 Set cellpadding and cellspacing in CSS?
on load:
2192 How do I give text or an image a
jQuery transparent background using CSS?
Plain JavaScript (not in the demo) Which (if any) is the "most major" airport / field /
strip on Antartica?
This method is the most cross-compatible as the target browser does not need to support CSS 3
transitions or animations.
share improve this answer edited Sep 25 at 17:16 answered Jul 27 '12 at 4:10
Peter Mortensen A.M.K
14.5k 19 89 118 13.4k 3 26 58
138 CSS3 Animation is supported just fine by every modern browser around. Of course, IE is not a modern
browser. – Rob Jul 27 '12 at 13:20
6 Yes, but what if you want/need to be backwards compatible to IE6? In that case I think that jQuery is the best
option. But, the asker wants it in CSS so I posted it as an alternative. – A.M.K Jul 27 '12 at 13:22
In addition (not tested) you should be able to use the transition property without jQuery to achieve the same
effect. – Rob Jul 27 '12 at 13:22
4 Isn't it best to set the initial opacity to 0 in javascript? That way if the user has javascript disabled the element
is just there instead of never appearing. – Jonathan. Mar 20 '14 at 21:29
2 @A.M.K I tried doing "fix" that just in the javascript but didn't manage to, so in the end I created a separate
css file with opacity: 1 !important; and put in a <noscript> element. – Jonathan. Mar 22 '14 at
11:40
You can use the onload="" HTML attribute and use JavaScript to adjust the opacity style of your
element.
19 Leave your CSS as you proposed. Edit your HTML code to:
<body onload="document.getElementById(test).style.opacity='1'">
<div id="test">
<p>This is a test</p>
</div>
</body>
This also works to fade-in the complete page when finished loading:
HTML:
<body onload="document.body.style.opacity='1'">
</body>
CSS:
body{
opacity: 0;
transition: opacity 2s;
-webkit-transition: opacity 2s; /* Safari */
}
Check the W3Schools website: transitions and an article for changing styles with JavaScript.
share improve this answer edited Sep 25 at 17:20 answered Apr 24 '13 at 9:36
Peter Mortensen Ned
14.5k 19 89 118 251 2 5
Great answer. Alternatively I guess you can set the onload directly on the element. Like <div id="test"
onload="this.style.opacity='1'"> . Not sure if it means the event is triggered before the entire body is
loaded though. – Jeppe Jul 5 '18 at 6:55
add a comment
In response to @A.M.K's question about how to do transitions without jQuery. A very simple example I
threw together. If I had time to think this through some more, I might be able to eliminate the
5 JavaScript code altogether:
<style>
body {
background-color: red;
transition: background-color 2s ease-in;
}
</style>
<script>
window.onload = function() {
document.body.style.backgroundColor = '#00f';
}
</script>
<body>
<p>test</p>
</body>
share improve this answer edited Sep 25 at 17:18 answered Jul 27 '12 at 13:38
Peter Mortensen Rob
14.5k 19 89 118 12k 8 33 54
add a comment
Not the answer you're looking for? Browse other questions tagged css css-transitions opacity or ask
your own question.
STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE Blog Facebook Twitter LinkedIn
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Life / Arts
Developer Jobs Directory Advertising Work Here
Culture / Recreation
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Other
Mobile Contact Us site design / logo © 2019 Stack Exchange Inc; user contributions
licensed under cc by-sa 4.0 with attribution required.
Disable Responsiveness rev 2019.10.14.35179