CSS Animations: A Complete Guide to Keyframes, Transitions, and Performance
Master CSS animations — transitions, keyframes, timing functions, performance optimization, and practical examples you can copy and use today.
DevToolsHub Team20 min read752 words
CSS Transitions vs Animations
CSS offers two ways to animate elements:
Transitions — Animate between two states when a property changes (hover, focus, class toggle) Animations — Define multi-step animations with keyframes that can run automatically
When to Use Each
| Feature | Transitions | Animations |
|---|---|---|
| Trigger | Property change | Automatic or class |
| States | 2 (start → end) | Unlimited (keyframes) |
| Looping | No | Yes |
| Complexity | Simple | Complex |
| Best for | Hover effects, toggles | Loading spinners, attention |
CSS Transitions
.button {
background: #6366f1;
transform: scale(1);
transition: all 0.3s ease;
}
.button:hover {
background: #4f46e5;
transform: scale(1.05);
}
Transition Properties
/* Shorthand */
transition: property duration timing-function delay;
/* Individual properties */
transition-property: transform, opacity;
transition-duration: 0.3s;
transition-timing-function: ease-in-out;
transition-delay: 0.1s;
/* Multiple transitions */
transition:
transform 0.3s ease,
opacity 0.2s ease 0.1s,
background-color 0.3s ease;
Timing Functions
transition-timing-function: ease; /* Default — slow start, fast middle, slow end */
transition-timing-function: linear; /* Constant speed */
transition-timing-function: ease-in; /* Slow start */
transition-timing-function: ease-out; /* Slow end */
transition-timing-function: ease-in-out; /* Slow start and end */
transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55); /* Custom bounce */
CSS Keyframe Animations
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.element {
animation: fadeIn 0.5s ease-out;
}
Multi-Step Animations
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
@keyframes rainbow {
0% { color: red; }
17% { color: orange; }
33% { color: yellow; }
50% { color: green; }
67% { color: blue; }
83% { color: indigo; }
100% { color: violet; }
}
Animation Properties
/* Shorthand */
animation: name duration timing-function delay iteration-count direction fill-mode;
/* Individual */
animation-name: fadeIn;
animation-duration: 0.5s;
animation-timing-function: ease-out;
animation-delay: 0.2s;
animation-iteration-count: infinite; /* or a number */
animation-direction: alternate; /* normal, reverse, alternate, alternate-reverse */
animation-fill-mode: forwards; /* none, forwards, backwards, both */
animation-play-state: running; /* running, paused */
Practical Animation Examples
Fade In on Scroll
.fade-in {
opacity: 0;
transform: translateY(30px);
transition: opacity 0.6s ease, transform 0.6s ease;
}
.fade-in.visible {
opacity: 1;
transform: translateY(0);
}
Loading Spinner
@keyframes spin {
to { transform: rotate(360deg); }
}
.spinner {
width: 40px;
height: 40px;
border: 3px solid #333;
border-top-color: #6366f1;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
Skeleton Loading
@keyframes shimmer {
0% { background-position: -200% 0; }
100% { background-position: 200% 0; }
}
.skeleton {
background: linear-gradient(90deg, #1a1a2e 25%, #2a2a4e 50%, #1a1a2e 75%);
background-size: 200% 100%;
animation: shimmer 1.5s infinite;
border-radius: 4px;
}
Bounce Effect
@keyframes bounce {
0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
40% { transform: translateY(-20px); }
60% { transform: translateY(-10px); }
}
.bounce { animation: bounce 1s ease infinite; }
Performance Optimization
The Golden Rule
Only animate these properties for smooth 60fps animations:
transform(translate, rotate, scale)opacity
These are handled by the GPU compositor and don't trigger layout or paint.
Properties to AVOID Animating
/* ❌ Triggers layout (very expensive) */
width, height, margin, padding, top, left, right, bottom, font-size
/* ❌ Triggers paint (expensive) */
background-color, color, border-color, box-shadow, border-radius
/* ✅ Compositor only (cheap) */
transform, opacity, filter
Instead of Animating Width/Height
/* ❌ Bad — animates width */
.element { width: 100px; transition: width 0.3s; }
.element:hover { width: 200px; }
/* ✅ Good — animates transform */
.element { transform: scaleX(1); transition: transform 0.3s; }
.element:hover { transform: scaleX(2); }
will-change
/* Tell the browser to optimize for upcoming animation */
.will-animate {
will-change: transform, opacity;
}
/* Remove after animation completes to free resources */
prefers-reduced-motion
Always respect user preferences:
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
Design your animations visually with our CSS Animation Generator tool.
Related Tools
- CSS Animation Generator — Create keyframe animations visually
- CSS Gradient Generator — Animated gradient backgrounds
- CSS Box Shadow Generator — Shadow hover effects
- CSS Flexbox Playground — Layout for animated elements
- Color Converter — Convert colors for animations
- CSS Minifier — Minify animation CSS for production
css animationkeyframescss transitionanimation performancecss effectsloading animationhover effect