← Learning Hub
Intermediate

Transitions, Transforms and Animations

A transition makes a change happen smoothly instead of instantly. Tell CSS which property to animate and how long it takes.

Motion helps users understand what changed: a button that gently lifts when hovered says "I'm clickable", a menu that slides in shows where it came from. Done subtly, animation makes an interface feel polished. Overdone, it's distracting and can even make some people feel unwell - so keep it short (0.2-0.3 seconds is typical) and purposeful.

Example
<!DOCTYPE html>
<html>
  <head>
    <style>
      .card {
        width: 180px; padding: 20px; background: #fff; border-radius: 12px;
        box-shadow: 0 2px 6px rgba(0,0,0,.1);
        transition: transform 0.25s ease, box-shadow 0.25s ease;
      }
      .card:hover {
        transform: translateY(-6px) scale(1.03);
        box-shadow: 0 12px 24px rgba(0,0,0,.18);
      }
    </style>
  </head>
  <body style="background:#f1f5f9; padding:30px">
    <div class="card">Hover me 👆</div>
  </body>
</html>
▶ Try it yourself

transform

  • translate(x, y) - move
  • scale(1.2) - grow or shrink
  • rotate(45deg) - turn
  • Several at once: transform: translateY(-6px) rotate(3deg);

Keyframe animations

For something that runs by itself or repeats, describe the steps with @keyframes and attach it with animation.

Example
<!DOCTYPE html>
<html>
  <head>
    <style>
      @keyframes pulse {
        0%   { transform: scale(1); }
        50%  { transform: scale(1.1); }
        100% { transform: scale(1); }
      }
      @keyframes spin { to { transform: rotate(360deg); } }

      .sale { display: inline-block; background: #dc2626; color: #fff; padding: 8px 14px;
              border-radius: 8px; animation: pulse 1.2s ease-in-out infinite; }
      .loader { width: 32px; height: 32px; border: 4px solid #e5e7eb; border-top-color: #7c3aed;
                border-radius: 50%; animation: spin 0.8s linear infinite; margin-top: 20px; }
    </style>
  </head>
  <body>
    <span class="sale">Flash sale!</span>
    <div class="loader"></div>
  </body>
</html>
▶ Try it yourself
Animate transform and opacity when you can - phones draw them smoothly. And respect users who turn motion off: wrap big animations in @media (prefers-reduced-motion: no-preference) { ... }.
Key points
  • transition animates changes between two states.
  • transform moves, scales and rotates efficiently.
  • @keyframes + animation for repeating or self-running motion.
  • Respect prefers-reduced-motion.

📝 Test yourself

1. Which property makes a hover change happen smoothly?
2. Which transform makes an element bigger?
3. Fill in the at-rule that describes animation steps:
@ spin { to { transform: rotate(360deg); } }