CSS Animations

CSS allows animation of HTML elements without using JavaScript!

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 specify some keyframes for the animation.

Keyframes hold what styles the element will have at certain times.


CSS animation-name and animation-duration

The animation-name property specifies a name for the animation.

The animation-duration property defines how long an animation should take to complete. If this property is not specified, no animation will occur, because the default value is 0s (0 seconds).


CSS @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 “myAnimation” animation to the <div> element. The animation will last for 4 seconds, and it will gradually change the background-color of the <div> element from “red” to “yellow”:

Example

Result

More Example

Result

Property Values

Value Description
animation-name Specifies the name of the keyframe you want to bind to the selector
animation-duration Specifies how many seconds or milliseconds an animation takes to complete
animation-timing-function Specifies the speed curve of the animation
animation-delay Specifies a delay before the animation will start
animation-iteration-count Specifies how many times an animation should be played
animation-direction Specifies whether or not the animation should play in reverse on alternate cycles
animation-fill-mode Specifies what values are applied by the animation outside the time it is executing
animation-play-state Specifies whether the animation is running or paused
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

CSS animation-name Property

Definition and Usage

The animation-name property specifies a name for the @keyframes animation.

Example

Result

Property Values

Value Description
keyframename Specifies the name of the keyframe you want to bind to the selector
none Default value. Specifies that there will be no animation (can be used to override animations coming from the cascade)
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

CSS animation-duration Property

Definition and Usage

The animation-duration property defines how long an animation should take to complete one cycle.

Example

Result

Property Values

Value Description Demo
time Specifies the length of time an animation should take to complete one cycle. This can be specified in seconds or milliseconds. Default value is 0, which means that no animation will occur
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

CSS animation-iteration-count Property

Definition and Usage

The animation-iteration-count property specifies the number of times an animation should be played.

Example

Result

Play the animation two times:

Property Values

Value Description Demo
number A number that defines how many times an animation should be played. Default value is 1
infinite Specifies that the animation should be played infinite times (for ever)
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

 

CSS animation-direction Property

Definition and Usage

The animation-direction property defines whether an animation should be played forward, backward or in alternate cycles.

Example

Result

 

Property Values

Value Description Demo
normal Default value. The animation is played as normal (forwards)
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
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

CSS animation-fill-mode Property

Definition and Usage

The animation-fill-mode property specifies a style for the element when the animation is not playing (before it starts, after it ends, or both).

CSS animations do not affect the element before the first keyframe is played or after the last keyframe is played. The animation-fill-mode property can override this behavior.

Example

Result

Property Values

Value Description
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
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit