Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Keyframe animation: Hiding element before animation starts

Is there a CSS way to hide an element and fading it in with a keyframe-animation after x seconds? Or do I need JavaScript/jQuery for that? How to do it?

At the moment, it is also show before the animation starts and if I set opacity: 0;, the animation jumps back to that value after the animation is complete.

Fiddle

like image 293
Sven Avatar asked Mar 14 '13 08:03

Sven


People also ask

How do you add an animation-delay in CSS?

The CSS animation-delay property has the following syntax: animation-delay: [time] | initial | inherit; As you can see, there are three possible values: time, initial, and inherit. The first option is [time], which is the number of seconds or milliseconds before the animation starts.

What is animation-fill-mode backwards?

backwards. When assigned animation-fill-mode: backwards, the target element will take the style values set by the first keyframe of the animation during the animation-delay period. animation-delay sets a time delay until an animation begins, and animation-fill-mode applies during this delay.

Which of the CSS property is used to define a delay before an animation starts?

The animation-delay CSS property specifies the amount of time to wait from applying the animation to an element before beginning to perform the animation.


1 Answers

You are close. An easy way is to just add animation-fill-mode: forwards; which will persist the last keyframe and in this case keep the div visible.

Here's an updated version of your Fiddle where the animation starts after 4s (as you specified) and keeps the div visible instead of jumping back to it's original state (opacity: 0;).

Hope that helps!

like image 99
Christofer Vilander Avatar answered Sep 18 '22 11:09

Christofer Vilander