Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Animations in CSS?

In the world of web development, CSS plays a crucial role in designing and styling the look of web pages. Among the many features of CSS, animations stand out as a powerful tool to create eye-catching and engaging user experiences. In this blog, we will dive deep into understanding what animations in CSS are, how to create them, and some practical examples to get you started on your journey of creating stunning web animations.

What are Animations in CSS?

Animations in CSS are a way of bringing motion and interactivity to web elements. They can be used to create smooth transitions between different states, give feedback to user actions, or simply create a more engaging and visually appealing design. With CSS animations, you can control the timing, duration, and the way elements change from one style to another.

Getting Started with CSS Animations

To create animations in CSS, we need two key components:

  1. Keyframes: These define the styles for different stages of the animation.
  2. Animation properties: These properties are used to control the behavior and timing of the animation.

Let's start by understanding keyframes.

Keyframes

Keyframes are the foundation of CSS animations. They define what the animation looks like at various points in time. To create keyframes, you use the @keyframes rule followed by a name for your animation. Inside the @keyframes rule, you can specify the styles for different percentages of the animation's duration using a percentage (0% to 100%) or the keywords from (0%) and to (100%).

Here is a simple example of a keyframe that changes the background color of an element:

@keyframes change-background {
  0% {
    background-color: blue;
  }
  50% {
    background-color: green;
  }
  100% {
    background-color: red;
  }
}

In this example, the animation starts with a blue background color, transitions to green at the halfway point (50%), and ends with a red background color.

Animation Properties

Now that we have defined our keyframes, we need to apply the animation to an element and control its behavior. For this, we use various animation properties such as:

  • animation-name: The name of the animation, as defined in the @keyframes rule.
  • animation-duration: How long the animation should take to complete one cycle.
  • animation-timing-function: The speed curve of the animation. This can be values like ease, linear, ease-in, ease-out, ease-in-out, or a custom cubic-bezier function.
  • animation-delay: The time to wait before starting the animation.
  • animation-iteration-count: How many times the animation should repeat. Can be a number or infinite.
  • animation-direction: Whether the animation should play in reverse or alternate between forward and reverse.
  • animation-fill-mode: Specifies the styles applied to the element before and after the animation.

Here's a practical example of applying these properties to our previously defined change-background animation:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    @keyframes change-background {
      0% {
        background-color: blue;
      }
      50% {
        background-color: green;
      }
      100% {
        background-color: red;
      }
    }

    .box {
      width: 100px;
      height: 100px;
      animation-name: change-background;
      animation-duration: 4s;
      animation-timing-function: ease-in-out;
      animation-iteration-count: infinite;
    }
  </style>
</head>
<body>
  <div class="box"></div>
</body>
</html>

In this example, we have a .box element to which we have applied the change-background animation. The animation will take 4 seconds to complete one cycle (animation-duration: 4s), and it will repeat infinitely (animation-iteration-count: infinite). The animation-timing-function is set to ease-in-out, which means the animation will start slowly, speed up in the middle, and slow down again at the end.

Practical Examples of CSS Animations

Now that we have learned the basics of CSS animations, let's look at some practical examples to help you get a better understanding of how to use animations in your projects.

Example 1: Button Hover Effect

In this example, we will create a simple button hover effect where the button's background color will transition smoothly when the user hovers over it.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .button {
      background-color: blue;
      color: white;
      padding: 10px 20px;
      cursor: pointer;
      transition: background-color 0.3s ease-in-out;
    }

    .button:hover {
      background-color: red;
    }
  </style>
</head>
<body>
  <button class="button">Click me</button>
</body>
</html>

In this example, we used the transition property, which is a shorthand for transition-property, transition-duration, transition-timing-function, and transition-delay. The transition property allows us to create smooth transitions between different states of an element. In this case, we are transitioning the background-color property with a duration of 0.3 seconds and an ease-in-out timing function.

Example 2: Loading Spinner

In this example, we will create a simple loading spinner using CSS animations.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    @keyframes spin {
      0% {
        transform: rotate(0deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }

    .spinner {
      width: 50px;
      height: 50px;
      border: 5px solid lightgray;
      border-top-color: blue;
      border-radius: 50%;
      animation-name: spin;
      animation-duration: 1s;
      animation-timing-function: linear;
      animation-iteration-count: infinite;
    }
  </style>
</head>
<body>
  <div class="spinner"></div>
</body>
</html>

In this example, we created a keyframe named spin that rotates an element 360 degrees. We then created a .spinner element with a circular shape and a blue border on top. We applied the spin animation to the .spinner element with a duration of 1 second and set it to repeat infinitely.

Conclusion

CSS animations are a powerful tool for creating engaging and interactive user experiences on the web. By understanding keyframes and animation properties, you can create a wide variety of animations for your projects. We hope this blog has helped you in understanding the basics of CSS animations and provided you with practical examples to practice and explore further. Happy animating!