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:
- @keyframes: Defines the animation name along with CSS property values at each step.
- animation-name: Specifies the name of the
@keyframesrule. - animation-duration: Defines how long the animation takes to complete one cycle.
- animation-timing-function: Specifies the speed curve of the animation.
- animation-delay: Specifies a delay before the animation starts.
- animation-iteration-count: Defines how many times the animation runs.
- animation-direction: Specifies whether the animation plays in reverse on alternate cycles.
- animation-fill-mode: Specifies the styles applied to the element before and after the animation runs.
- animation-play-state: Specifies whether the animation is running or paused.
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:
- animation-name:
noneβ no animation is applied - animation-duration:
0sβ animation plays instantly - animation-timing-function:
easeβ slow start, fast middle, slow end - animation-delay:
0sβ animation starts immediately - animation-iteration-count:
1β animation plays once - animation-direction:
normalβ plays forwards - animation-fill-mode:
noneβ no styles applied before or after execution - animation-play-state:
runningβ animation is playing
↓ 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 β Slow start, fast middle, slow end. (default)
- ease-in β Slow start, fast end (accelerates).
- ease-out β Fast start, slow end (decelerates).
- ease-in-out β Slow start and end, faster middle (symmetrical version of
ease). - linear β Constant speed throughout, no acceleration or deceleration.
- steps(n) β Divides the animation into
ndiscrete steps.steps(5)creates 5 jumps. -
cubic-bezier(x1, y1, x2, y2) β A custom timing function defined by a Bezier curve.
This lets you fine-tune exactly where the animation speeds up or slows down.
β οΈ Important:x1andx2must be between0and1, buty1andy2can go outside this range β this is what allows bounce and overshoot effects.
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 (default) β Plays forwards, 0% β 100%.
- reverse β Plays backwards, 100% β 0%.
- alternate β Plays forward first, then backward, then forward again β repeating continuously.
- alternate-reverse β Plays backward first, then forward, then backward again β repeating continuously.
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.
- none (default) β No animation styles are retained. The element uses its normal styles before, during (until the animation actually starts), and after.
- forwards β After the animation ends, the element keeps the styles from the last keyframe (100%).
- backwards β During the delay period (before the animation starts), the element uses the styles from the first keyframe (0%). After the animation ends, it reverts to its normal styles.
-
both β Combines
forwardsandbackwards: applies the first keyframe's styles during the delay, and retains the last keyframe's styles after the animation ends.
normal
backwards
forwards
both
Control All Animations
Animation Play State
- running (default) β The animation plays normally.
- paused β The animation is paused at its current frame.
β οΈ 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.