Complete Guide to CSS Animations

Learn how to create engaging animations using only CSS.

Introduction to CSS Animations

CSS animations let you animate transitions from one CSS style configuration to another. They consist of two components: a style describing the animation, and a set of keyframes that indicate the start, end, and intermediate states of the animation.

/* Basic animation syntax */
@keyframes example {
  from { background-color: red; }
  to   { background-color: yellow; }
}

.element {
  animation-name: example;
  animation-duration: 4s;
}

Animation Properties

CSS animations are controlled with the following properties:

Animation Shorthand & Default Values

The animation shorthand combines all sub-properties into one declaration. Its default value is:

.element {
  animation: name duration timing-function delay iteration-count direction fill-mode play-state;
}

/* Default value */
animation: none 0 ease 0 1 normal none running;

This expands to the following individual defaults:

↓ longhand ↓ animation-name: sdTech; animation-duration: 3s; animation-timing-function: linear; animation-delay: 1s; animation-iteration-count: infinite; animation-direction: reverse; animation-fill-mode: forwards; animation-play-state: running;

↓ shorthand ↓ animation: sdTech 3s linear 1s infinite reverse forwards running;

💗

Control All Animations

Animation Name

This property links an element to a specific @keyframes rule by name.

animation-name: myAnimation;

Each element can point to a different @keyframes rule. Below, three planes share the same markup but use three different animation-name values β€” each one plays a completely different motion.

animation-name: anFly;

✈

animation-name: anSpin;

✈

animation-name: anZoom;

✈

Control All Animations

What are CSS @keyframes?

@keyframes let you define styles at various points during the animation sequence. You specify what styles the element has at certain times, and the browser fills in the transitions between those keyframes.

@keyframes animation-name {
  from { /* starting styles */ }
  to   { /* ending styles   */ }
}

/* Equivalent syntax using percentages */
@keyframes animation-name {
  0%   { /* styles at 0%   */ }
  100% { /* styles at 100% */ }
}

/* Multiple intermediate steps */
@keyframes animation-name {
  0%   { /* styles at 0%   */ }
  50%  { /* styles at 50%  */ }
  100% { /* styles at 100% */ }
}

Below is a live demo of a keyframe rule with five steps (0%, 25%, 50%, 75%, 100%). The plane slides left–right twice, and its hue shifts at each keyframe so you can clearly see every stop point.

0% β†’ 25% β†’ 50% β†’ 75% β†’ 100%

✈

Control All Animations

CSS Animation Duration

Sets how long the animation takes to complete one cycle. A value of 2s means the animation finishes in 2 seconds; 3s means 3 seconds, and so on.

animation-duration: 1s;

animation-duration: 1s;

✈

animation-duration: 2s;

✈

animation-duration: 3s;

✈

animation-duration: 4s;

✈

Control All Animations

What are Timing Functions?

Timing functions (also called easing functions) specify the rate of change of an animation over time. They make animations feel more natural by controlling acceleration and deceleration. Instead of moving at constant speed, objects in the real world speed up, slow down, and bounce β€” timing functions recreate these natural movements.

Timing Function Details

ease

✈

ease-in

✈

ease-out

✈

ease-in-out

✈

linear

✈

steps(8)

✈

cubic-bezier(0.9, 0.2, 0.1, 1)

✈

Control All Animations

CSS Animation Delay

The animation-delay property waits a specified amount of time before the animation starts playing.

animation-delay: 2s; /* waits 2 seconds before starting */

normal

✈

animation-delay: 1s;

✈

animation-delay: 2s;

✈

animation-delay: 3s;

✈

Control All Animations

Animation Iteration Count

The animation-iteration-count property controls how many times the animation repeats. For example, to repeat 5 times:

animation-iteration-count: 5;

To make the animation repeat forever, use the keyword infinite:

animation-iteration-count: infinite;

animation-iteration-count: 1;

✈

animation-iteration-count: 2;

✈

animation-iteration-count: 3;

✈

animation-iteration-count: 4;

✈

Control All Animations

CSS Animation Direction

This property sets the direction in which the animation plays. Values:

normal

✈

reverse

🚓

alternate

✈

alternate-reverse

🚓

Control All Animations

CSS Animation Fill Mode

The animation-fill-mode property controls what styles are applied to the element before and after the animation runs.

normal

✈

backwards

✈

forwards

✈

both

✈

Control All Animations

Animation Play State

⚠️ A paused animation is frozen at its current frame β€” it does NOT reset to the start. Click any Toggle button below to see: running planes freeze mid-flight, and paused planes resume from where they stopped.

running (3s normal speed)

✈

paused (frozen at 25%)

✈

running (2s faster)

✈

paused (frozen at 75%)

✈

Control All Animations

Bonus: Accessibility & Performance Tips

Respect user preferences (prefers-reduced-motion)

Some users experience motion sickness from animations. Always honor the prefers-reduced-motion media query:

@media (prefers-reduced-motion: reduce) {
  .element {
    animation: none;
  }
}

Optimize for performance

For smooth 60 fps animations, prefer animating transform and opacity over layout-triggering properties like width, height, top, or left. Use will-change sparingly to hint the browser to optimize ahead of time:

.element {
  will-change: transform;
}

Run multiple animations on one element

Separate multiple animations with commas:

.element {
  animation: slideIn 1s ease, fadeIn 2s ease;
}

Conclusion

CSS Animation is a powerful way to make websites more attractive, interactive, and modern. It allows developers to add impressive animations to text, buttons, images, and other HTML elements without relying on JavaScript.

By using CSS properties such as @keyframes, animation-duration, animation-delay, animation-timing-function, and animation-iteration-count, you can create smooth animations, loading effects, button animations, and engaging website interfaces.

Whether you want to add a simple hover effect to a button or create advanced animations for an entire website, CSS Animation can help improve your web design and user experience. However, animations should be used carefully to maintain website performance and avoid unnecessary visual distractions.

You should also consider users who prefer reduced motion. The CSS prefers-reduced-motion media query helps developers provide a more comfortable browsing experience for these users.

Start practicing CSS Animation today and learn to create beautiful, smooth, and professional animation effects that bring your website designs to life.