CSS Animations

CSS Animations allow HTML elements to move, change color, rotate, resize, fade, and perform many visual effects automatically without using JavaScript.

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
CSS Animations make websites look modern and engaging.

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%).
Every CSS animation starts with an @keyframes rule.

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.
The animation will not run until a valid animation name is assigned.

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.
Without animation-duration, the animation will not be visible because the default duration is 0 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.
Use animation delays carefully to avoid making the interface feel slow.

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.
Use infinite carefully because continuous animations may distract users.

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.
alternate is commonly used for smooth back-and-forth animations.

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.
The timing function makes animations feel natural and smooth.

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;

}
CSS Animations are commonly used for loaders, menus, buttons, cards, images, notifications, and page transitions.

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;

}
The animation shorthand property combines multiple animation properties into a single declaration, making your CSS cleaner and easier to maintain.

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 transform and opacity for 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 animation property to keep code clean.
Well-designed animations improve usability without distracting users.

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;

}
Excessive animations can negatively affect performance and user experience.

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
CSS Animations are supported in all major modern browsers.

14. CSS Animation Interview Questions

  1. What are CSS Animations?
  2. What is the purpose of @keyframes?
  3. Which property specifies the animation name?
  4. What does animation-duration do?
  5. How do you delay an animation?
  6. What is the purpose of animation-iteration-count?
  7. What does animation-direction control?
  8. Which property controls animation speed?
  9. What is the shorthand property for CSS Animations?
  10. What is the difference between CSS Transitions and CSS Animations?
These questions are commonly asked during Front-End Developer interviews.

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.
Congratulations! You have successfully completed the CSS Animations lesson. You can now create engaging animations such as loaders, buttons, banners, icons, menus, and interactive UI components using pure CSS.

Quick Quiz

Which CSS rule is used to define an animation?