Introduction to CSS Transitions & Animations
CSS transitions and animations allow you to create smooth, engaging visual effects without JavaScript. Transitions provide simple state changes when triggered by events like hover or click, while animations offer more complex multi-step movements and transformations. These powerful tools enhance user experience, provide visual feedback, and create more engaging interfaces.
Core Concepts & Properties
CSS Transitions
Transitions create smooth changes between property values over a specified duration.
Basic Transition Syntax:
.element {
transition-property: property;
transition-duration: time;
transition-timing-function: function;
transition-delay: time;
/* Shorthand */
transition: property duration timing-function delay;
}
Transition Properties:
| Property | Description | Example Values |
|---|---|---|
transition-property | Specifies which CSS properties to transition | all, opacity, transform, multiple values |
transition-duration | Duration of transition | 0.5s, 500ms |
transition-timing-function | How intermediate values are calculated | ease, linear, ease-in, ease-out, cubic-bezier() |
transition-delay | Delay before transition starts | 0s, 0.2s, 200ms |
transition | Shorthand for all properties | all 0.3s ease 0s |
Timing Function Visualization:
• linear – Constant speed • ease – Slow start, then fast, then slow end (default) • ease-in – Slow start, then fast end • ease-out – Fast start, then slow end • ease-in-out – Slow start and end, fast in middle • cubic-bezier(n,n,n,n) – Custom timing function
CSS Animations
Animations provide more control with multiple keyframes and repeating capabilities.
Basic Animation Syntax:
@keyframes animationName {
0% {
/* CSS properties */
}
50% {
/* CSS properties */
}
100% {
/* CSS properties */
}
}
.element {
animation-name: animationName;
animation-duration: time;
animation-timing-function: function;
animation-delay: time;
animation-iteration-count: number | infinite;
animation-direction: normal | reverse | alternate | alternate-reverse;
animation-fill-mode: none | forwards | backwards | both;
animation-play-state: running | paused;
/* Shorthand */
animation: name duration timing-function delay iteration-count direction fill-mode play-state;
}
Animation Properties:
| Property | Description | Example Values |
|---|---|---|
animation-name | Name of the @keyframes rule | fadeIn, slideIn |
animation-duration | Duration of one animation cycle | 1s, 500ms |
animation-timing-function | How intermediate values are calculated | ease, linear, steps(4, end) |
animation-delay | Delay before animation starts | 0s, 2s |
animation-iteration-count | Number of times to run | 1, 3, infinite |
animation-direction | Direction of animation | normal, reverse, alternate, alternate-reverse |
animation-fill-mode | Style applied before/after animation | none, forwards, backwards, both |
animation-play-state | Whether animation is running or paused | running, paused |
animation | Shorthand for all properties | fadeIn 1s ease 0s infinite normal forwards |
Transitions vs. Animations: When to Use Each
| Feature | Transitions | Animations |
|---|---|---|
| Trigger | Requires state change (hover, focus, etc.) | Automatically or by adding class |
| Complexity | Simple A to B changes | Multiple steps with keyframes |
| Control | Limited control points | Multiple keyframes for precise control |
| Looping | No built-in looping | Can loop with animation-iteration-count |
| Autoplay | Only on state change | Can start automatically |
| Direction | Forward only | Can be reversed, alternated |
| Best for | Simple hover effects, simple feedback | Complex movements, multi-step animations |
Common Transition Examples
Hover Effects
/* Button hover effect */
.button {
background-color: #3498db;
color: white;
padding: 10px 20px;
transition: background-color 0.3s ease, transform 0.2s ease;
}
.button:hover {
background-color: #2980b9;
transform: scale(1.05);
}
/* Image zoom effect */
.image-container {
overflow: hidden;
}
.image-container img {
transition: transform 0.5s ease;
}
.image-container:hover img {
transform: scale(1.1);
}
Transition Multiple Properties
.card {
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
transform: translateY(0);
opacity: 0.9;
transition: box-shadow 0.3s ease,
transform 0.3s ease-out,
opacity 0.2s ease;
}
.card:hover {
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
transform: translateY(-5px);
opacity: 1;
}
Menu Dropdown with Transitions
.dropdown-menu {
max-height: 0;
overflow: hidden;
opacity: 0;
transition: max-height 0.4s ease-out, opacity 0.3s ease;
}
.dropdown:hover .dropdown-menu {
max-height: 500px;
opacity: 1;
}
Common Animation Examples
Basic Fade In
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.fade-element {
animation: fadeIn 1s ease forwards;
}
Bounce Effect
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-30px);
}
60% {
transform: translateY(-15px);
}
}
.bounce-element {
animation: bounce 2s infinite;
}
Loading Spinner
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
}
Multi-Step Animation
@keyframes pulse-and-scale {
0% {
transform: scale(1);
background-color: #3498db;
}
50% {
transform: scale(1.1);
background-color: #e74c3c;
}
100% {
transform: scale(1);
background-color: #3498db;
}
}
.pulsing-element {
animation: pulse-and-scale 2s ease infinite;
}
Advanced Animation Techniques
Animating Along a Path (with offset-path)
@keyframes move-along-path {
0% {
offset-distance: 0%;
}
100% {
offset-distance: 100%;
}
}
.path-element {
offset-path: path('M10,80 Q50,10 90,80');
offset-rotate: 0deg;
animation: move-along-path 3s linear infinite;
}
Staggered Animations
.stagger-item:nth-child(1) {
animation-delay: 0s;
}
.stagger-item:nth-child(2) {
animation-delay: 0.2s;
}
.stagger-item:nth-child(3) {
animation-delay: 0.4s;
}
.stagger-item:nth-child(4) {
animation-delay: 0.6s;
}
Parallax Effect with CSS
.parallax-container {
perspective: 1px;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
}
.parallax-layer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.parallax-back {
transform: translateZ(-1px) scale(2);
}
.parallax-front {
transform: translateZ(0);
}
Animation Libraries & Keyframe Collections
Common Animation Patterns
/* Fade in up */
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Shake */
@keyframes shake {
0%, 100% {transform: translateX(0);}
10%, 30%, 50%, 70%, 90% {transform: translateX(-10px);}
20%, 40%, 60%, 80% {transform: translateX(10px);}
}
/* Pulse */
@keyframes pulse {
0% {transform: scale(1);}
50% {transform: scale(1.1);}
100% {transform: scale(1);}
}
/* Flip */
@keyframes flip {
0% {transform: perspective(400px) rotateY(0);}
100% {transform: perspective(400px) rotateY(360deg);}
}
Common Challenges & Solutions
Preventing Animation on Page Load
/* Add this class when you want to animate */
.animate {
animation: slideIn 1s ease forwards;
}
/* Alternative approach with JavaScript */
document.addEventListener('DOMContentLoaded', function() {
document.body.classList.add('content-loaded');
});
.content-loaded .element {
animation: fadeIn 1s ease;
}
Handling Animation End
/* CSS part */
.element {
animation: fadeOut 1s ease forwards;
}
/* JavaScript part */
const element = document.querySelector('.element');
element.addEventListener('animationend', function() {
// Do something when animation ends
element.style.display = 'none';
});
Reducing Animation for Users with Vestibular Disorders
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.001s !important;
transition-duration: 0.001s !important;
}
}
Performance Optimization
Properties That Animate Efficiently
• transform – scale, rotate, translate • opacity • filter
These properties don’t trigger layout recalculation and can be hardware-accelerated.
Forcing GPU Acceleration
.hardware-accelerated {
transform: translateZ(0);
will-change: transform, opacity;
}
Use will-change sparingly:
.element:hover {
will-change: transform;
}
.element:active {
transform: scale(1.1);
}
Best Practices & Tips
• Keep animations under 300ms for responsive UI feedback • Aim for 60fps animations (each frame = ~16ms) • Limit animated elements on screen at one time • Use transform and opacity when possible for better performance • Test on multiple devices to ensure smooth performance • Implement prefers-reduced-motion media query for accessibility • Use animation-fill-mode: forwards to maintain end state • Consider using JavaScript for complex, interactive animations • Combine transitions with classes added via JavaScript for controlled timing • Use DevTools Animation Inspector to debug complex animations
CSS Variables with Animations
:root {
--animation-duration: 0.3s;
--animation-easing: ease-in-out;
--hover-scale: 1.05;
}
.button {
transition: transform var(--animation-duration) var(--animation-easing);
}
.button:hover {
transform: scale(var(--hover-scale));
}
/* Responsive adjustments */
@media (max-width: 768px) {
:root {
--animation-duration: 0.2s;
--hover-scale: 1.03;
}
}
CSS Animation Events (JavaScript)
const element = document.querySelector('.animated-element');
element.addEventListener('animationstart', (e) => {
console.log(`Animation ${e.animationName} started`);
});
element.addEventListener('animationend', (e) => {
console.log(`Animation ${e.animationName} ended`);
});
element.addEventListener('animationiteration', (e) => {
console.log(`Animation ${e.animationName} iteration`);
});
// For transitions
element.addEventListener('transitionend', (e) => {
console.log(`Transition of property ${e.propertyName} ended`);
});
Resources for Further Learning
• Documentation:
• Tools:
- Animista – CSS animation library
- Cubic Bezier – Visual bezier curve creator
- Keyframes.app – Visual animation generator
• Animation Libraries:
- Animate.css – Ready-to-use animation library
- Magic Animations – CSS3 animations with special effects
- Hover.css – Collection of hover effects
• Articles:
- “High Performance Animations” on web.dev
- “Designing Meaningful Animation” by Val Head
- “Animation Performance 101” by Paul Lewis
Remember that animations should enhance user experience, not distract from it. Use animations purposefully to guide users, provide feedback, and create delight.
