Transitions
The smoothest way to react to user interaction.
🤔 What Is a Transition?
Without a transition, a CSS property change happens instantly. A transition tells the browser: "don't snap, animate smoothly between these two values."
📝 The Syntax — 4 Parts
Which CSS property to animate.
Use
all to animate everything.How long the animation takes.
In seconds (s) or ms.
The speed curve. Controls how
it accelerates/decelerates.
Wait this long before starting.
Default is 0s.
Click each part above to learn about it.
⏱ Timing Functions — The Speed Curve
Same animation with different timing functions feels completely different. Hover each row or click run:
linear
ease
ease-in
ease-out
ease-in-out
cubic-bezier
⏳ Transition Delay — Staggered Menus
Delay creates a stagger effect. Hover the menu below:
💻 Section 1 Practice Exercises
Smooth Hover Button
- In
.ex1-btn, write atransitionrule targetingbackground-colorandborder-radius. - Set duration to
0.4sand timing function toease. - Click ▶ Test Code, then hover the button to see it transform smoothly!
✅ Solution Code:
transition: background-color 0.4s ease, border-radius 0.4s ease;
/* OR shorthand: */
transition: all 0.4s ease;
📖 Detailed Explanation:
The transition property specifies which CSS properties should change gradually rather than instantly when a state changes (e.g. :hover). Setting 0.4s defines the duration, and ease provides a smooth acceleration curve.
Staggered Card List
- Set
transition-delay: 0.1s;for.ex2-item:nth-child(2). - Set
transition-delay: 0.2s;for.ex2-item:nth-child(3). - Click ▶ Test Code and hover the container to watch cards lift one after another!
✅ Solution Code:
.ex2-item:nth-child(2) { transition-delay: 0.1s; }
.ex2-item:nth-child(3) { transition-delay: 0.2s; }
📖 Detailed Explanation:
transition-delay pauses the start of a transition by a set duration. Incrementing delays across sibling elements (0s, 0.1s, 0.2s) creates a cascading "stagger" effect without requiring JavaScript.
Transforms
Move, rotate, scale, skew — without affecting layout.
⚡ Why Transforms? The Performance Reason
Browser steps: Layout → Paint → Composite. Changing margin or top triggers all 3 — slow. transform triggers Composite only — 60fps GPU acceleration.
🔧 The 4 Transform Functions — Interactive
transform: translate(0px, 0px) scale(1) rotate(0deg) skewX(0deg);
🎯 transform-origin — The Pivot Point
💻 Section 2 Practice Exercises
Interactive Card Lift & Grow
- Inside
.ex3-card:hover, write atransformproperty. - Combine
translateY(-12px)(to move UP 12px) andscale(1.08)(to grow 8%). - Click ▶ Test Code and hover over the card to see the combined motion!
✅ Solution Code:
transform: translateY(-12px) scale(1.08);
📖 Detailed Explanation:
translateY(-12px) shifts the element upward along the vertical axis (negative values move up). scale(1.08) enlarges the element uniformly by 8%. Combining multiple functions in one transform property executes them smoothly on the GPU.
Door Swing Animation
- In
.ex4-door, settransform-origin: bottom left;to pin the hinge point. - In
.ex4-door:hover, settransform: rotate(-30deg);. - Click ▶ Test Code and hover the door to see it swing like a hinge!
✅ Solution Code:
.ex4-door { transform-origin: bottom left; }
.ex4-door:hover { transform: rotate(-30deg); }
📖 Detailed Explanation:
transform-origin changes the pivot coordinates of rotation or scaling. Changing it from default center center to bottom left forces rotation to pivot around the corner, producing a door hinge mechanism.
Keyframe Animations
Full control — no user interaction required.
📝 @keyframes — Defining the Steps
🎛️ The animation Property — All 6 Sub-Properties
animation-name
Name of @keyframes
animation-duration
Length of one cycle
animation-timing-function
Speed curve
animation-iteration-count
Repeats (1, infinite)
animation-direction
normal, reverse, alternate
animation-fill-mode
forwards, backwards, none
💻 Section 3 Practice Exercises
Create a Pulse Animation
- Locate the
50%keyframe block in@keyframes pulseEx. - Add
transform: scale(1.25);andopacity: 0.6;. - Click ▶ Test Code to watch the ball pulse continuously!
✅ Solution Code:
50% {
transform: scale(1.25);
opacity: 0.6;
}
📖 Detailed Explanation:
Keyframes interpolate values across key time percentages. Setting 0%, 100% to scale 1 and 50% to scale 1.25 with dim opacity creates a smooth pulsing heartbeat cycle.
Progress Fill with Fill-Mode
- In
.ex6-bar, write the shorthandanimationproperty. - Pass keyframe name
fillProgress, duration2s, timingease-out. - Add
forwardsas fill-mode so the bar stays 100% full when finished. - Click ▶ Run Animation to test!
✅ Solution Code:
animation: fillProgress 2s ease-out forwards;
📖 Detailed Explanation:
By default, animations snap back to their original state (width: 0%) upon completion. animation-fill-mode: forwards retains the computed styles of the last keyframe (width: 100%), keeping the progress bar filled.
Real World UI Patterns
Combine transitions, transforms, and keyframes.
🛍️ Product Card & UI Showcase
Premium Product
$49.99
💻 Section 4 Practice Exercises
Floating Action Button (FAB)
- In
.ex7-fab:hover, setbackground-color: #ef4444;(red). - Add
transform: rotate(45deg);to turn the+icon into an×icon. - Click ▶ Test Code and hover the button to see the morphing effect!
✅ Solution Code:
background-color: #ef4444;
transform: rotate(45deg);
📖 Detailed Explanation:
Rotating a + symbol by 45 degrees transforms it visually into a close × icon. Combining color transition and rotational transform inside hover states creates intuitive UI button feedback.
Interactive Shimmer Skeleton
- In
.ex8-skeleton, add the shorthandanimationproperty. - Set name
shimmerEx, duration1.5s, countinfinite, timinglinear. - Click ▶ Test Code to see the loading shimmer sweep across the bar!
✅ Solution Code:
animation: shimmerEx 1.5s infinite linear;
📖 Detailed Explanation:
Skeleton loaders animate a multi-stop gradient background across an element using background-position. Animating infinitely with linear speed gives a realistic content loading indicator.
Knowledge Check
Tap each card to reveal the answer.
transition shorthand?transform instead of margin-top?margin-top triggers layout + paint (jank).animation-fill-mode: forwards do?transition and @keyframes?transition: state-triggered (hover). @keyframes: auto-running, multi-step, looping.