Sponsored

Not the solution you were looking for?

We have seen CSS3’s power in our previous articles using transitions, animations and transforms. Thanks to CSS3, with which we can create effects and animations without using heavy javascript. In this tutorial, we’ll see how to create an infinite looping using CSS3, without trigger. You can implement this technique to create an image slider, a gif or intro of your website.

Note: Older browsers do not support CSS transitions and keyframe animations. Certain browsers require vendor specific prefix to be added.

Basic structure of animation

The Animation is very simple: We will have 5 images and they follow a predefined path. All the images would be overlapped one above the other. As the time passes, the first image makes a way to second using the opacity and z-index property. Similarly the other images too.

Let’s see how the HTML markup would look like.

HTML Markup

Lets keep the HTML markup very simple. It’s organized and SEO-friendly. Let’s see the code and then figure out in detail.

With the above css code, we have created our slider and 5 image holders. Since each image holder holds different image, we shall code it differently along with the animation. Now we just need to add the animation using keyframes and the corresponding images in the background.

CSS3 Keyframes Animation and the images

In order to get the right kind of animation, we need to arrive at few parameters based on the calculation. Lets see what those calculations are:

Lets fix the total duration of an animation to be 25 seconds. Now we shall calculate, how many keyframes take 1 second. So, let’s work out the calculations:

1. No. of images in the slider: 5
2. The total duration of the animation : 25
3. Animation length of each image Should be : 25 / 5 = 5 seconds
4. To calculate the no. of keyframes equals one second:

Divide the total number of keyframes by the total duration of the animation.

Total Keyframes = 100 (0 to 100%)

100 keyframes/25 seconds = 4 keyframes
4 keyframes = 1 second.

We shall apply CSS animation to the slider. We will be able to put the animation on infinite loop because each image will follow its own animation that activates once it comes front in the slider.

Once the properties of the animation have been assigned, we need to use keyframes to set the animation in motion.

The keyframes can be better explained with the below timing diagram.

Consider the above timing diagram: Each image takes 1s to enter, stays for 3s and 1s to exit in total 5s.

On load, the first image will be shown till 16%, ie., 4secs

4keyframes = 1sec, so 16 keyframes = 16/4 = 4secs.

Next the first image goes to background with the transition of opacity and z-index in 1sec, making way to the second image. Second image starts entering at 20th frame and stays till 36th frame (3sec). From 40 to 44th keyframes, it starts fading to pave way for the third image. This goes in an infinite loop until with 5th image fading to pave way for the 1st image again!

Following the timing-diagram, we can connect the animations to each other making an infinite loop. For smoother transitions, opacity and z-index properties are been used while switching from one image to the next image.