Can CSS Transitions be used to allow a text paragraph to fade-in on page load?
I really like how it looks on http://dotmailapp.com/ and would love to use a similar effect using CSS.
Note: The domain has since been purchased and no longer has the effect mentioned. An archived copy can be viewed on the Wayback Machine.
Illustration
Having this markup:
<div id="test">
<p>This is a test</p>
</div>
With the following CSS rule:
#test p {
opacity: 0;
margin-top: 25px;
font-size: 21px;
text-align: center;
-webkit-transition: opacity 2s ease-in;
-moz-transition: opacity 2s ease-in;
-o-transition: opacity 2s ease-in;
-ms-transition: opacity 2s ease-in;
transition: opacity 2s ease-in;
}
How can the transition be triggered on load?
Method 1:
If you are looking for a self-invoking transition then you should use CSS3 Animations, they aren't supported as well but this is exactly the kind of thing they were made for.
CSS
Demo
Browser Support
All modern browsers, IE 10+: http://caniuse.com/#feat=css-animation
Method 2:
Alternatively, you can use jQuery (or plain JS, see third code block) to change the class on load:
jQuery
CSS
Plain JS (not in demo)
Demo
Browser Support
All modern browsers, IE 10+: http://caniuse.com/#feat=css-transitions
Method 3:
Or, you can use the method that .Mail uses:
jQuery
CSS
Demo
Browser Support
jQuery 1.x: All modern browsers, IE 6+: http://jquery.com/browser-support/
jQuery 2.x: All modern browsers, IE 9+: http://jquery.com/browser-support/
This method is the most cross-compatible as the target browser does not need to support CSS3 transitions or animations.
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 javascript altogether:
You can use the
onload=""
HTML attribute and use JavaScript to adjust the opacity style of your element.Leave your CSS as you proposed. Edit your HTML code to:
This also works to fade-in the complete page when finished loading:
HTML:
CSS:
Check the W3Schools website: transitions and an article for changing styles with javascript.