We’ve just seen how CSS transitions are just a way to animate CSS properties between a starting state, and an end state.
SO CSS transitions are specific kind of animations, where:
- there’s only 2 states: start and end
- the animation doesn’t loop
- the intermediate states are only controlled by the timing function
Well what if you want:
- to have control over the intermediate states?
- to make an animation loop?
- different animations on the same element?
- to animate a specific property only halfway through the animation?
- to simulate different timing functions for different properties?
CSS animations allow all of this, and more.
CSS animations are like mini movies where you are the director giving out instructions (CSS rules) to your actors (HTML elements) for different scenes (keyframes).
Animation properties
Like the transition
property, animation
is a shorthand property for several others:
- name: the animation’s name
- duration: how long the transition lasts
- timing-function: how the intermediate states are calculated
- delay: to start the animation after a certain amount of time
- iteration-count: how many times the animation should be performed
- direction: if the animation should be reversed or not
- fill-mode: what styles are applied before the animation starts and after it ends
Quick example
To animate a loading button, you could write a bouncing animation:
You first need to write the actual bouncing animation using keyframes and naming it bouncing
. Then you can use that animation by applying it to .loading-button
.
I used the shorthand animation
property, and included all options:
- name: bouncing (matches the keyframes name)
- duration: 0.5s (half a second)
- timing-function: cubic-bezier(0.1,0.25,0.1,1)
- delay: 0s (no delay)
- iteration-count: infinite (plays indefinitely)
- direction: alternate (goes back and forth)
- fill-mode: both
@keyframes
Before applying animations to HTML elements, you need to write animations using keyframes. Basically, keyframes are each intermediate step in an animation. They are defined using percentages:
0%
is the first step of the animation50%
is the step halfway through the animation100%
is the last step
You can also use the keywords from
and to
instead of 0%
and 100%
respectively.
You can define as many keyframes as you want, like 33%
, 4%
or even 29.86%
. In practice, you’ll only write a few.
Each keyframe is CSS rule, meaning that you can write CSS properties just like usual.
To define an animation, just write the keyword @keyframes
followed by its name:
Hello
Notice how the start 0%
and the end 100%
have the same CSS rules. This ensures that the animation loops perfectly. Because the iteration count is set to infinite
, the animation will go from 0%
to 100%
, and then back to 0%
instantly and indefinitely.
animation-name
The animation’s name is used at least twice:
- when writing the animation using
@keyframes
- when using the animation using the
animation-name
property (or withing theanimation
shorthand)
Like CSS class names, animation names can only include:
- letters (a-z)
- numbers (0-9)
- underscores (_)
- dashes (-)
It can not start with a number or two dashes.
animation-duration
Just like transition durations, animation durations can be set in seconds 1s
or milliseconds 200ms
.
It defaults to 0s
, which means no animation at all.
animation-timing-function
Just like transition timing functions, animation timing functions can use keywords like linear
, ease-out
, or be defined using custom cubic bezier functions.
It defaults to ease
.
Because CSS animations use keyframes, you can set a linear timing function and simulate an specific cubic bezier curve by defining a lot of very specific keyframes. Check out Bounce.js to generate advanced animations.
animation-delay
Just like transition delays, animation delays can be set in seconds 1s
or milliseconds 200ms
.
It defaults to 0s
, which means no delay at all.
It’s useful when triggering multiple animations in sequence.
animation-iteration-count
By default, animations are only played once (value of 1
). You can set 3 types of values:
- integers like
2
or3
- non-integers like
0.5
which will play only half the animation - the keyword
infinite
which will repeat the animation indefinitely
animation-direction
The animation’s direction defines in which order the keyframes are read.
- normal: starts at
0%
, ends at100%
, starts at0%
again - reverse: starts at
100%
, ends at0%
, starts at100%
again - alternate: starts at
0%
, goes to100%
, goes to0%
- alternate-reverse: starts at
100%
, goes to0%
, goes to100%
It’s easier to visualise if the animation’s iteration count is set to infinite
.
Normal: 0% --> 100%
Reverse: 100% --> 0%
Alternate: 0% <--> 100%
Alternate reverse: 100% <--> 0%
animation-fill-mode
An animation’s fill mode defines what happens before the animation starts and after it ends.
When you define keyframes you define CSS rules to be applied at different step of the animation. Now, these CSS rules can clash with the ones already applied on the elements being animated.
The fill mode allows to tell the browser if the animation’s styles should also be applied outside of the animation.
Let’s imagine a button that is:
- red by default
- turns green at the start of the animation
- ends up blue when the animation is over
animation-fill-mode |
Before the animation | Start of the animation | End of the animation | After the animation |
---|---|---|---|---|
none |
Default | Start | End | Default |
forwards |
Default | Start | End | End |
backwards |
Start | Start | End | Default |
both |
Start | Start | End | End |
1. Before the animation 2. During the animation 3. After the animation
None: the animation styles do not affect the default style
Default red From blue to green Back to red again
Forwards: the last styles applied at the end of the animation are retained afterwards
Default red From blue to green Remains green
Backwards: the animation's styles will already be applied before the animation actually starts
Already blue From blue to green Back to red again
Both: the styles are applied before and after the animation plays
Already blue From blue to green Remains green