CSS

CSS (Cascading Style Sheets) is the language that brings web pages to life with colors, layouts, and animations. It works alongside HTML to create visually stunning and responsive designs. Let's explore the power of CSS.

CSS Animations

1 April 2025 | Category:

CSS animations allow you to create smooth, interactive animations on elements by defining keyframes and the behavior of the element at various points during the animation. With animations, you can control how an element changes over time without relying on JavaScript. They provide a powerful way to create complex visual effects such as fading, sliding, rotating, and more.


🔹 1. Overview of CSS Animations

CSS animations make it possible to animate an element’s properties over time. Unlike CSS transitions, which require a trigger (e.g., hover or focus), CSS animations can run automatically or be triggered by events.

Key Concepts:

  • Keyframes: Define the starting and ending points (and intermediate points) of the animation.
  • Animation Properties: Control how the animation is executed, such as duration, timing, and iteration.

🔹 2. Basic Syntax of CSS Animations

The basic syntax for CSS animations involves two main components:

  1. Defining the Keyframes: Using the @keyframes rule to define the animation’s states.
  2. Applying the Animation to Elements: Using the animation shorthand property to apply the animation to elements.

Basic Syntax:

@keyframes animation-name {
  from {
    /* Starting state */
  }
  to {
    /* Ending state */
  }
}

.element {
  animation: animation-name duration timing-function delay iteration-count direction;
}
  • animation-name: The name of the animation defined in @keyframes.
  • duration: How long the animation takes to complete (e.g., 2s).
  • timing-function: Defines how the speed of the animation progresses (e.g., ease, linear).
  • delay: How long to wait before starting the animation.
  • iteration-count: How many times the animation should run (e.g., infinite for infinite loops).
  • direction: The direction in which the animation plays (normal, reverse, etc.).

🔹 3. Defining Keyframes

The @keyframes rule defines the intermediate points of an animation, from the beginning (0%) to the end (100%), or any percentage in between.

Syntax:

@keyframes animation-name {
  0% {
    /* Starting state */
  }
  100% {
    /* Ending state */
  }
}

You can also use intermediate keyframes for more complex animations:

Example:

@keyframes slideIn {
  0% {
    transform: translateX(-100%); /* Start off-screen */
  }
  50% {
    transform: translateX(50%);  /* Move halfway */
  }
  100% {
    transform: translateX(0);    /* End at original position */
  }
}

In this example, the element will start off the screen, move halfway across, and then slide into its final position.


🔹 4. Applying the Animation to an Element

Once the @keyframes rule is defined, apply the animation using the animation property. The animation property is a shorthand for several animation-related properties.

Example:

.element {
  animation: slideIn 2s ease-in-out infinite;
}

Here:

  • The animation slideIn is applied.
  • The animation lasts 2 seconds (2s).
  • The ease-in-out timing function makes the animation start and end slowly.
  • The infinite iteration count makes the animation loop forever.

🔹 5. Animation Properties Explained

1. animation-name

This property specifies the name of the @keyframes animation to apply.

animation-name: slideIn;

2. animation-duration

This defines how long the animation will run. It can be set in seconds (s) or milliseconds (ms).

animation-duration: 2s;

3. animation-timing-function

This property controls the pacing of the animation. Some common values are:

  • linear: The animation has the same speed from start to finish.
  • ease: The animation starts slow, speeds up, and then slows down.
  • ease-in: The animation starts slow and speeds up.
  • ease-out: The animation starts fast and slows down.
  • ease-in-out: The animation starts and ends slowly, with a faster middle.
animation-timing-function: ease-in-out;

4. animation-delay

This property defines how long to wait before starting the animation. It can be set in seconds (s) or milliseconds (ms).

animation-delay: 1s;

5. animation-iteration-count

This property specifies how many times the animation should run. It can be set to a number or infinite for continuous animation.

animation-iteration-count: infinite;

6. animation-direction

This property defines the direction in which the animation plays. Common values are:

  • normal: The animation plays in the defined direction.
  • reverse: The animation plays in the reverse direction.
  • alternate: The animation alternates between forward and reverse.
  • alternate-reverse: The animation alternates in the reverse direction.
animation-direction: alternate;

7. animation-fill-mode

This property specifies how styles are applied before and after the animation is played. Common values are:

  • none: No styles are applied before or after the animation.
  • forwards: The final styles of the animation are applied after the animation ends.
  • backwards: The initial styles of the animation are applied before the animation starts.
  • both: Combines forwards and backwards.
animation-fill-mode: forwards;

🔹 6. Example of CSS Animation

Here’s an example of a simple bouncing ball animation:

HTML:

<div class="ball"></div>

CSS:

.ball {
  width: 50px;
  height: 50px;
  background-color: red;
  border-radius: 50%;
  position: absolute;
  bottom: 0;
  animation: bounce 2s ease-in-out infinite;
}

@keyframes bounce {
  0% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-100px);
  }
  100% {
    transform: translateY(0);
  }
}

In this example:

  • The ball moves up and down with a smooth bouncing effect.
  • The animation shorthand is used to apply the bounce animation.
  • The translateY property moves the ball vertically.

🔹 7. Using Multiple Animations

You can apply multiple animations to the same element by separating them with commas.

Example:

.element {
  animation: fadeIn 2s ease-in, slideIn 3s ease-out;
}

In this example:

  • The fadeIn animation runs first for 2 seconds with ease-in timing.
  • The slideIn animation runs afterward for 3 seconds with ease-out timing.

🔹 8. Animation Performance Considerations

While CSS animations are very efficient, complex animations can still cause performance issues, especially on mobile devices or with many elements on the page.

  • Use Transform and Opacity: Animations that involve transform and opacity are generally more efficient than animating properties like width, height, or left, which can trigger reflows.
  • Limit the use of box-shadow and text-shadow: Animating shadows can be resource-heavy.
  • Use Will-Change Sparingly: The will-change property can optimize certain properties for animation but should be used cautiously, as overuse can negatively affect performance.

🔹 9. Conclusion

CSS animations are a powerful way to add movement and interactivity to your website without using JavaScript. With the use of keyframes and animation properties, you can create a wide range of visual effects that enhance the user experience.

Key Takeaways:

  • @keyframes defines the key states of an animation.
  • The animation property allows you to control the animation duration, timing, delay, and more.
  • You can combine multiple animations, set loops, and control the direction of animations.
  • Always consider performance when using animations, especially for mobile users.

CSS animations provide a smooth, efficient, and interactive way to animate properties on your website, making it more engaging and user-friendly.