Transitions, Transforms & Keyframe Animations
Adding motion to web user interfaces elevates the user experience by providing visual feedback, guiding user attention, and creating polished interactions. CSS provides three core mechanisms for UI motion: Transforms, Transitions, and Keyframe Animations.
1. 2D & 3D CSS Transformsβ
The transform property allows you to visually manipulate an element's spatial geometry without disturbing the surrounding normal document flow (preventing unnecessary layout reflows).
[ CSS Transform Functions ]
β
ββββββββββββββββββ¬β βββββββββββ΄ββββββββββββ¬βββββββββββββββββ
βΌ βΌ βΌ βΌ
[ translate() ] [ scale() ] [ rotate() ] [ skew() ]
Moves element Resizes element Rotates element Tilts element along
along X/Y/Z axes relative to origin by angle deg X/Y axes
Common Transform Propertiesβ
.card {
/* Combine multiple transform operations */
transform: translateY(-8px) scale(1.03) rotate(1deg);
/* Change the origin point of transformation (Default: 50% 50%) */
transform-origin: top left;
}
Always prefer animating transform and opacity. Browsers offload these properties directly to the GPU (Graphics Processing Unit), avoiding expensive layout recalculations (reflow) and repaints.
2. CSS Transitionsβ
CSS Transitions enable smooth value changes over a specified duration when an element switches between different visual states (such as :hover, :focus, or JavaScript class toggles).
Shorthand Syntaxβ
.button {
background-color: #2563eb;
transform: scale(1);
/* Transition specific properties for optimal performance */
transition: background-color 0.3s ease, transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}
.button:hover {
background-color: #1d4ed8;
transform: scale(1.05);
}
Common Timing Functions (transition-timing-function)β
ease(default): Starts slow, speeds up, then slows down.linear: Constant speed from start to end.ease-in-out: Symmetric slow start and end.cubic-bezier(p1, p2, p3, p4): Custom acceleration curve for ultra-smooth UI motion.
3. Multi-Stage Keyframe Animationsβ
While transitions require a state change trigger (like :hover), @keyframes animations run automatically, can loop infinitely, and support complex multi-step sequences.
Defining Keyframes & Applying Animationsβ
/* 1. Define the Keyframe Sequence */
@keyframes pulseGlow {
0% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(37, 99, 235, 0.7);
}
50% {
transform: scale(1.05);
box-shadow: 0 0 20px 10px rgba(37, 99, 235, 0);
}
100% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(37, 99, 235, 0);
}
}
/* 2. Apply to Element */
.badge-live {
/* animation: name duration timing-function delay iteration-count direction fill-mode */
animation: pulseGlow 2s ease-in-out infinite;
}
Interactive Playground: Hardware-Accelerated Motionβ
Test combined transform state changes, transition curves, and continuous keyframe loops in the live editor below:
Summary Reference Tableβ
| Motion Tool | Primary Trigger | Complexity Level | GPU Accelerated? |
|---|---|---|---|
transform | Applied directly or via state | Spatial changes (move, rotate, scale) | Yes |
transition | State change (:hover, JS class) | A-to-B smooth state interpolation | Yes (for opacity/transform) |
@keyframes | Page load or class assignment | Multi-step dynamic sequence loops | Yes (for opacity/transform) |