Animations

Syntax
Overview

Learn to write animations in CSS3

Tutorials#


Animations are specified using the animation CSS properties and CSS @keyframes.

In this tutorial:


Animation CSS Properties

#

You can see on the right the typlical values.


Animation Keyframes

Keyframes are the animation instructions.

To set multiple points at which an element should undergo a transition, use the @keyframes rule.
The @keyframes rule includes the animation name, any animation breakpoints, and the properties intended to be animated.

Percentage values are used to indicate the point at which the values of a keyframe should be achieved. The “from” keyword references 0% and the “to” keyword references 100%. Functionally the following is identical:

@keyframes example {
    0% { bottom: -90px; right: -40px; }
    100% { bottom: -90px; right: -40px; }
}

@keyframes example {
    from { bottom: -90px; right: -40px; }
    to { bottom: -90px; right: -40px; }
}        
Example:

The keyframes block defined below will change background of the element continiously.

Note that in order the change from 100% to 0% will be smooth, we have to activate the animation in alternate direction, or to give to 0 and 100% the same declarations as is done below.

Look at me
I am .demo1 class
@keyframes change-bg {
    0%,
    100% {
        background: lightseagreen;
        color: white;
    }

    33.3% {
        background: blue;
        color: orange;
    }
    66.6% {
        background: red;
    }
}
.demo1 {
    animation: change-bg 10s ease-in-out 2s;
    animation-iteration-count: infinite;
}

Multiple Animation

You can comma-separate the values to declare multiple animations on a selector as well. In the example below, we want to change the color of the ellipse in a @keyframe while also nudging it from side to side with another.

Example:

In addition to background change, moving animation is applied to the same element.

Look at me
I am .demo2 class

@keyframes move {
    0%,
    100% {
        transform: translate(0, 0);
    }
    25% {
        transform: translate(300px, 0);
    }
    50% {
        transform: translate(300px, -50px);
    }
    75% {
        transform: translate(0px, -50px);
    }
}

.demo2 {
    animation: change-bg 8s ease infinite, move 10s linear infinite;
}

Animation-fill-mode

animation-fill-mode specifies if the animation styles are visible before or after the animation plays.

By default, the animation will not effect the styles of the element before the animation begins (if there is an animation-delay) or after the animation is finished. The animation-fill-mode property can override this behavior with the following possible values:

The importance of the values backwards and both, come in handy in complex animations that are controlled by scripting or user input. For more see Understanding the CSS animation-fill-mode Property.

Refresh the page to see the animation below.

Look at me
I am .demo3 class
Look at me
I am .demo4 class
@keyframes scale {
    0% {
        transform: scale(1);
    }
    100% {
        transform: scale(1.2);
    }
}

.demo3 {
    animation: scale 2s linear 1;
}

.demo4 {
    animation: scale 2s linear 1  forwards;
}