CSS

CSS Animation: Syntax, Usage, and Examples

CSS animation allows developers to add smooth transitions and motion effects to elements without relying on JavaScript or external libraries. It enhances user experience by making web pages feel more interactive and engaging.

How to Use CSS Animation

Define CSS animations using the animation property, which serves as a shorthand for multiple animation-related properties. To create an animation, specify two key components:

  1. Keyframes (@keyframes) – Define the animation states.
  2. Animation Properties – Control how the animation behaves.

Basic Syntax of CSS Animation

@keyframes example {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.element {
  animation: example 2s ease-in-out;
}
  • The @keyframes rule sets the animation sequence.
  • The .element class applies the animation with a duration of 2s and an ease-in-out timing function.

CSS Animation Properties Explained

  • animation-name – Specifies which keyframe animation to use.
  • animation-duration – Defines how long the animation runs.
  • animation-timing-function – Controls the speed curve (e.g., ease, linear, ease-in-out).
  • animation-delay – Delays the animation’s start.
  • animation-iteration-count – Determines how many times the animation plays (infinite, 1, 3, etc.).
  • animation-direction – Defines the direction (normal, reverse, alternate).
  • animation-fill-mode – Specifies styles before and after animation (none, forwards, backwards, both).
  • animation-play-state – Toggles between running and paused states.

When to Use CSS Animation

Enhancing User Interaction

Animations provide visual feedback for user interactions, such as button hover effects, menu transitions, or click responses.

.button:hover {
  animation: pulse 0.5s ease-in-out;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

Creating Loading Indicators

A CSS loading animation improves usability by visually indicating that content is being processed.

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.loader {
  animation: spin 1s linear infinite;
}

Animating Text for Visual Impact

CSS text animations make text fade, slide, or scale smoothly.

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.text {
  animation: fade-in 2s ease-in-out;
}

Examples of CSS Animation in Action

Rotating Elements

A CSS rotation animation applies a continuous spin effect.

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.icon {
  animation: rotate 2s linear infinite;
}

Expanding a Box on Hover

Hover animations smoothly scale elements when a user hovers over them.

@keyframes scale-up {
  from {
    transform: scale(1);
  }
  to {
    transform: scale(1.2);
  }
}

.box:hover {
  animation: scale-up 0.3s ease-in-out;
}

Color Transitions with Background Animation

CSS animations smoothly transition background colors for dynamic UI effects.

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

.box {
  animation: change-bg 4s infinite alternate;
}

Animating Multiple Properties Simultaneously

Combine animations to move and fade elements at the same time.

@keyframes move-and-fade {
  0% {
    transform: translateY(-20px);
    opacity: 0;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

.element {
  animation: move-and-fade 1s ease-out;
}

Advanced CSS Animation Concepts

Animation Delay for Staggered Effects

Delaying each animation cycle helps create staggered effects.

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.text {
  animation: fadeIn 2s ease-in-out infinite;
  animation-delay: 1s;
}

Controlling Animation Iteration

To repeat animations a set number of times, specify animation-iteration-count.

.loop {
  animation: bounce 1s ease-in-out 3;
}

Reversing and Alternating Animations

Using animation-direction: alternate makes the animation run in reverse every other cycle.

.alternate {
  animation: slide 3s alternate infinite;
}

Pausing and Resuming Animations

CSS animations can pause and resume dynamically.

.paused {
  animation-play-state: paused;
}

Performance Considerations for CSS Animation

Animations enhance design but can slow performance if not optimized. Follow these best practices:

  1. Use transform and opacity instead of animating width or height – GPU acceleration makes transformations smoother.
  2. Avoid animating large elements – Reducing the affected area improves rendering speed.
  3. Use will-change on frequently animated elements – This helps browsers optimize rendering.
  4. Minimize animation-delay on interactive elements – This prevents unnecessary wait times.

Learn More About CSS Animation

CSS Animation vs. CSS Transitions

  • CSS Transitions animate changes between states, triggered by user interaction.
  • CSS Animations allow complex sequences, looping, and multiple keyframes.

Combining CSS Animation with JavaScript

JavaScript provides more dynamic control over animations.

document.querySelector(".box").addEventListener("click", function() {
  this.style.animation = "shake 0.5s";
});

When to Use JavaScript Over CSS for Animation

  • When animations need to stop, pause, or reverse dynamically.
  • When animations depend on complex user input beyond hover or focus.
  • When synchronizing animations with other JavaScript events.

CSS Animation Libraries

Using CSS libraries like Animate.css speeds up development by providing pre-built effects.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css">

<div class="animate__animated animate__bounce">Hello!</div>

CSS animations enhance modern web design by enabling smooth, interactive effects without JavaScript. They improve usability, guide users, and create visually appealing experiences. By mastering keyframes, animation properties, and best practices, developers can build polished animations that enhance web interfaces efficiently.

Learn CSS for Free
Start learning now
button icon
To advance beyond this tutorial and learn CSS by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH