Animations in CSS can add a dynamic touch to any website, making it more engaging and interactive for users.
However, sometimes you may want to delay the animation from starting immediately upon page load.
In this CSS tutorial, we’ll walk through how to delay an animation in CSS using the animation-delay
property.
First, let’s define the animation using the animation
property.
This property allows you to specify the name of the animation, the duration, and any other animation properties such as animation-iteration-count
and animation-timing-function
.
Here’s an example of defining an animation called “fade-in” that lasts for 1 second and repeats infinitely:
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } } .fade-in-element { animation: fade-in 1s infinite; }
Now that we have our animation defined, we can use the animation-delay
property to delay the start of the animation.
The value for animation-delay
is specified in seconds (or milliseconds if you prefer).
Here’s an example of delaying the “fade-in” animation by 2 seconds:
fade-in-element { animation: fade-in 1s infinite; animation-delay: 2s; }
It’s important to note that the animation-delay
property only delays the start of the animation, not the duration of the animation itself.
In the example above, the animation will still last for 1 second, but it will start 2 seconds after the page has loaded.
You can also use the animation-delay
property in conjunction with other animation properties, such as animation-direction
and animation-fill-mode
.
For example, you can set the animation to alternate direction every time it repeats and hold the final animation state:
fade-in-element { animation: fade-in 1s infinite; animation-delay: 2s; animation-direction: alternate; animation-fill-mode: forwards; }
In conclusion, delaying an animation in CSS is a simple task that can be achieved using the animation-delay
property.
By specifying the delay in seconds, you can control when your animations start and add an extra layer of interactivity to your website.
With this tutorial, you’ll be able to create animations that are timed perfectly with your website’s content and user interactions.