Writing Keyframe Animations with @keyframes
- CSS Basics
- Selectors & Specificity
- Box Model & Units
- Typography & Text Styling
- Colors & Color Spaces
- Positioning & Stacking Context
- Flexbox
- Grid
- Grid Advanced
- Responsive Design
- Container Queries
- Pseudo-classes & Pseudo-elements
- Modern Selectors
- CSS Custom Properties
- CSS Functions
- Transitions & Animations
- Transforms
- Forms & Input Styling
- CSS Architecture & Methodologies
- Accessibility in CSS
- Performance & Optimization
- View Transitions & Modern APIs
- Cascade Layers & Scope
Why Animations Go Beyond Transitions
A transition only animates between two states - a start and an end. @keyframes animations can define multiple steps in between, run automatically without needing a triggering state change, and repeat indefinitely.
Defining a @keyframes Sequence
@keyframes fade-in-up {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}from and to are shorthand for 0% and 100%. More precise control is possible by using percentage steps directly.
Multiple Steps with Percentages
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}Applying the Animation to an Element
.card {
animation: fade-in-up 0.4s ease-out;
}
.badge {
animation: pulse 2s ease-in-out infinite;
}The animation shorthand accepts the keyframe name, duration, timing function, and options like infinite to repeat forever, or a specific repeat count.
Quick Comparison
| Transition | Animation | |
|---|---|---|
| Number of steps | Two (start, end) | Any number, via keyframes |
| Needs a trigger? | Yes (state change) | No, can run automatically on load |
| Can repeat? | No | Yes, with infinite or a count |