Premium CSS Course

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.

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:

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:

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