CSS Documentation

Animations

CSS Animations

CSS animations allow you to create smooth, gradual transitions and dynamic effects for web elements without using JavaScript or Flash.

@keyframes Rule

The @keyframes rule specifies the animation code:

@keyframes example {
from { background-color: red; }
to { background-color: yellow; }
}

Animation Properties

  • animation-name: Specifies the name of the @keyframes animation
  • animation-duration: Defines how long the animation takes to complete one cycle
  • animation-timing-function: Specifies the speed curve of the animation
  • animation-delay: Defines a delay before the animation starts
  • animation-iteration-count: Specifies the number of times the animation should run
  • animation-direction: Defines whether the animation should play in reverse on alternate cycles

Example

Here's an example of using CSS animations:

.box {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
}

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

Best Practices

  • Use animations sparingly and purposefully
  • Keep animations short and snappy for better performance
  • Consider using the animation shorthand property for concise code
  • Test animations across different browsers and devices
  • Use animation-play-state to pause and resume animations

CSS animations are a powerful tool for creating engaging and interactive web experiences without the need for complex JavaScript or external libraries.