- Infinite Animation in CSS — How it Works + Loop Example
- Full example of infinite animation
- Using keyframes
- Compatibility of animation among browsers
- CSS Animations
- Browser Support for Animations
- What are CSS Animations?
- The @keyframes Rule
- Example
- Example
- Example
- Delay an Animation
- Example
- Example
- Set How Many Times an Animation Should Run
- Example
- Example
- Run Animation in Reverse Direction or Alternate Cycles
- Example
- Example
- Example
- Specify the Speed Curve of the Animation
- Example
- Specify the fill-mode For an Animation
- Example
- Example
- Example
- Animation Shorthand Property
- Example
- Example
- CSS Animation Properties
Infinite Animation in CSS — How it Works + Loop Example
Animations, used to the right extent, can enhance a good experience on your website. Today the best and only universally accepted technology for this is CSS. Follow me and you will learn how to work with loop animations.
To create infinite animations in CSS, we will use the property animation-iteration-count: infinite;.
If you apply this rule to an existing animation, it is done. But for more details, I bring other examples and resources, check out below:
Full example of infinite animation
Below is the complete code for an example of a loading animation. This animation can be applied to images, divs or any other HTML element other than default display: inline element, like texts or links.
.loading < background: #094CFA; width: 60px; height: 60px; margin: auto; border-radius: 4px; /* Here we declare our inline animation */ animation: rotate 3s linear infinite; >/* Here we define our animation with the name "rotate" */ @keyframes rotate < from < transform: rotate(0deg); >to < transform: rotate(359deg); >> The above script will have the same animation effect shown below. I just added some visual effects. If you want to copy, just click on the CSS tab below:
In the CSS loading class, we apply some styles and finally the animation.
To understand what happens, take a look at the animation property and @keyframes rotate.
In animation, we defined the animation in the shorthand method, in only one line, but we could also write it using 4 lines, in the long form:
I always prefer to use the shorthand way of writing, because it doesn’t matter the order in which you define the values of the properties, and it’s much less stuff to memorize.
Using keyframes
Keyframes, as the name implies, are frames that we define with specific times from beginning to end of an animation, to represent it.
If you want to animate an object falling to the ground, for example, you need to define the initial position of the object in the air, and the final position, on the ground. That’s basically what we did with the example animation in this article.
As we can see above, the duration of the animations is not defined within @keyframes, but we use percentages to generally define the time proportion between one keyframe and another. Alternatively, we can use from and to to define the frames 0% and 100% respectively. See the example below to understand it better:
Compatibility of animation among browsers
I worked with Flash for a long time, and a couple of years ago it was the only thing that existed to support animations on the web. Fortunately, for a few years this scenario has changed and CSS animations are the current standard.
According to the site caniuse.com, over 96.5% of users use browsers fully compatible with CSS animations, and as this number tends to increase more and more. In my opinion, there’s nothing to worry about in terms of compatibility.
Using prefixes -moz-animation, -webkit-animation, etc. has also become somewhat archaic. Still according to caniuse, over 94% of users use browsers that no longer require prefixes for animation. I didn’t use prefixes in the examples in order not to prolong the text nor to support something that has been losing importance, but this is up to you. For some other CSS properties, however, it is mandatory to use prefixes.
CSS Animations
CSS allows animation of HTML elements without using JavaScript or Flash!
In this chapter you will learn about the following properties:
- @keyframes
- animation-name
- animation-duration
- animation-delay
- animation-iteration-count
- animation-direction
- animation-timing-function
- animation-fill-mode
- animation
Browser Support for Animations
The numbers in the table specify the first browser version that fully supports the property.
Property | |||||
---|---|---|---|---|---|
@keyframes | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-name | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-duration | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-delay | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-iteration-count | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-direction | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-timing-function | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-fill-mode | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
What are CSS Animations?
An animation lets an element gradually change from one style to another.
You can change as many CSS properties you want, as many times as you want.
To use CSS animation, you must first specify some keyframes for the animation.
Keyframes hold what styles the element will have at certain times.
The @keyframes Rule
When you specify CSS styles inside the @keyframes rule, the animation will gradually change from the current style to the new style at certain times.
To get an animation to work, you must bind the animation to an element.
The following example binds the «example» animation to the element. The animation will last for 4 seconds, and it will gradually change the background-color of the element from «red» to «yellow»:
Example
/* The element to apply the animation to */
div width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
>
Note: The animation-duration property defines how long an animation should take to complete. If the animation-duration property is not specified, no animation will occur, because the default value is 0s (0 seconds).
In the example above we have specified when the style will change by using the keywords «from» and «to» (which represents 0% (start) and 100% (complete)).
It is also possible to use percent. By using percent, you can add as many style changes as you like.
The following example will change the background-color of the element when the animation is 25% complete, 50% complete, and again when the animation is 100% complete:
Example
/* The element to apply the animation to */
div width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
>
The following example will change both the background-color and the position of the element when the animation is 25% complete, 50% complete, and again when the animation is 100% complete:
Example
/* The element to apply the animation to */
div width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
>
Delay an Animation
The animation-delay property specifies a delay for the start of an animation.
The following example has a 2 seconds delay before starting the animation:
Example
div <
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-delay: 2s;
>
Negative values are also allowed. If using negative values, the animation will start as if it had already been playing for N seconds.
In the following example, the animation will start as if it had already been playing for 2 seconds:
Example
div <
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-delay: -2s;
>
Set How Many Times an Animation Should Run
The animation-iteration-count property specifies the number of times an animation should run.
The following example will run the animation 3 times before it stops:
Example
div <
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 3;
>
The following example uses the value «infinite» to make the animation continue for ever:
Example
div <
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: infinite;
>
Run Animation in Reverse Direction or Alternate Cycles
The animation-direction property specifies whether an animation should be played forwards, backwards or in alternate cycles.
The animation-direction property can have the following values:
- normal — The animation is played as normal (forwards). This is default
- reverse — The animation is played in reverse direction (backwards)
- alternate — The animation is played forwards first, then backwards
- alternate-reverse — The animation is played backwards first, then forwards
The following example will run the animation in reverse direction (backwards):
Example
div <
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-direction: reverse;
>
The following example uses the value «alternate» to make the animation run forwards first, then backwards:
Example
div <
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 2;
animation-direction: alternate;
>
The following example uses the value «alternate-reverse» to make the animation run backwards first, then forwards:
Example
div <
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 2;
animation-direction: alternate-reverse;
>
Specify the Speed Curve of the Animation
The animation-timing-function property specifies the speed curve of the animation.
The animation-timing-function property can have the following values:
- ease — Specifies an animation with a slow start, then fast, then end slowly (this is default)
- linear — Specifies an animation with the same speed from start to end
- ease-in — Specifies an animation with a slow start
- ease-out — Specifies an animation with a slow end
- ease-in-out — Specifies an animation with a slow start and end
- cubic-bezier(n,n,n,n) — Lets you define your own values in a cubic-bezier function
The following example shows some of the different speed curves that can be used:
Example
Specify the fill-mode For an Animation
CSS animations do not affect an element before the first keyframe is played or after the last keyframe is played. The animation-fill-mode property can override this behavior.
The animation-fill-mode property specifies a style for the target element when the animation is not playing (before it starts, after it ends, or both).
The animation-fill-mode property can have the following values:
- none — Default value. Animation will not apply any styles to the element before or after it is executing
- forwards — The element will retain the style values that is set by the last keyframe (depends on animation-direction and animation-iteration-count)
- backwards — The element will get the style values that is set by the first keyframe (depends on animation-direction), and retain this during the animation-delay period
- both — The animation will follow the rules for both forwards and backwards, extending the animation properties in both directions
The following example lets the element retain the style values from the last keyframe when the animation ends:
Example
div <
width: 100px;
height: 100px;
background: red;
position: relative;
animation-name: example;
animation-duration: 3s;
animation-fill-mode: forwards;
>
The following example lets the element get the style values set by the first keyframe before the animation starts (during the animation-delay period):
Example
div <
width: 100px;
height: 100px;
background: red;
position: relative;
animation-name: example;
animation-duration: 3s;
animation-delay: 2s;
animation-fill-mode: backwards;
>
The following example lets the element get the style values set by the first keyframe before the animation starts, and retain the style values from the last keyframe when the animation ends:
Example
div <
width: 100px;
height: 100px;
background: red;
position: relative;
animation-name: example;
animation-duration: 3s;
animation-delay: 2s;
animation-fill-mode: both;
>
Animation Shorthand Property
The example below uses six of the animation properties:
Example
div <
animation-name: example;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
>
The same animation effect as above can be achieved by using the shorthand animation property:
Example
CSS Animation Properties
The following table lists the @keyframes rule and all the CSS animation properties:
Property | Description |
---|---|
@keyframes | Specifies the animation code |
animation | A shorthand property for setting all the animation properties |
animation-delay | Specifies a delay for the start of an animation |
animation-direction | Specifies whether an animation should be played forwards, backwards or in alternate cycles |
animation-duration | Specifies how long time an animation should take to complete one cycle |
animation-fill-mode | Specifies a style for the element when the animation is not playing (before it starts, after it ends, or both) |
animation-iteration-count | Specifies the number of times an animation should be played |
animation-name | Specifies the name of the @keyframes animation |
animation-play-state | Specifies whether the animation is running or paused |
animation-timing-function | Specifies the speed curve of the animation |