1. Introduction
In this tutorial, our goal is to learn how to create animations using the @keyframes
rule in CSS. This rule allows us to specify the behavior of an animation sequence, controlling how the animation transitions through keyframes, from the start of the sequence until the end.
By the end of this tutorial, you will learn:
- What CSS Keyframe Animations are
- How to create a basic animation using @keyframes
- How to control animation properties
Prerequisites for this tutorial include a basic understanding of HTML and CSS. You should know how to create a basic HTML document and how to apply CSS styles.
2. Step-by-Step Guide
CSS animations are made up of two basic building blocks: keyframes and animation properties. Keyframes define the stages and styles of the animation sequence, while the animation properties link a CSS style to a set of keyframes.
Let's break down these concepts:
Keyframes
A keyframe in CSS is defined using the @keyframes
rule, followed by a name for the animation. Inside the curly braces, you specify the styles for each keyframe.
For instance:
@keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
In this example, the animation will change the background color from red to yellow.
Animation Properties
Animation properties are used to control the behavior of the animation. The most commonly used properties include animation-name
, animation-duration
, animation-delay
, animation-iteration-count
, animation-direction
, etc.
For instance:
div {
animation-name: example;
animation-duration: 5s;
}
In this example, the animation named 'example' will be applied to the div element and it will last 5 seconds.
3. Code Examples
Let's look at a few examples.
Example 1: Basic CSS Keyframes Animation
/* Define the animation */
@keyframes example {
0% {background-color: red; left:0px; top:0px;}
25% {background-color: yellow; left:200px; top:0px;}
50% {background-color: blue; left:200px; top:200px;}
75% {background-color: green; left:0px; top:200px;}
100% {background-color: red; left:0px; top:0px;}
}
/* Apply the animation to the div element */
div {
width: 100px;
height: 100px;
position: relative;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: infinite;
}
In this example, the div element will animate in a square path, changing its background color at each corner. The animation will repeat indefinitely.
Example 2: CSS Keyframes Animation with Delay
/* Define the animation */
@keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
/* Apply the animation to the div element */
div {
width: 100px;
height: 100px;
animation-name: example;
animation-duration: 4s;
animation-delay: 2s;
animation-iteration-count: infinite;
}
In this example, the animation will not start until 2 seconds after the page loads.
4. Summary
In this tutorial, we have learned what CSS Keyframe Animations are and how to create them. We've seen how to define keyframes using the @keyframes
rule and how to control animation properties.
To further your learning, explore more complex animations and experiment with different animation properties. You can also learn about CSS transitions and transforms which can be combined with animations for more complex effects.
5. Practice Exercises
Exercise 1: Create a CSS animation that moves a div element from the left side of the screen to the right over a period of 10 seconds.
Exercise 2: Create a CSS animation that changes the background color of a div element from red to blue to green over a period of 5 seconds.
Exercise 3: Create a CSS animation that scales a div element from 1x to 2x over a period of 3 seconds, with a delay of 1 second.
Remember, practice makes perfect. Happy coding!