DEV Community

Cover image for Learn CSS Animation the Cool Way
Cocoandrew
Cocoandrew

Posted on

Learn CSS Animation the Cool Way

What is CSS Animation?

CSS animation allows you to animate the values of CSS properties over time. It can be used to add visual interest and interactivity to a webpage and can also be used to create smooth transitions between different states of an element.

To create a CSS animation, you need to use the @keyframes rule to define the animation. The @keyframes rule specifies the name of the animation and the styles that the element should have at different points during the animation.

Here's an example of a simple CSS animation that changes the color of an element from red to blue over the course of 5 seconds:

@keyframes color-animation {
  0% {
    color: red;
  }
  100% {
    color: blue;
  }
}
Enter fullscreen mode Exit fullscreen mode

To apply the animation to an element, you can use the animation property in your CSS:

#my-element {
  animation: color-animation 5s linear;
}
Enter fullscreen mode Exit fullscreen mode

This will apply the color-animation animation to the element with the ID my-element, and the animation will take 5 seconds to complete. The linear value specifies the timing function, which determines the speed of the animation over time. There are many other options for the animation property, such as animation-delay to specify a delay before the animation starts, and animation-iteration-count to specify the number of times the animation should repeat.

You can also use the transition property to create simple, smooth transitions between different states of an element. For example, you can use the :hoverpseudo-class to change the color of an element when the user hovers over it:

#My-element {
  transition: color 0.9s ease-in-out;
}
Enter fullscreen mode Exit fullscreen mode
#My-element: hover {
  color: Yellow;
}
Enter fullscreen mode Exit fullscreen mode

In the HTML code for your page, you can add the

element that the animation will be applied to:
div>
  This element will animate using the growAndFadeIn animation.
</div>

This will cause the color of the element to smoothly transition from its original color to blue when the user hovers over it, and back to its original color when the user moves the mouse away.

CSS animation and transitions can add a lot of visual interest and interactivity to your webpage and are a powerful tool in the web developer's toolkit.

How does CSS animation work

CSS animation works by changing the values of CSS properties over time. The @keyframes rule is used to define the animation and specifies the name of the animation and the styles that the element should have at different points during the animation.

To apply the animation to an element, you can use the animation property in your CSS. The animation property takes several values, including the name of the animation, the duration of the animation, and the timing function. The timing function determines the speed of the animation over time, and can be set to linear, ease, ease-in, ease-out, or ease-in-out.

Once the animation has been defined and applied to an element, the browser will automatically update the element's styles at regular intervals, based on the duration and timing function of the animation. This causes the element to smoothly transition from one set of styles to another over time.

You can also use the animation-delay and animation-iteration-count properties to control when the animation starts and how many times it should repeat.

CSS animation and transitions can be used to create a wide variety of effects, such as smoothly changing an element's size, color, or position, or adding an element to or removing it from the page. They are a powerful tool for adding visual interest and interactivity to a webpage.

The benefits of using CSS Animation

There are several benefits to using CSS animation:

  • Improved user experience: CSS animation can make a webpage more
    interactive and engaging, improving the user experience. It can also be used to create smooth transitions between different states of an element, making the webpage feel more polished and professional.

  • Performance: CSS animation can be more efficient than using JavaScript to animate elements, as it offloads the animation work to the browser's rendering engine, which is optimized for this type of task. This can help improve the overall performance of the webpage.

  • Ease of use: CSS animation is easy to use and can be implemented quickly. It also allows for easy maintenance, as all the animation styles are stored in the CSS file, rather than scattered throughout the codebase.

  • Compatibility: CSS animation is widely supported by modern browsers, so it can be used with confidence that it will work on a wide range of devices.

  • Accessibility: CSS animation can be made accessible to users with disabilities by using the aria-* attributes and the prefers-reduced-motion media query.

Creating a simple animation

Here's an example of how to create a simple CSS animation that changes the color of an element from red to blue over the course of 5 seconds:

First, define the animation using the @keyframes rule:

@keyframes color-animation {
  0% {
    color: red;
  }
  100% {
    color: blue;
  }
}

This defines an animation called color-animation that changes the color of the element from red to blue. The 0% and 100% values specify the start and end points of the animation, and the color property specifies the style that the element should have at each point.
Next, apply the animation to an element using the animation property:

#my-element {
  animation: color-animation 5s linear;
}

This will apply the color-animation animation to the element with the ID my-element, and the animation will take 5 seconds to complete. The linear value specifies the timing function, which determines the speed of the animation over time.
That's it! The element will now smoothly transition from red to blue over the course of 5 seconds. You can adjust the duration and timing function to achieve the desired effect.

You can also use the animation-delay and animation-iteration-count properties to control when the animation starts and how many times it should repeat. For example:

#my-element {
  animation: color-animation 5s linear 2s infinite;
}

This will cause the animation to start 2 seconds after the page loads, and to repeat indefinitely.
There are many other options for the animation property, such as animation-direction to specify the direction of the animation (for example, forwards, backwards, or alternating), and animation-fill-mode to specify how the element should be styled when the animation is not actively running.

Conclusion
In conclusion, CSS animation is a powerful tool for adding interactive and engaging elements to a website. By defining animations using the @keyframes rule and applying them to elements using the animation property, you can create dynamic and engaging animations that add movement and interest to your page. With a little practice, you can create complex and sophisticated animations that add an extra layer of interactivity to your website. If you're still finding it difficult on how to implement CSS animation on your website you can check out this example

Top comments (11)

Collapse
 
nomzykush profile image
Nomzy-kush

👏👏

Collapse
 
elliot_brenya profile image
Elliot Brenya sarfo

well done 👍

Collapse
 
user_741b33494c profile image
user_741b33494c

Nice one dear, this is a job well done

Collapse
 
stanleydave profile image
Stanley

Properly constructed

Collapse
 
girlundtech profile image
Lilibel Obiadika

I loved. Pretty good read.

Collapse
 
christian_ezenwa_a60f667c profile image
Christian Ezenwa

Love this. Thanks🔥

Collapse
 
shre1310 profile image
Kashh

loved it :)

Collapse
 
r4iz3nth3god profile image
Raizen

Detailed explanation, love it.
Thank you!

Collapse
 
cocoandrew profile image
Cocoandrew

I'm glad you love it

Collapse
 
mimisan profile image
Tech reven

Been having issues this , thank you so much it was really helpful

Collapse
 
okotieefe profile image
Okotie Abibat

This was really helpful