CSS Animations – How to Animate Transitions between CSS Keyframes
CSS animations provide a smooth and gradual way to animate an element’s style changes from one keyframe to another.
Components of CSS Animations
CSS animations consist of two components:
- Keyframes
- Animation properties
What Are CSS @keyframes
?
@keyframes define the styles you want browsers to apply smoothly to an element at some specified key moments (frames).
Syntax of CSS @keyframes
A CSS @keyframes
at-rule consists of the following:
- An
@keyframes
keyword - The
@keyframes
’s name - A block of zero or more keyframes
- The animation’s key moment selector
- The key moment’s style declarations
Here’s an illustration:
Examples of CSS @keyframes
Below are examples of the CSS @keyframes
.
How to define change-color
’s keyframes
Here’s what we did in the snippet above:
- We created the
@keyframes
at-rule namedchange-color
. - We defined a keyframe for browsers to apply when an associated element’s animation is at its zero percent (
0%
) duration. - We defined a keyframe for browsers to apply when an associated element’s animation is at its one hundred percent (
100%
) duration.
Note:
- You can name your
@keyframes
anything you wish. 0%
is equivalent to the keywordfrom
. And100%
is the same as the keywordto
. In other words, the snippet above is equal to the following:
- An animation’s start and end states (
from
andto
) are optional. - Suppose you omit an
@keyframes
’s start (0%
) or end (100%
) state. In that case, browsers will 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.
Let’s see another example.
How to define shape-image
keyframes
Here’s what we did in the snippet above:
- We created the
@keyframes
ruleset namedshape-image
. - We defined four keyframes for browsers to apply when an associated element’s animation is at the specified key moments.
So, now that we know the CSS @keyframes
ruleset, we can discuss the other component of CSS animations—animation properties.
What Are CSS Animation Properties?
CSS animation properties inform browsers about the animation you wish to apply to a specific element.
In other words, the CSS animation properties describe the animation’s attributes, such as its name, duration, direction, and iteration.
The nine (9) types of CSS animation properties are:
animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
animation-play-state
animation-fill-mode
animation
Let’s discuss each one now.
What Is the CSS animation-name
Property?
The CSS animation-name
property defines the name of the @keyframes
at-rules containing the styles you wish to apply to a specific element.
Here’s an example:
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - We created
change-color
’s@keyframes
ruleset. - We defined two keyframes for browsers to use when the
div
element’s animation is at its zero percent (0%
) and one hundred percent (100%
) duration.
What Is the CSS animation-duration
Property?
The CSS animation-duration
property defines the time to complete one animation cycle.
Note the following:
- The
animation-duration
property must be in milliseconds (ms
) or seconds (s
) units. animation-duration
’s value must be zero or positive. Otherwise, browsers will ignore the duration declaration.- Zero seconds (
0s
) isanimation-duration
’s default value. - Suppose
0s
isanimation-duration
’s value. In that case, browsers will still execute the animation by firing theanimationStart
andanimationEnd
events. But theanimation-fill-mode
’s value will determine whether the animation is visible. For instance, if you set theanimation-fill-mode
tonone
, the animation will be invisible.
Let’s see some examples of the animation-duration
property.
Design in Figma, launch in Webflow
How to animate an element’s color change within three seconds
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to three seconds (3s
). - We created
change-color
’s @keyframes ruleset. - 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.
How to animate an image’s border and width changes within seven seconds
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to theimg
element. - The
animation-duration
property sets the animation’s runtime for one cycle to seven seconds (7s
). - We created
shape-image
’s@keyframes
ruleset. - 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.
Design in Figma, launch in Webflow
What Is the CSS animation-timing-function
Property?
The CSS animation-timing-function
property defines an animation’s implementation timing (speed) throughout its duration.
In other words, the animation-timing-function
property specifies the speed for implementing the animation at various intervals of its duration.
The values the animation-timing-function
property accepts are:
ease
: Starts the animation slowly. Then speeds it up and ends it slowly.ease
is theanimation-timing-function
property’s default value. It is equivalent tocubic-bezier(0.25, 0.1, 0.25, 1)
.ease-in
: Starts the animation slowly with a gradual increase in speed. It is equivalent tocubic-bezier(0.42, 0, 1, 1)
.ease-out
: Start fast and end the animation slowly. It is equivalent tocubic-bezier(0, 0, 0.58, 1)
.ease-in-out
: Start and end the animation slowly. It is equivalent tocubic-bezier(0.42, 0, 0.58, 1)
.linear
: Start and end the animation using a consistent speed throughout the animation’s duration. It is equivalent tocubic-bezier(0, 0, 1, 1)
.cubic-bezier(p1, p2, p3, p4)
: Allows you to define the values of the cubic-Bezier curve.
Let’s see some examples of the animation-timing-function
property.
How to animate an element’s width change using a linear speed
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to seven seconds (7s
). - The
linear
timing function applied a consistent speed to thediv
’s animation. - We created
change-width
’s@keyframes
ruleset. - 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.
Let’s see another example.
How to animate an element’s width change using an ease-in-out and a linear speed
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to seven seconds (7s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thefirst-div
’s animation. - The
linear
timing function applied a consistent speed to thesecond-div
’s animation. - We created
change-width
’s@keyframes
ruleset. - We defined two keyframes for browsers to apply when the
div
elements’ animations are at 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.
Design and develop at the same time
What Is the CSS animation-delay
Property?
The CSS animation-delay
property defines how long browsers should wait before starting an animation.
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:
- The
animation-delay
property must be in milliseconds (ms
) or seconds (s
) units. 0s
isanimation-delay
’s default value. It causes the animation to start once browsers apply 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.
Let’s see some examples of the animation-delay
property.
How to animate an element’s width change with a four-second delay
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
elements. - The
animation-duration
property sets the animation’s runtime for one cycle to seven seconds (7s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thefirst-div
’s animation. - The
linear
timing function applied a consistent speed to thesecond-div
’s animation. - The
animation-delay
property applied a four-second (4s
) delay to the starting time of thesecond-div
’s animation. - We created
change-width
’s @keyframes ruleset. - We defined two keyframes for browsers to apply when the
div
elements’ animations are at 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.
Below is another example of the animation-delay
property.
How to animate an element’s width change from the fourth-second mark of the animation sequence
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
elements. - The
animation-duration
property sets the animation’s runtime for one cycle to seven seconds (7s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thefirst-div
’s animation. - The
linear
timing function applied a consistent speed to thesecond-div
’s animation. - The
animation-delay
property makes thesecond-div
’s animation start from the fourth-second mark of the animation sequence. - We created
change-width
’s @keyframes ruleset. - We defined two keyframes for browsers to apply when the
div
elements’ animations are at 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.
Want tech support right now?
What Is the CSS animation-iteration-count
Property?
The CSS animation-iteration-count
property defines the number of times browsers should repeat an animation.
Note the following:
1
isanimation-iteration-count
’s default value.- The
animation-iteration-count
property accepts non-integer values—for instance,0.5
tells browsers to play half of a single animation cycle. animation-iteration-count
does not accept negative values.- An
infinite
value means browsers should repeat the animation forever.
Below are some examples.
How to animate an element’s width change with a two-cycle iteration
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to five seconds (5s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thediv
’s animation. - The
animation-iteration-count
property tells browsers to run the animation twice. - We created
change-width
’s @keyframes ruleset. - 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.
How to animate an element’s width change with an infinite iteration
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to five seconds (5s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thediv
’s animation. - The
animation-iteration-count
property tells browsers to run the animation infinitely. - We created
change-width
’s @keyframes ruleset. - We defined two keyframes for browsers to apply when the
div
element’s animation is at its zero percent (0%
) and one hundred percent (100%
) duration.
Therefore, browsers will run the div
’s animation infinitely.
Want tech support right now?
What Is the CSS animation-direction
Property?
The CSS animation-direction
property specifies whether the animation’s first iteration should run forward or backward. It also defines whether browsers should alternate the direction of subsequent iterations.
The values animation-direction
accepts are:
normal
: Play the animation in the normal direction (that is, forward).normal
isanimation-direction
’s default value.reverse
: Play the animation in the reverse direction (backward).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.
Below are some examples.
How to animate an element’s width change while starting each animation cycle backward
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to five seconds (5s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thediv
’s animation. - The
animation-iteration-count
property tells browsers to run the animation infinitely. - The
animation-direction
property starts each animation cycle in reverse (backward). - We created
change-width
’s @keyframes ruleset. - 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.
Below is another example of the animation-direction
property.
How to animate an element’s width change while alternating the animation’s direction
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to five seconds (5s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thediv
’s animation. - The
animation-iteration-count
property tells browsers to run the animation infinitely. - The
animation-direction
property alternates the direction of each cycle’s animation. - We created
change-width
’s @keyframes ruleset. - 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.
Want tech support right now?
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.
The values the animation-play-state
property accepts are:
running
: Specifies that the browser is running the animation.running
isanimation-play-state
’s default value.paused
: Specifies that the browser has paused the animation.
Here’s an example:
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to five seconds (5s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thediv
’s animation. - The
animation-iteration-count
property tells browsers to run the animation infinitely. - The
animation-direction
property alternates the direction of each cycle’s animation. - We used the
animation-play-state
on thediv
’s hover pseudo-class to pause the animation whenever users move their mouse over thediv
element. - We created
change-width
’s@keyframes
ruleset. - 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-fill-mode
Property?
The CSS animation-fill-mode
property defines the styles browsers should apply to an element before (or after) its animation runs.
The values the animation-fill-mode
property accepts are:
none
: Browsers will apply no style to the element before or after the animation runs.none
isanimation-fill-mode
’s default value.forwards
: The element will retain the last keyframe’s style declarations when the animation ends. (Note: Theanimation-direction
andanimation-iteration-count
determines the last keyframe.)backwards
: The element will retain the first keyframe’s style declarations during theanimation-delay
period. (Note: Theanimation-direction
determines the first keyframe.)both
: Browsers will apply both the forwards and backwards rules. Therefore, the animation properties will extend in both directions.
Below are some examples.
How to style an element after its animation ends
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to five seconds (5s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thediv
’s animation. - The
animation-fill-mode
property tells browsers to retain the last keyframe’s style declarations when the animation ends. - We created
change-width
’s @keyframes ruleset. - 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.
Design and develop at the same time
Below is another example of the animation-fill-mode
property.
How to style an element during its animation delay period
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to five seconds (5s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thediv
’s animation. - The
animation-fill-mode
property tells browsers to retain the first keyframe’s style declarations during theanimation-delay
period. - We created
change-width
’s @keyframes ruleset. - 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.
Let’s see a third example of the animation-fill-mode
property.
How to style an element during its animation delay and after the animation
Here’s what we did in the snippet above:
- The
animation-name
property specifies the@keyframes
we wish to apply to thediv
element. - The
animation-duration
property sets the animation’s runtime for one cycle to five seconds (5s
). - We used the
ease-in-out
timing function to apply a slow start and slow end speed to thediv
’s animation. - The
animation-fill-mode
property tells browsers to apply both the forwards and backwards rules. - We created
change-width
’s @keyframes ruleset. - 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.
Use Flexbox like a pro
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
In other words, instead of writing:
You can alternatively use the animation
property to shorten your code like so:
Here is the animation
property’s syntax:
Note the following:
- The way you arrange the time values is essential. Browsers read the first time-value as
animation-duration
. And they assign the second one toanimation-delay
. - It is best to list
animation-name
last. Otherwise, browsers may assign theanimation-name
’s value to other properties. - CSS animations are expensive for most CSS properties—except
opacity
andtransform
. In other words, applying animations on any CSS box model property is inherently a CPU-intensive task. Therefore, if you are concerned about your page’s performance, use animations on onlyopacity
andtransform
properties. - You can apply multiple
@keyframes
rulesets to an element using theanimation
property. Here’s an example:
The snippet above applied three @keyframes
rulesets to the div
element using commas (,
) to separate each @keyframes
’s configurations.
Now that you know the animation properties, we can discuss the differences between CSS animations and CSS transitions.
CSS Animations vs. CSS Transitions: What’s the Difference?
Below are five key differences between CSS transitions and CSS animations.
1. What is the purpose of CSS transitions and animations?
CSS transitions create smooth transitions from one CSS value to another.
CSS animations animate the style change from one CSS keyframe to another.
2. Do CSS transitions and animations need triggers?
You need triggers to run CSS transitions.
For instance, you can use the :hover
pseudo-class to run transitions when a user’s pointer hovers over an element.
Alternatively, you can trigger transition effects using JavaScript to add or remove a class.
On the other hand, CSS animations do not need triggers. CSS animations run automatically by default. You can optionally use triggers to make it run only when an element is in a specific state.
3. How many states do CSS transitions and animations have?
CSS transitions have only two states: an initial and a final state. You cannot create intermediate steps.
CSS animations allow you to create multiple states.
4. Can CSS transitions and animations run multiple times?
CSS transitions run only once. But you can run multiple CSS animations iterations—even to infinity.
5. What is the recommended use of CSS transitions and animations?
CSS transitions are best used for basic style changes, while CSS animations are suitable for dynamic style changes.