CSS Animations
1. Introduction to CSS Animations
CSS Animations enable you to create smooth motion effects by changing CSS property values over time. Animations improve user experience and make websites more attractive and interactive.
Advantages of CSS Animations
- Create attractive visual effects.
- Improve user interaction.
- Do not require JavaScript.
- Run smoothly in modern browsers.
- Useful for buttons, cards, images, loaders, and menus.
Without Animation
Button ā Static
With Animation
Button ā Smoothly moves,
changes color or fades
2. @keyframes Rule
The @keyframes rule defines the animation by specifying what styles an element should have at different stages of the animation.
@keyframes move{
from{
left:0;
}
to{
left:200px;
}
}
| Keyword | Description |
|---|---|
| from | Starting point (0%). |
| to | Ending point (100%). |
3. animation-name
The animation-name property connects an
element to a specific @keyframes animation.
@keyframes colorChange{
from{
background:red;
}
to{
background:blue;
}
}
.box{
animation-name:colorChange;
}
| Property | Purpose |
|---|---|
| animation-name | Specifies which animation to use. |
4. animation-duration
The animation-duration property specifies how long an animation takes to complete one cycle.
.box{
animation-name:move;
animation-duration:2s;
}
| Value | Meaning |
|---|---|
| 1s | Animation runs for one second. |
| 2s | Animation runs for two seconds. |
| 5s | Animation runs for five seconds. |
5. animation-delay
The animation-delay property specifies how long the browser waits before starting an animation.
.box{
animation-name:move;
animation-duration:2s;
animation-delay:1s;
}
| Delay | Effect |
|---|---|
| 0s | Animation starts immediately. |
| 1s | Starts after one second. |
| 2s | Starts after two seconds. |
6. animation-iteration-count
The animation-iteration-count property specifies how many times an animation should repeat.
.box{
animation-name:bounce;
animation-duration:2s;
animation-iteration-count:3;
}
| Value | Description |
|---|---|
| 1 | Runs once. |
| 3 | Runs three times. |
| infinite | Runs continuously. |
7. animation-direction
The animation-direction property specifies whether an animation should play forward, backward, or alternate between directions.
.box{
animation-name:move;
animation-duration:2s;
animation-direction:alternate;
}
| Value | Description |
|---|---|
| normal | Default direction. |
| reverse | Plays backward. |
| alternate | Forward then backward. |
| alternate-reverse | Backward then forward. |
8. animation-timing-function
The animation-timing-function property controls the speed curve of an animation.
.box{
animation-name:move;
animation-duration:3s;
animation-timing-function:ease-in-out;
}
| Value | Behavior |
|---|---|
| linear | Constant speed. |
| ease | Slow start and end. |
| ease-in | Slow start. |
| ease-out | Slow end. |
| ease-in-out | Slow start and end. |
9. Practical Animation Examples
Example 1: Moving Box
@keyframes move{
from{
left:0;
}
to{
left:200px;
}
}
.box{
position:relative;
animation:move 2s;
}
Example 2: Rotating Element
@keyframes rotate{
from{
transform:rotate(0deg);
}
to{
transform:rotate(360deg);
}
}
.logo{
animation:rotate 3s linear infinite;
}
Example 3: Fade In
@keyframes fade{
from{
opacity:0;
}
to{
opacity:1;
}
}
.text{
animation:fade 2s;
}
10. CSS Animation Properties Comparison Table
| Property | Purpose |
|---|---|
| animation-name | Selects the animation. |
| animation-duration | Specifies animation time. |
| animation-delay | Delays the animation. |
| animation-iteration-count | Sets repeat count. |
| animation-direction | Controls animation direction. |
| animation-timing-function | Controls animation speed. |
| animation | Shorthand property. |
.box{
animation:
move 2s ease-in-out
1s infinite alternate;
}
11. CSS Animation Best Practices
Following best practices helps create smooth, attractive, and user-friendly animations while maintaining website performance.
- Use animations only where they improve user experience.
- Keep animation duration reasonable (0.3sā2s).
- Prefer animating
transformandopacityfor better performance. - Avoid too many animations on the same page.
- Use meaningful animation names.
- Test animations on different browsers and devices.
- Use infinite animations only when necessary.
- Use the shorthand
animationproperty to keep code clean.
12. Common CSS Animation Mistakes
| Mistake | Correct Practice |
|---|---|
| Using extremely long animations. | Keep animations short and meaningful. |
| Adding too many animations. | Animate only important elements. |
Using infinite unnecessarily. |
Repeat only when required. |
| Ignoring performance. | Animate transform and opacity. |
| Forgetting browser testing. | Always test animations before deployment. |
ā Incorrect
.box{
animation:move 10s infinite;
}
ā Correct
.box{
animation:move 1s ease;
}
13. Browser Support
CSS Animations are fully supported by all modern web browsers and are widely used in responsive web applications.
| Browser | Support |
|---|---|
| Google Chrome | ā Supported |
| Mozilla Firefox | ā Supported |
| Microsoft Edge | ā Supported |
| Safari | ā Supported |
| Opera | ā Supported |
14. CSS Animation Interview Questions
- What are CSS Animations?
- What is the purpose of
@keyframes? - Which property specifies the animation name?
- What does
animation-durationdo? - How do you delay an animation?
- What is the purpose of
animation-iteration-count? - What does
animation-directioncontrol? - Which property controls animation speed?
- What is the shorthand property for CSS Animations?
- What is the difference between CSS Transitions and CSS Animations?
15. Lesson Summary
In this lesson, you learned:
- ā Introduction to CSS Animations.
- ā @keyframes Rule.
- ā animation-name.
- ā animation-duration.
- ā animation-delay.
- ā animation-iteration-count.
- ā animation-direction.
- ā animation-timing-function.
- ā Practical Animation Examples.
- ā Best Practices and Common Mistakes.
Quick Quiz
Which CSS rule is used to define an animation?