Home >Web Front-end >CSS Tutorial >Can CSS Transitions or Animations Create a Fade-In Effect for Text on Page Load?
Question:
Can CSS transitions be leveraged to create a fade-in effect for a text paragraph as the page loads?
Explanation:
CSS transitions allow elements to smoothly transition from one style to another over a specified period. This property can be applied to the opacity property, enabling a fade-in effect.
For self-invoking transitions, CSS Animations are more suitable. They provide a dedicated animation syntax specifically designed for this purpose.
#test p { margin-top: 25px; font-size: 21px; text-align: center; -webkit-animation: fadein 2s; -moz-animation: fadein 2s; -ms-animation: fadein 2s; -o-animation: fadein 2s; animation: fadein 2s; } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
Browser Support:
Alternatively, CSS transitions can be triggered using a class toggle:
#test p { opacity: 0; font-size: 21px; margin-top: 25px; text-align: center; transition: opacity 2s ease-in; } #test p.load { opacity: 1; }
$("#test p").addClass("load");
Browser Support:
For cross-browser compatibility, jQuery can be employed to animate the opacity change:
$("#test p").delay(1000).animate({ opacity: 1 }, 700);
.Mail Method:
[DotmailApp](http://dotmailapp.com/) uses a similar method with a slight delay to enhance the effect. The technique, however, requires jQuery 2.x with broader browser support.
The above is the detailed content of Can CSS Transitions or Animations Create a Fade-In Effect for Text on Page Load?. For more information, please follow other related articles on the PHP Chinese website!