Frontend Master

JavaScript Curriculum

Transitions and Animations
+65 XP

Transitions and Animations

medium
~35 min·65 XP

The coffee shop site looks great but feels static. Hovering a card, clicking a button, loading the page — these moments deserve motion. Let's add polish without going overboard.

Transitions and Animations

CSS motion is about communication — a hover state that responds, a card that lifts, a modal that slides in. Done well, it makes your UI feel alive. Done poorly, it feels distracting. The key is restraint: smooth, purposeful, fast.

Transitions — Smooth State Changes

A transition animates between two CSS states (like :hover). You define what to animate, how long, with what easing, and after what delay:

transition-builder.css
property
duration
easing
delay

Slow start, fast middle, slow end. Natural feel.

Hover to preview
Hover me →
.button {
  transition: all 300ms ease;
}
.button:hover {
  background: #1f6feb;
  color: white;
  transform: translateY(-4px) scale(1.02);
  box-shadow: 0 8px 24px rgba(31,111,235,0.4);
}
💡The four transition properties
transition is shorthand for: property (what to animate), duration (how long), timing-function (the curve), and delay (wait before starting). The most important rule: keep durations short. 200–300ms for hovers, 400–600ms for entrances. Anything over 1s feels broken.

@keyframes — Full Animations

When you need more than two states, @keyframes lets you define an animation timeline with as many steps as you need:

keyframe-studio.css

opacity: 0 → 1. The most common entrance animation.

☕ The Grind
@keyframes
@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}
Usage
.element {
  animation: fadeIn 600ms ease-out forwards;
}
ℹ️animation shorthand
animation: name duration timing-function delay iteration-count direction fill-mode. The most important values: name (matches @keyframes), duration, and fill-mode: forwards (stay at the end state after finishing — almost always what you want for entrance animations).

Respecting Reduced Motion

Some users experience nausea, dizziness, or seizures from motion on screen. CSS gives us a media query to detect their preference:

reduced-motion.css
✗ Inaccessible

Animation plays regardless of user preferences. Fails WCAG 2.1 SC 2.3.3.

CSS
.hero-element {
  animation: slideIn 600ms ease-out;
}
/* No reduced-motion consideration */
⚠ Affects users with vestibular disorders, epilepsy, and motion sensitivity. WCAG 2.1 SC 2.3.3 (Level AAA) requires respecting this preference.
⚠️Always add reduced motion support
prefers-reduced-motion: reduce is set by users with vestibular disorders, epilepsy, and motion sensitivity. It takes 2 lines of CSS to respect it. There is no excuse not to. Make it a habit on every animation you write.

Your Challenge

Add purposeful motion to the coffee shop page with transitions, an entrance animation, and full reduced-motion support.

Challenge

Add transitions to all interactive elements on the coffee shop page: card hover (lift + shadow), button hover (colour shift), nav links (underline grows). Then add a fade-in slide-up entrance animation to the hero heading. Wrap all animations in prefers-reduced-motion: no-preference.

transitionanimationkeyframes@keyframestiming-functioneaseease-inease-outcubic-bezieranimation-durationanimation-delayanimation-iteration-countanimation-fill-modeprefers-reduced-motiontransformopacity
Transitions and Animations | Nexus Learn