CSS Animations
CSS Animations allow you to create complex animations by defining keyframes and applying animation properties to web elements. Unlike transitions, which are triggered by user interactions, animations run automatically and can repeat for continuous effects.
What are CSS Animations?
CSS Animations enable you to change an element’s style over time, often through a series of defined keyframes. They offer more control than transitions by allowing the animation to progress through multiple states and include more than just a simple property change.
Basic Syntax of CSS Animations
To create an animation, you need to define keyframes and then apply animation properties to the element you want to animate.
- @keyframes: Specifies the animation's keyframes, which define the styles at various stages of the animation.
- animation-name: The name of the animation, corresponding to the name of the keyframes.
- animation-duration: The time it takes for the animation to complete one cycle.
- animation-timing-function: Specifies the speed curve of the animation (e.g.,
ease
,linear
, etc.). - animation-delay: The delay before the animation starts.
- animation-iteration-count: The number of times the animation should repeat (e.g.,
infinite
for continuous animation). - animation-direction: Defines whether the animation should play forwards, backwards, or alternate.
Example of a Simple CSS Animation
In this example, we create a simple animation that moves a box from left to right:
@keyframes moveRight {
0% { left: 0; }
100% { left: 300px; }
}
.box {
position: relative;
width: 100px;
height: 100px;
background-color: #4CAF50;
animation-name: moveRight;
animation-duration: 2s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
}
The @keyframes
rule defines the movement of the box, starting from the left (0%) and moving to 300px (100%). The animation
property applies the animation to the box, with a duration of 2 seconds and an infinite loop.
Understanding Keyframes
Keyframes allow you to define specific states of the element at different times during the animation. Each percentage value represents a point in the animation timeline:
- 0%: The initial state of the animation.
- 50%: The midpoint of the animation.
- 100%: The final state of the animation.
Example with Multiple Keyframes
Here is an example of an animation with multiple keyframes:
@keyframes bounce {
0% { transform: translateY(0); }
50% { transform: translateY(-30px); }
100% { transform: translateY(0); }
}
.ball {
width: 50px;
height: 50px;
background-color: #FF6347;
border-radius: 50%;
animation-name: bounce;
animation-duration: 1s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
}
This animation makes a ball bounce up and down, starting from its initial position (0%), moving up (50%), and returning to the starting position (100%).
Animation Properties
CSS provides several properties for controlling animations:
- animation-name: The name of the keyframes to apply to the element.
- animation-duration: The time it takes for the animation to complete.
- animation-timing-function: Controls the pacing of the animation. Common values include
linear
,ease
,ease-in
,ease-out
, andease-in-out
. - animation-delay: Adds a delay before the animation starts.
- animation-iteration-count: Specifies how many times the animation should repeat. Set it to
infinite
for a continuous animation. - animation-direction: Defines whether the animation should run forwards, backwards, or alternate between both directions.
- animation-fill-mode: Specifies whether the animation styles should be applied before or after the animation runs (e.g.,
forwards
,backwards
,both
).
Example: Animation with Delays and Multiple Iterations
Here is an animation example with delays and multiple iterations:
@keyframes slide {
0% { left: 0; }
100% { left: 100px; }
}
.slider {
position: relative;
width: 100px;
height: 100px;
background-color: #4682B4;
animation-name: slide;
animation-duration: 3s;
animation-timing-function: ease-in-out;
animation-delay: 1s;
animation-iteration-count: 2;
animation-direction: alternate;
}
This slider will move from left to right, starting after a 1-second delay. It will repeat twice, moving back and forth with the alternate
direction.
Best Practices for CSS Animations
- Use animations sparingly: While animations can make websites more engaging, too many can be overwhelming or distracting to users.
- Consider performance: Complex animations can impact performance, especially on mobile devices. Always test your animations on different devices.
- Combine animations and transitions: For interactive design, you can combine transitions and animations to create smooth user experiences.
- Keep animations short: Limit the duration of animations to avoid frustrating users or causing distractions.