CSS Animations & Transitions

Make interfaces feel alive — smooth hover transitions, and looping @keyframes animations. Grade on the real computed animation, so you know it actually moves.

1. Two tools for motion

Motion, used well, guides attention and makes an interface feel responsive and polished. CSS gives you two tools:

Transitions animate between two states. You set a transition, then change a property (usually on :hover or when a class is added), and the browser smoothly interpolates:

.button {
  transform: scale(1);
  transition: transform 0.2s ease;   /* smooth any change to transform */
}
.button:hover {
  transform: scale(1.06);            /* grows smoothly on hover */
}

Animations (@keyframes) define a multi-step sequence that can play automatically and loop:

@keyframes pulse {
  0%, 100% { transform: scale(1);    opacity: 1;   }
  50%      { transform: scale(1.15); opacity: 0.7; }
}
.badge {
  animation: pulse 1.2s ease-in-out infinite;
}

The animation shorthand is: name duration timing-function iteration-count. infinite loops forever.

2. Animate the right properties

For buttery-smooth 60fps animation, prefer transform (move/scale/rotate) and opacity. These are handled by the GPU and don’t force the browser to recalculate page layout. Animating width, height, top or left is expensive and can stutter.

/* smooth */   transform: translateY(-4px);
/* janky */    top: -4px;

3. Build hover + a pulsing badge

Add a smooth hover scale to the like button, and a looping pulse to the “NEW” badge. The grader checks the computed animation, so it only passes when motion is really applied.

Hover transition + pulsing badge
PREVIEW

Your tasks

  • ○.card has a transition (duration > 0)
  • ○.like has a transition (duration > 0)
  • ○.like transitions transform (or all)
  • ○.badge has a @keyframes animation applied
  • ○.badge animation loops infinitely
  • ○.badge animation has a duration > 0

4. What you learned

You can now add motion the way real products do: transitions for smooth state changes (hover, focus, toggles) and @keyframes animations for looping, multi-step movement — all driven by GPU-friendly transform and opacity so it stays smooth. Hover the like button in the preview and watch your badge pulse; that’s the exact CSS shipping on production sites.

Next, we collect input properly with forms and validation.

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for CSS Animations & Transitions. What would you like to know?
📝

Knowledge Check

1 of 3

What's the difference between a transition and an animation in CSS?

Report a problem

About: /lessons/css-animations-and-transitions