You might think transitions and animations refer to the same thing, but they don’t. The CSS Transition can not change CSS properties which CSS Animation can do.
CSS Transitions allow changing property values very smoothly, over a given duration. To create a transition effect, you must specify two things:
The CSS property you want to add an effect to
The duration of the effect
Note: If the duration is not specified, the transition will not occur (default 0).
Example:
Hover over the div element (square) below, to see the transition effect (<div> element is changing the width from 100px to 200px).
Most often you can see transitions used on hover states, or when information on a page is added or removed. The hover states may be a subtle change in font color and information on the page may fade from invisible to visible.
CSS Animations are a more powerful alternative to transitions. Rather than rely on a change from one beginning state to an end state.
Where a transition goes only from A to B, the animation can go from A, B, C to D. Or any number of stages as needed. (To see the basics of CSS Animations click here.)
Animation is a great way to make your web page more attractive. CSS animation propertyallows the animation of HTML elements without using JavaScript.
What is a CSS Animation?
An animation lets an element gradually change from one style to another one. You can change as many CSS properties you want, as many times you want. To use CSS animation, you must first specify some keyframes for the animation.
The main component of CSS Animation is @keyframes – the CSS rule where animation is created. Inside @keyframes, you can define the stages and settings of the animation.
Note: The animation will finish in 4 seconds. It will gradually change the background color of the <div> element from “green” to “yellow”. After the animation, the square changes back to its original color.
Output:
Click on the square to repeat the animation.
The animation-durationproperty defines how long time an animation should take to complete. If the animation-duration property is not specified, no animation will occur, because the default value is 0 seconds.
The following example will change the background and the position of the <div> element when the animation is 50% complete, and again when the animation is 100% complete:
The animation-iteration-count property specifies the number of times an animation should run. Using the value “infinite” you will make the animation continue forever.
The animation-direction property specifies whether the animation should be played forward, backward, or in alternate cycles.
Demo:
The animation-direction property can have the following values:
normal (default) – the animation is played as normal (forward). reverse – the animation is played in the reverse direction (backward). alternate – the animation is played in forward first, then backward. alternate-reverse – the animation is played backward first, then forward.
The animation-timing-function property can have the following values:
ease – specifies an animation with a slow start, then fast, then and slowly. This is the default.
linear – specifies an animation with the same speed from start to end.
ease-in – specifies an animation with a slow start.
ease-in-out – specifies an animation with a slow start and end.
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.
The animation-fill-mode property can have the following values:
none – the default one. The animation will not apply any styles to the element before or after it is executing.
forwards– the element will retain the style values that are set by the last keyframe.
backwards– the element will get the style values that are set by the first keyframe.
both – the animation will follow the rules for both forwards and backwards, extending the animation properties in both directions.
Example(square will move 200px to the left and will stay in this position):