Skip to content
Kuza Ndoto Academy

Transitions & Animations

Beginner → IntermediateChapter 4 · Page 16 of 224 min readUpdated 15 Nov 2026

Movement helps people understand what's happening on screen: a button that gently changes colour feels responsive, and a loading spinner reassures users that something is working. CSS gives you two tools: transitions for smooth changes between two states, and keyframe animations for sequences that can run on their own.

Transitions

Without a transition, a change such as a hover colour happens instantly. A transition tells the browser to animate the change over time. You put it on the element's normal state, so it applies both when the change happens and when it reverses.

.btn {
  background-color: #1e3a8a;
  transition: background-color 0.3s ease;
}
.btn:hover {
  background-color: #2563eb;
}

Hovering now fades the background from dark to bright blue over 0.3 seconds, and moving away fades it back.

The transition shorthand takes up to four parts: the property to animate, the duration, the timing function and an optional delay. List several transitions separated by commas:

.card {
  transition: transform 0.2s ease-out, box-shadow 0.2s ease-out 0.05s;
}

You can write transition: all 0.3s, but naming properties is better: it avoids animating things you didn't intend and is easier for the browser.

Timing functions

The timing function controls how the speed changes during the animation. Each keyword is a curve showing progress against time:

linearconstant speed easedefault; gentle end ease-inslow start ease-outslow end ease-in-outslow at both ends Each box: time runs left to right, progress of the change runs bottom to top
Figure 1. The keyword curves, drawn from their official cubic-bézier values (for example ease-in is cubic-bezier(0.42, 0, 1, 1)). A steep part of the curve means fast movement; a flat part means slow.

ease-out suits things entering the screen (they arrive quickly and settle), while ease-in suits things leaving. For full control, write your own curve with cubic-bezier(), or use steps(4) for jumpy, frame-by-frame motion.

Keyframe animations

Transitions need a trigger and only go from A to B. Animations can have many steps, repeat, and start automatically. First define the steps with @keyframes, then attach them with the animation properties.

@keyframes pulse {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.live-badge {
  animation: pulse 1.5s ease-in-out infinite;
}

The badge gently grows by 8% and shrinks back every 1.5 seconds, forever. from and to can replace 0% and 100%.

PropertyExampleMeaning
animation-namepulseWhich @keyframes to use
animation-duration1.5sLength of one cycle
animation-timing-functionease-in-outSpeed curve, as for transitions
animation-delay0.5sWait before starting
animation-iteration-count3, infiniteHow many cycles
animation-directionalternatePlay forwards then backwards on alternate cycles
animation-fill-modeforwardsKeep the final keyframe's styles after finishing

Performance and accessibility

Browsers can animate transform and opacity very cheaply, often on the graphics chip, so they stay smooth even on budget phones. Animating width, height, top or margin forces the browser to recalculate the layout on every frame and can stutter. To move something, prefer transform: translate() over changing left. Keep interface transitions short, roughly 150 to 300 milliseconds, so the site feels quick.

Some people experience dizziness or nausea from on-screen motion and turn on "reduce motion" in their device settings. Respect it:

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Try it

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Motion</title>
  <style>
    body { font-family: system-ui, sans-serif; margin: 2rem; }
    .btn {
      background: #1e3a8a;
      color: white;
      border: none;
      padding: 0.75rem 1.5rem;
      border-radius: 8px;
      transition: background-color 0.3s ease, transform 0.2s ease-out;
    }
    .btn:hover {
      background: #2563eb;
      transform: translateY(-3px);
    }
    @keyframes spin {
      to { transform: rotate(360deg); }
    }
    .spinner {
      width: 40px;
      height: 40px;
      margin-top: 2rem;
      border: 5px solid #cbd5e1;
      border-top-color: #1e3a8a;
      border-radius: 50%;
      animation: spin 0.9s linear infinite;
    }
    @keyframes slide-in {
      from { opacity: 0; transform: translateX(-40px); }
      to   { opacity: 1; transform: translateX(0); }
    }
    .toast {
      margin-top: 2rem;
      padding: 1rem;
      background: #dcfce7;
      border-left: 4px solid #16a34a;
      animation: slide-in 0.6s ease-out 0.5s both;
    }
  </style>
</head>
<body>
  <button class="btn">Hover me</button>
  <div class="spinner"></div>
  <div class="toast">Payment of KES 500 received.</div>
</body>
</html>

The button lifts by 3px and brightens smoothly when you hover. Below it, a grey ring with a dark blue segment spins continuously at a steady speed. After a half-second pause, a green "Payment received" message fades in while sliding from the left. The both fill mode keeps it invisible during the delay and visible after it finishes.

Key points

  • transition: property duration timing-function delay animates a change between two states.
  • @keyframes plus animation creates multi-step, repeating or automatic motion.
  • Animate transform and opacity for smooth performance.
  • Honour prefers-reduced-motion, and keep interface motion short and purposeful.
Up next · Selectors, Cascade & EffectsTransformsMove, scale, rotate and skew elements.
→

Tip: use the ← and → keys to move between pages.