Ctrl + K
CSS11 min read

CSS Animations Guide

Master CSS animations, keyframes, animation properties and practical techniques for creating engaging user interfaces.

Published: 2026-08-01

CSS animations allow web elements to change their appearance over time without requiring JavaScript. They make interfaces feel more interactive by smoothly animating properties such as position, opacity, color, rotation and scale.

Modern browsers provide powerful animation capabilities directly in CSS, making it possible to build loaders, hover effects, page transitions and complex UI interactions using only stylesheets.

What Are CSS Animations?

A CSS animation consists of two main parts: an @keyframes rule that describes how values change, and animation properties that apply those keyframes to an element.

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

.box {
  animation: fadeIn 600ms ease;
}

When the animation starts, the browser interpolates values between the keyframes to create smooth motion.

Why Use CSS Animations?

Animations improve user experience by providing visual feedback, drawing attention to important elements and making interfaces feel more responsive.

  • Improve user feedback.
  • Highlight important changes.
  • Create smoother interfaces.
  • Guide user attention.
  • Increase perceived quality.
  • Require very little code.

Understanding @keyframes

The @keyframes rule defines one or more stages of an animation. Each stage specifies how CSS properties should look at a certain percentage of the animation timeline.

@keyframes move {
  0% {
    transform: translateX(0);
  }

  50% {
    transform: translateX(120px);
  }

  100% {
    transform: translateX(0);
  }
}

The browser automatically calculates the intermediate values between every keyframe.

Animation Properties

PropertyPurpose
animation-nameSelects the keyframes
animation-durationDefines animation length
animation-timing-functionControls animation speed
animation-delayDelays the start
animation-iteration-countSets repeat count
animation-directionControls playback direction
animation-fill-modeKeeps final or initial state

Using the Shorthand Property

Instead of writing every animation property individually, developers often use the animation shorthand.

.button {
  animation: pulse 800ms ease-in-out infinite;
}

This single declaration combines the animation name, duration, timing function and iteration count.

Animation Duration

The animation-duration property specifies how long one animation cycle lasts. It accepts seconds or milliseconds.

.card {
  animation-duration: 500ms;
}

Short animations usually feel more responsive, while longer animations are better suited for decorative effects.

Timing Functions

Timing functions determine how animation speed changes throughout its duration. They greatly affect how natural an animation feels.

FunctionBehavior
linearConstant speed
easeSlow start and end
ease-inStarts slowly
ease-outEnds slowly
ease-in-outSmooth start and finish
cubic-bezier()Custom timing curve
💡 Avoid using linear timing for interface animations unless a perfectly constant speed is required. Ease and ease-in-out usually feel more natural.

Animation Delay

Animations do not have to begin immediately. The animation-delay property postpones playback for a specified amount of time.

.notification {
  animation: fadeIn 500ms ease;
  animation-delay: 300ms;
}

Delays are commonly used to stagger multiple animations and create smoother sequences.

Repeating Animations

Animations can run once or repeat indefinitely. The animation-iteration-count property controls how many times an animation plays.

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

Infinite animations are commonly used for loading indicators, while interface animations often play only once.

Animation Direction

Animation direction determines whether keyframes play normally, in reverse or alternate between forward and backward playback.

ValueBehavior
normalAlways forward
reverseAlways backward
alternateForward then backward
alternate-reverseBackward then forward

Fill Modes

Normally an element returns to its original state after an animation finishes. The animation-fill-mode property controls whether the initial or final keyframe should remain applied.

.modal {
  animation: fadeIn 300ms ease;
  animation-fill-mode: forwards;
}

Using forwards keeps the element in its final animated state after playback completes.

Animating Multiple Properties

A single animation can modify several CSS properties simultaneously, allowing developers to create complex effects while keeping code organized.

@keyframes popup {
  from {
    opacity: 0;
    transform: scale(.8);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

Opacity and transform are frequently combined because they produce smooth animations while remaining performant.

Animations vs Transitions

CSS transitions and CSS animations both create motion, but they solve different problems. Transitions react to property changes, whereas animations can play automatically and contain multiple keyframes.

CSS TransitionCSS Animation
Requires a property changeCan start automatically
Two statesMultiple keyframes
Simple interactionsComplex sequences
Usually hover/focusIndependent timeline

What Should Be Animated?

Not every CSS property performs equally well during animation. Some properties trigger expensive layout calculations, while others are optimized by browsers.

RecommendedAvoid When Possible
transformwidth
opacityheight
rotatemargin
scaleleft / top
translatepadding
💡 Whenever possible, animate transform and opacity. They are usually hardware accelerated and provide the smoothest results.

Common Animation Examples

  • Loading spinners.
  • Fade-in effects.
  • Sliding menus.
  • Button hover animations.
  • Toast notifications.
  • Skeleton loaders.
  • Modal dialogs.
  • Page transitions.

Performance Tips

Smooth animations depend on minimizing expensive rendering work. Keeping animations lightweight improves responsiveness across all devices.

  • Animate transform and opacity whenever possible.
  • Avoid animating layout properties.
  • Keep animation durations reasonable.
  • Limit the number of simultaneous animations.
  • Test animations on lower-powered devices.
⚠️ Excessive animations can distract users and negatively affect usability. Motion should support the interface rather than dominate it.

Accessibility Considerations

Animations should enhance usability rather than interfere with it. Some users experience motion sensitivity, so websites should respect the prefers-reduced-motion media query and reduce or disable unnecessary movement.

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}

Supporting reduced motion improves accessibility while maintaining a comfortable browsing experience for all users.

Common Mistakes

  • Animating too many elements simultaneously.
  • Using extremely long animation durations.
  • Animating layout properties instead of transforms.
  • Applying infinite animations without purpose.
  • Ignoring users who prefer reduced motion.
  • Using distracting decorative animations throughout the interface.

When CSS Animations Are the Right Choice

Use CSS AnimationsConsider JavaScript
Fade effectsPhysics simulations
Loading indicatorsInteractive games
Hover effectsComplex timelines
Menu transitionsScroll-driven logic
UI feedbackDynamic user-controlled sequences

Best Practices

  • Keep animations short and purposeful.
  • Use consistent timing throughout the application.
  • Prefer transform and opacity for smooth rendering.
  • Reuse keyframes where possible.
  • Test animations on desktop and mobile devices.
  • Respect accessibility preferences using prefers-reduced-motion.

Frequently Asked Questions

What is the difference between CSS animations and transitions?

Transitions animate changes between two states, while animations can contain multiple keyframes and play independently of user interaction.

Which CSS properties are best for animation?

Transform and opacity are generally the best choices because browsers can optimize them for smooth rendering.

Can CSS animations repeat forever?

Yes. Setting animation-iteration-count to infinite makes the animation loop continuously.

Can multiple animations run on the same element?

Yes. Multiple animations can be applied by separating them with commas in the animation property.

Do CSS animations require JavaScript?

No. CSS animations work entirely in CSS, although JavaScript can be used to trigger or control them when needed.

Helpful CSS Animation Tools

A CSS Animation Generator helps build keyframes visually, a CSS Transition Generator simplifies transition properties, a CSS Timing Function Generator creates custom easing curves, a CSS Transform Generator builds transform declarations for translation, rotation and scaling, and a CSS Formatter keeps animation-heavy stylesheets clean and easy to maintain.

Conclusion

CSS animations are an essential part of modern web design. They provide an efficient way to add movement, improve user feedback and create polished interfaces without relying on JavaScript for every interaction. By understanding keyframes, animation properties, timing functions and performance best practices, developers can build animations that are smooth, accessible and enjoyable while keeping their stylesheets maintainable and efficient.