CSS3 animations represent a revolutionary feature enabling granular control over element motion through multiple control points. Unlike transitions that respond to state changes, animations operate autonomously, support intricate transformations, and provide extensive playback controls.
Core Implementation Pattern
The animation workflow follows two distinct phases: declaration and application.
/* Phase 1: Declaration */
@keyframes expandElement {
from {
width: 80px;
opacity: 0.3;
}
to {
width: 300px;
opacity: 1;
}
}
/* Phase 2: Application */
.animated-box {
animation-name: expandElement;
animation-duration: 1.8s;
}
Keyframe Specifications
Animation sequences define intermediate states using percentage notation or keyword aliases (from/to). Each keyframe represents a temporal snapshot where CSS properteis interpolate smoothly between defined values.
.slider {
width: 120px;
height: 120px;
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
animation-name: slideRight;
animation-duration: 3s;
}
@keyframes slideRight {
0% {
transform: translateX(0) rotate(0deg);
border-radius: 0;
}
50% {
transform: translateX(250px) rotate(180deg);
border-radius: 50%;
}
100% {
transform: translateX(500px) rotate(360deg);
border-radius: 0;
}
}
Animation Property Reference
| Property | Functionality |
|---|---|
@keyframes |
Defines the animation sequence blueprint |
animation-name |
Specifies which keyframe set to apply (required) |
animation-duration |
Sets cycle length in seconds/milliseconds (required) |
animation-timing-function |
Controls velocity curve, defaults to ease |
animation-delay |
Postpones animation start, defaults to 0s |
animation-iteration-count |
Determines repeat cycles, accepts infinite |
animation-direction |
Enables reverse playback (alternate) |
animation-play-state |
Toggles between running and paused |
animation-fill-mode |
Defines pre/post-animation styling behavior |
.bouncer {
width: 100px;
height: 100px;
background: #9b59b6;
animation-name: bounce;
animation-duration: 2.5s;
animation-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
animation-delay: 1s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-fill-mode: both;
}
.bouncer:hover {
animation-play-state: paused;
}
@keyframes bounce {
0%, 100% { transform: translateY(0) scale(1); }
50% { transform: translateY(-150px) scale(0.8); }
}
Shorthand Notation
The animation property consolidates multiple declarations into a single statement, excluding animation-play-state.
/* syntax: name duration timing-function delay iteration-count direction fill-mode */
.element {
animation: pulse 3s ease-in-out 0.5s 4 alternate forwards;
}
@keyframes pulse {
from {
box-shadow: 0 0 0 0 rgba(52, 152, 219, 0.7);
}
to {
box-shadow: 0 0 0 40px rgba(52, 152, 219, 0);
}
}
Key considerations:
animation-direction: alternatecreates back-and-forth motionanimation-fill-mode: forwardspreserves the final keyframe state- Playback control via
animation-play-stateis typically used interactively
Timing Function Precision
The animation-timing-function property defines acceleration patterns:
| Value | Characteristics |
|---|---|
linear |
Constant velocity throughout |
ease |
Default; slow start, accelerates, decelerates |
ease-in |
Gradual acceleration from rest |
ease-out |
Deceleration toward end |
ease-in-out |
Smooth start and finish |
steps(n) |
Discrete jumps across n intervals |
Frame-Based Animation with steps()
The steps() function enables sprite-sheet style animations by dividing the duration into equal intervals.
.typewriter-container {
width: 0;
height: 60px;
overflow: hidden;
white-space: nowrap;
border-right: 3px solid #2c3e50;
animation: typing 5s steps(30) forwards;
}
@keyframes typing {
from {
width: 0;
}
to {
width: 450px;
}
}
This technique creates a typewriter affect where text reveals character-by-character through discrete width increments rather than smooth interpolation.