Transitions & Animations
transition, @keyframes, animation
Skills taught: css.transitions, css.animations, css.keyframes
CSS Transitions
.button {
background: #4fd1c5;
color: #0e1116;
padding: 12px 24px;
border-radius: 8px;
border: none;
cursor: pointer;
/* transition: property duration easing delay */
transition: background 200ms ease;
/* Multiple properties */
transition:
background 200ms ease,
transform 150ms ease,
box-shadow 200ms ease;
/* All properties (use sparingly — expensive) */
transition: all 200ms ease;
}
.button:hover {
background: #38b2ac;
transform: translateY(-2px);
box-shadow: 0 8px 20px rgba(79, 209, 197, 0.3);
}
.button:active {
transform: translateY(0);
}
Easing functions
/* Keyword easings */
transition: all 300ms ease; /* slow start and end (default) */
transition: all 300ms ease-in; /* slow start */
transition: all 300ms ease-out; /* slow end — feels natural */
transition: all 300ms ease-in-out; /* slow start and end */
transition: all 300ms linear; /* constant speed */
/* Custom cubic-bezier */
transition: all 300ms cubic-bezier(0.34, 1.56, 0.64, 1); /* spring bounce */
/* Modern: spring-like step functions */
transition: all 400ms steps(4, end);
CSS Animations with @keyframes
/* Define the animation sequence */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(8px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
/* Apply animations */
.card {
animation: fadeIn 300ms ease-out;
animation-fill-mode: both; /* keep final state after animation ends */
}
.loader {
animation: spin 1s linear infinite;
}
.badge {
animation: pulse 2s ease-in-out infinite;
}
Performance — animate transform and opacity
/* ✅ GPU-accelerated — smooth 60fps */
.good:hover {
transform: translateX(8px);
opacity: 0.8;
}
/* ❌ Forces layout recalculation — janky */
.bad:hover {
margin-left: 8px; /* triggers layout */
width: 110%; /* triggers layout */
}
Warning: Always check prefers-reduced-motion: wrap all non-essential animations in @media (prefers-reduced-motion: no-preference) { ... }. Some users experience motion sickness from animations.
Frequently Asked Questions
What is the difference between transition and animation?
transition animates between two states triggered by a change (hover, focus, class toggle). animation runs a @keyframes sequence independently — it can loop, have multiple steps, and start automatically without a trigger.
What does animation-fill-mode: both do?
both applies the animation's first keyframe before it starts (forwards fills) and keeps the last keyframe after it ends (backwards fills). Without it, elements snap back to their original state after the animation completes.
What is transform: translateY(-3px) and why is it used for hover effects?
translateY(-3px) moves the element 3px upward. It is used on hover to create a 'lift' effect that feels tactile. It is GPU-accelerated (unlike margin-top), so it runs at 60fps with no layout cost.