CSS animations give you a second way to animate your elements. They’re like transitions, but they’re way more powerful.
How powerful?
You can create animations like this heartbeat here if you know it well. :)
Creating the animation
To create an animation, you need to write the animation with the @keyframes syntax. It looks like this:
@keyframes animation-name { 0% { margin-left: 0px; }
50% { margin-right: 400px; }
100% { margin-left: 0px; }}In the keyframes above, the 0%, 50% and 100% values are points on an animation timeline. It means the following:
- Start the animation (0%); set
margin-leftto0px - Start to middle of animation (50%); change
margin-leftto400px - Middle to end of animation (100%); change
margin-leftto0px
You can add any number of points on the @keyframes declaration. Each point should be a percentage value.
@keyframes animation-name { 0% { /* ... */ } 25% { /* ... */ } 50% { /* ... */ } 75% { /* ... */ } 100% { /* ... */ }}Using the animation
To use the animation, you use the animation property. Like transition, animation is a shorthand (but for 8 properties!).
.element { animation: name duration timing-function delay iteration-count direction fill-mode play-state;}The eight properties are:
animation-nameanimation-durationanimation-timing-functionanimation-delayanimation-iteration-countanimation-directionanimation-fill-modeanimation-play-state
.element { animation-name: name; animation-duration: duration; animation-timing-function: timing-function; animation-delay: duration; animation-iteration-count: count; animation-direction: direction; animation-fill-mode: fill-mode; animation-play-state: play-state;}You should already be familiar with four of them:
animation-nameis the name of the animation. It’s the same name you create with@keyframes.animation-durationis the duration of the animation. It is written in seconds with thessuffix, like3s.animation-timing-functionis the timing-function used for the animation. It the same astransition-timing-function.animation-delayis the duration to wait before the animation starts. This delay is also applied every time the animation repeats.
These four properties are slightly new:
animation-iteration-countdetermines how many times the animation should repeat. If you want the animation to loop infinitely (until you say stop), useinfinite.animation-directionis the direction of the animation. More onanimation-directionbelow.animation-fill-modetells CSS what to do when the animation ends. We won’t cover this property.animation-play-statedetermines whether the animation is playing or paused.
Animation-direction
animation-direction determines how the animation is played. You can set it to four values:
normalreversealternatealternate-reverse
Here’s the difference between each of them:
normal: starts at 0% and ends at 100%reverse: starts at 100% and ends at 0%alternate: Every odd iteration starts at 0% and ends at 100%. Every even iteration starts at 100% and ends at 0%alternate-reverse: Every odd iteration starts at 100% and ends at 0%; every even iteration starts at 0% and ends at 100%
See this CodePen for a demo: https://codepen.io/zellwk/pen/eGxBWP?editors=1100.
Animation-play-state
CSS plays animations immediately when the page loads. If you want to pause your animations, you need to set animation-play-state to paused.
/* Pause an animation */.element { animation-play-state: paused;}If you want to play the animation, you need to set animation-play-state to running.
/* Plays animation when a user hovers over the element */.element:hover { animation-play-state: running;}See this pen for a demo: https://codepen.io/zellwk/pen/WZPRva.
Note: pause your CSS animations when you don’t need them. This saves precious computing power!
Animating two or more properties.
You can use multiple animations at the same time. Just do the same as what you do with CSS transitions and you’ll be fine 🤓.
.selector { animation-name: slideIn, fadeIn; animation-duration: 2s;}When to use CSS Animations
CSS animations can be complicated. I recommend using them only if the following conditions are met:
- You need to change a property more than once in the animation
- You need to change more than 2 properties at once
- You want to trigger the animation when the screen loads (without listening for any JavaScript event)
- The animation has less than 4 points (the percentage thing)
If your animations are simpler than the conditions above, use CSS Transitions.
If your animation is more complicated than the conditions above, use JavaScript to animate.
Exercise
- Create a
@keyframesdeclaration - Write the animation with the
animationshorthand - Experiment with
animation-iteration-count - Experiment with
animation-direction - Pause and play our CSS animation with
animation-play-state
Create a @keyframes declaration
@keyframes jump { 0% { transform: translateY(0px); } 10% { transform: translateY(-30px); } 20% { transform: translateY(0px); } 35% { transform: translateY(-60px); }}Write the animation with the animation shorthand
/* Background, width, and height lets you see the element moving */.element { position: relative; height: 100px; width: 100px; background-color: red; top: 500px; animation: 3s jump infinite;}Experiment with animation-iteration-count.
.element { position: relative; height: 100px; width: 100px; background-color: red; top: 500px; animation: jump; animation-duration: 3s; animation-iteration-count: 2;}Experiment with animation-direction
.element { position: relative; height: 100px; width: 100px; background-color: red; top: 500px; animation: jump; animation-duration: 3s; animation-iteration-count: 2; animation-direction: reverse;}Pause and play our CSS animation with animation-play-state.
.element { position: relative; height: 100px; width: 100px; background-color: red; top: 500px; animation: jump; animation-duration: 3s; animation-iteration-count: 2; animation-direction: reverse; animation-play-state: paused;}
.element:hover { animation-play-state: running;}Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson