CSS Transition Vs Animation Handbook
CSS Transition Vs Animation Handbook
So, instead of allowing browsers to change a property's value immediately, CSS transitions
cause the change to happen smoothly over a specified period of time.
For instance, suppose you wish to change an element's size on hover. In that case, you
have two options:
2. Use CSS transitions to transition smoothly from the element's initial size to its new state.
width: 40%;
}
img:hover {
width: 100%;
}
Try Editing It
The code above instantaneously changes the image's size from its initial width ( 40% ) to its
new dimension ( 100% ) because we did not use CSS transitions.
With CSS transitions, you will get a much more pleasing experience. Let's see an example
below.
width: 40%;
transition: width 3s ease-out 0.4s;
}
img:hover {
width: 100%;
}
Try Editing It
The transitionproperty applied a smooth and gradual transition from width: 40% to width:
100% on the image.
transition-property
transition-duration
The transition-duration property only accepts time in milliseconds (ms) or seconds (s).
width: 40%;
transition-property: width;
transition-duration: 3s;
}
img:hover {
width: 100%;
}
Try Editing It
1. The transition-property tells browsers to transition the img 's width from its initial value
( 40% ) to its new state ( 100% ).
2. We used the transition-duration property to define three seconds ( 3s ) duration from the
start to the end of the transition.
Therefore, instead of an instantaneous change from the image's initial width ( 40% ) to its
new size ( 100% ), browsers will transition smoothly between the old and new state in three
seconds ( 3s ).
p {
font-size: 1rem;
transition-property: font-size;
transition-duration: 5s;
}
p:hover {
font-size: 7rem;
}
Try Editing It
Therefore, instead of an immediate change from the paragraph's initial font size ( 1rem ) to
its new size ( 7rem ), browsers will transition smoothly between the old and new state in five
seconds ( 5s ).
transition-timing-function
transition-delay
In other words, the transition-timing-function specifies the speed for implementing the
transition at various intervals of its duration.
bezier(0.25,0.1,0.25,1) .
ease-in : Starts the transition slowly with a gradual increase in speed. It is equal to cubic-
bezier(0.42,0,1,1) .
ease-out : Starts fast and ends the transition slowly. It is equivalent to cubic-
bezier(0,0,0.58,1) .
bezier(0.42,0,0.58,1) .
linear : Starts and ends the transition using a consistent speed throughout the transition's
duration. It is equivalent to cubic-bezier(0,0,1,1) .
cubic-bezier(p1, p2, p3, p4) : Allows you to define the values of the cubic-bezier curve.
0s is the transition-delay 's default value. It causes the transition to start immediately
from the moment browsers apply it to an HTML element.
A negative value causes the transition to begin immediately from the specified time. For
instance, suppose an element's transition-delay value is -3s . In that case, the transition
will start immediately at 3 seconds.
A positive value causes the transition to begin after the specified delay time has elapsed.
For instance, suppose an element's transition-delay value is 3s . In that case, the
transition will start after a 3-second delay.
img {
width: 40%;
transition-property: width;
transition-duration: 3s;
transition-timing-function: ease-out;
}
img:hover {
width: 100%;
}
Try Editing It
2. We used the transition-duration property to define three seconds ( 3s ) duration from the
start to the end of the transition.
3. We used the transition-timing-function property to begin the transition quickly and end i
slowly.
img {
width: 40%;
transition-property: width;
transition-duration: 3s;
transition-timing-function: linear;
}
img:hover {
width: 100%;
}
Try Editing It
2. We used the transition-duration property to define three seconds ( 3s ) duration from the
start to the end of the transition.
img {
width: 40%;
transition-property: width;
transition-duration: 3s;
transition-timing-function: linear;
transition-delay: 2s;
}
img:hover {
width: 100%;
}
Try Editing It
1. The transition-property informs browsers to transition the img element's width property
2. We used the transition-duration property to define three seconds ( 3s ) duration from the
start to the end of the transition.
Now that we know the CSS transition properties, we can discuss defining them with a
shorthand syntax.
img {
transition-property: width;
transition-duration: 3s;
transition-timing-function: linear;
transition-delay: 2s;
}
You can alternatively use the transition property to shorten your code like so:
img {
Try Editing It
Note that you can use the transition property to transition the state of multiple CSS
properties.
Here's an example:
img {
width: 40%;
opacity: 0.4;
transition: width 3s linear, opacity 4s ease-out, transform 5s;
}
img:hover {
width: 100%;
opacity: 1;
transform: rotate(45deg);
}
Try Editing It
The snippet above used commas ( , ) to separate each of the transitional properties we are
applying to the img element.
So, now that we know what CSS transitions are and how they work, we can discuss CSS
animations.
1. Keyframes
2. Animation properties
1. An @keyframes keyword
Here's an illustration:
A CSS @keyframes at-rule consists of a keyword, a name, and a block of keyframes
@keyframes change-color {
0% {background-color: purple;}
/* The last keyframe */
Note:
0% is equivalent to the keyword from . And 100% is the same as the keyword to . In other
words, the snippet above is equal to the following:
@keyframes change-color {
to {background-color: yellow;}
}
Suppose you omit an @keyframes ' start ( 0% ) or end ( 100% ) state. In that case, browsers w
default to the element's existing styles for either state.
The important rule ( !important ) does not work in keyframes. Browsers will ignore any
keyframe declaration with an !important rule.
2. We defined four keyframes for browsers to apply when an associated element's animation
is at the specified key moments.
Tip: Use the CSSKeyframesRule interface in JavaScript to access the CSS @keyframes at-
rules.
So, now that we know the CSS @keyframes ruleset, we can discuss the other component
of CSS animations—animation properties.
In other words, CSS animation properties describe the animation's attributes, such as its
name, duration, direction, and iteration.
animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
animation-play-state
animation-fill-mode
animation
Here's an example:
div {
width: 150px;
height: 150px;
animation-name: change-color;
}
@keyframes change-color {
animation-duration 's value must be zero or positive. Otherwise, browsers will ignore the
duration declaration.
Suppose 0s is animation-duration 's value. In that case, browsers will still execute the
animation by firing the animationStart and animationEnd events. But the animation-fill-
mode 's value will determine whether the animation is visible. For instance, if you set
the animation-fill-mode to none , the animation will be invisible.
div {
width: 150px;
height: 150px;
animation-name: change-color;
animation-duration: 3s;
}
@keyframes change-color {
from {background-color: purple;}
to {background-color: yellow;}
}
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to three
seconds ( 3s ).
4. We defined two keyframes for browsers to apply when the div element's animation is at
zero percent ( 0% ) and one hundred percent ( 100% ) duration.
Therefore, browsers will create a smooth three-second animation from change-color 's first
keyframe to its last.
img {
animation-name: shape-image;
animation-duration: 7s;
}
@keyframes shape-image {
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to seven
seconds ( 7s ).
4. We defined four keyframes for browsers to apply when the image's animation is at the
specified key moments.
Therefore, browsers will create a smooth seven-second animation from shape-image 's first
keyframe to its last.
In other words, the animation-timing-function property specifies the speed for implementing
the animation at various intervals of its duration.
ease : Starts the animation slowly. Then speeds it up and ends it slowly. ease is
the animation-timing-function property's default value. It is equivalent to cubic-
ease-in : Starts the animation slowly with a gradual increase in speed. It is equivalent
to cubic-bezier(0.42, 0, 1, 1) .
ease-out : Starts fast and ends the animation slowly. It is equivalent to cubic-bezier(0, 0,
0.58, 1) .
ease-in-out : Starts and ends the animation slowly. It is equivalent to cubic-bezier(0.42, 0
0.58, 1) .
linear : Starts and ends the animation using a consistent speed throughout the
animation's duration. It is equivalent to cubic-bezier(0, 0, 1, 1) .
cubic-bezier(p1, p2, p3, p4) : Allows you to define the values of the cubic-Bezier curve.
div {
width: 150px;
height: 150px;
background-color: purple;
animation-name: change-width;
animation-duration: 7s;
animation-timing-function: linear;
}
@keyframes change-width {
Try Editing It
3. The linear timing function applied a consistent speed to the div 's animation.
5. We defined two keyframes for browsers to apply when the div 's animation is at zero
percent ( 0% ) and one hundred percent ( 100% ) duration.
Therefore, browsers will create a smooth seven-second animation from change-width 's first
keyframe to its last.
div {
width: 150px;
height: 150px;
color: white;
animation-name: change-width;
animation-duration: 7s;
}
.first-div {
background-color: purple;
animation-timing-function: ease-in-out;
}
.second-div {
background-color: orange;
animation-timing-function: linear;
}
@keyframes change-width {
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to seven
seconds ( 7s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the first-div 's animation.
4. The linear timing function applied a consistent speed to the second-div 's animation.
6. We defined two keyframes for browsers to apply when the div elements' animations are
their zero percent ( 0% ) and one hundred percent ( 100% ) durations.
Therefore, browsers will create a smooth seven-second animation from change-width 's first
keyframe to its last.
In other words, use animation-delay to specify whether the animation should start
immediately from the beginning, immediately from a specific time, or later (after some
delay).
Note the following:
0s is animation-delay 's default value. It causes the animation to start once browsers app
it to an HTML element.
A negative value causes the animation to start immediately from the specified time. For
instance, suppose an element's animation-delay value is -3s . In that case, the animation
will begin immediately at 3 seconds.
A positive value causes the animation to start after the specified delay time has elapsed.
For instance, suppose an element's animation-delay value is 3s . In that case, the
animation will begin after a 3-second delay.
div {
width: 150px;
height: 150px;
color: white;
animation-name: change-width;
animation-duration: 7s;
}
.first-div {
background-color: purple;
animation-timing-function: ease-in-out;
}
.second-div {
background-color: orange;
animation-timing-function: linear;
animation-delay: 4s;
}
@keyframes change-width {
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to seven
seconds ( 7s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the first-div 's animation.
4. The linear timing function applied a consistent speed to the second-div 's animation.
7. We defined two keyframes for browsers to apply when the div elements' animations are
their zero percent ( 0% ) and one hundred percent ( 100% ) durations.
Therefore, browsers will delay the second-div 's animation for four seconds while starting
the first-div 's animation immediately.
div {
width: 150px;
height: 150px;
color: white;
animation-name: change-width;
animation-duration: 7s;
}
.first-div {
background-color: purple;
animation-timing-function: ease-in-out;
}
.second-div {
background-color: orange;
animation-timing-function: linear;
animation-delay: -4s;
}
@keyframes change-width {
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to seven
seconds ( 7s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the first-div 's animation.
4. The linear timing function applied a consistent speed to the second-div 's animation.
5. The animation-delay property makes the second-div 's animation start from the fourth-
second mark of the animation sequence.
7. We defined two keyframes for browsers to apply when the div elements' animations are
their zero percent ( 0% ) and one hundred percent ( 100% ) durations.
Therefore, browsers will start the second-div 's animation immediately at the fourth-second
mark.
width: 70px;
height: 50px;
background-color: purple;
animation-name: change-width;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-iteration-count: 2;
}
@keyframes change-width {
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to five second
( 5s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the div 's animation.
6. We defined two keyframes for browsers to apply when the div element's animation is at
zero percent ( 0% ) and one hundred percent ( 100% ) duration.
Therefore, browsers will run the div 's animation in two cycles.
Below is another example of the animation-iteration-count property.
div {
width: 70px;
height: 50px;
animation-name: change-width;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
}
@keyframes change-width {
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to five second
( 5s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the div 's animation.
normal : Play the animation in the normal direction (that is, forward). normal is animation-
direction 's default value.
alternate : Play the first animation cycle in the normal direction. Then, alternates the
subsequent iterations between the backward and forward directions.
alternate-reverse : Play the first animation cycle in the reverse direction. Then, alternates
the subsequent iterations between the forward and backward directions.
div {
width: 70px;
height: 50px;
animation-name: change-width;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-direction: reverse;
}
@keyframes change-width {
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to five second
( 5s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the div 's animation.
7. We defined two keyframes for browsers to apply when the div element's animation is at
zero percent ( 0% ) and one hundred percent ( 100% ) duration.
width: 70px;
height: 50px;
animation-name: change-width;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-direction: alternate;
}
@keyframes change-width {
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to five second
( 5s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the div 's animation.
7. We defined two keyframes for browsers to apply when the div element's animation is at
zero percent ( 0% ) and one hundred percent ( 100% ) duration.
What is the CSS animation-play-
state property?
The CSS animation-play-state property specifies whether the browser is running or has
paused a specific animation.
running : Specifies that the browser is running the animation. running is animation-play-
state 's default value.
Here's an example:
div {
width: 70px;
height: 50px;
animation-name: change-width;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-direction: alternate;
}
div:hover {
animation-play-state: paused;
}
@keyframes change-width {
2. The animation-duration property sets the animation's runtime for one cycle to five second
( 5s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the div 's animation.
6. We used the animation-play-state on the div 's hover pseudo-class to pause the
animation whenever users move their mouse over the div element.
8. We defined two keyframes for browsers to apply when the div element's animation is at
zero percent ( 0% ) and one hundred percent ( 100% ) duration.
none : Browsers will apply no style to the element before or after the animation
runs. none is animation-fill-mode 's default value.
forwards : The element will retain the last keyframe's style declarations when the animatio
ends. (Note: The animation-direction and animation-iteration-count properties determine
the last keyframe.)
backwards : The element will retain the first keyframe's style declarations during
the animation-delay period. (Note: The animation-direction property determines the first
keyframe.)
both : Browsers will apply both the forwards and backwards rules. Therefore, the animatio
properties will extend in both directions.
div {
width: 70px;
height: 50px;
background-color: green;
animation-name: change-width;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-fill-mode: forwards;
}
@keyframes change-width {
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to five second
( 5s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the div 's animation.
4. The animation-fill-mode property tells browsers to retain the last keyframe's style
declarations when the animation ends.
6. We defined two keyframes for browsers to apply when the div element's animation is at
zero percent ( 0% ) and one hundred percent ( 100% ) duration.
div {
width: 70px;
height: 50px;
background-color: green;
animation-name: change-width;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-delay: 3s;
animation-fill-mode: backwards;
}
@keyframes change-width {
Try Editing It
Here's what we did in the snippet above:
2. The animation-duration property sets the animation's runtime for one cycle to five second
( 5s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the div 's animation.
4. The animation-fill-mode property tells browsers to retain the first keyframe's style
declarations during the animation-delay period.
6. We defined two keyframes for browsers to apply when the div element's animation is at
zero percent ( 0% ) and one hundred percent ( 100% ) duration.
div {
width: 70px;
height: 50px;
background-color: green;
animation-name: change-width;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-delay: 3s;
animation-fill-mode: both;
}
@keyframes change-width {
from {width: 70px; background-color: purple;}
to {width: 100%; background-color: orange;}
}
Try Editing It
2. The animation-duration property sets the animation's runtime for one cycle to five second
( 5s ).
3. We used the ease-in-out timing function to apply a slow start and slow end speed to
the div 's animation.
4. The animation-fill-mode property tells browsers to apply both the forwards and backward
rules.
6. We defined two keyframes for browsers to use when the div element's animation is at
zero percent ( 0% ) and one hundred percent ( 100% ) duration.
What is the
CSS animation Property?
We use the animation property as a shorthand for:
animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
animation-play-state
animation-fill-mode
div {
animation-name: change-width;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-delay: 2s;
animation-iteration-count: 3;
animation-direction: alternate;
animation-play-state: running;
animation-fill-mode: both;
}
You can alternatively use the animation property to shorten your code like so:
div {
Try Editing It
The way you arrange the time values is essential. Browsers read the first time-value
as animation-duration . And they assign the second one to animation-delay .
It is best to list animation-name last. Otherwise, browsers may assign the animation-name 's
value to other properties.
You can apply multiple @keyframes rulesets to an element using the animation property.
Here's an example:
div {
width: 70px;
height: 70px;
background-color: green;
animation:
@keyframes change-width {
@keyframes change-shape {
@keyframes rotate-hue {
Try Editing It
The snippet above applied three @keyframes rulesets to the div element using commas
( , ) to separate each @keyframes ' configurations.
Note: We used the hue-rotate() function to rotate the div 's colors.
2. CSS transitions and animations are expensive operations for most CSS properties—
except opacity and transform . In other words, applying transitions (or animations) to any
CSS box model property is inherently a CPU-intensive task. Therefore, animate
only opacity , and transform properties if you are concerned about your page's
performance.
3. Be mindful of the layout repainting issues that CSS transitions may cause through your
elements' stacking order.
Wrapping up
In this article, we discuss the differences between CSS transitions and animations. We also
used examples to discuss how to use them.