On this page10 sections
  1. Learning Objectives
  2. 1. Transitions
  3. 2. Transforms
  4. 3. Keyframe Animations
  5. 4. A Fade-in on Load
  6. 5. Reduced Motion
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 037: Transitions & Animations

Category7: Advanced Styling
Lecture037 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelIntermediate

A button that changes colour instantly feels abrupt; one that fades in 0.2 seconds feels polished. Transitions smooth the change between two states. Animations run a sequence of steps by themselves, such as a spinner or a pulsing badge.


Learning Objectives

  • Smooth state changes with transition
  • Move, scale and rotate elements with transform
  • Write keyframe animations with @keyframes and animation
  • Respect visitors who prefer reduced motion

1. Transitions

transition: property duration timing;

Example: hover over the button:

html
<style>
  .btn {
    background: #2f9e44; color: white; border: 0; padding: 12px 22px; border-radius: 8px; cursor: pointer;
    transition: background 0.25s ease, transform 0.25s ease;
  }
  .btn:hover { background: #2b8a3e; transform: translateY(-3px); }
</style>

<button class="btn">Hover me</button>

The transition goes on the normal state, so it runs both on the way in and on the way out.

TimingFeel
easeStarts fast, slows down (the default)
linearConstant speed
ease-in-outSlow start and end

2. Transforms

transform moves or reshapes an element without affecting the layout around it.

Example: hover each box:

html
<style>
  .box { display: inline-block; width: 80px; padding: 20px 0; margin: 14px; text-align: center;
         background: #4c6ef5; color: white; border-radius: 8px; transition: transform 0.3s; }
  .move:hover   { transform: translateX(20px); }
  .grow:hover   { transform: scale(1.2); }
  .spin:hover   { transform: rotate(20deg); }
</style>

<div class="box move">move</div>
<div class="box grow">grow</div>
<div class="box spin">rotate</div>

Animating transform and opacity is smooth even on slow phones. Animating width or margin is not.


3. Keyframe Animations

Define the steps with @keyframes, then attach them with animation.

Example: a loading spinner and a pulsing dot:

html
<style>
  @keyframes spin  { to { transform: rotate(360deg); } }
  @keyframes pulse { 0%, 100% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.4); opacity: 0.5; } }

  .spinner { width: 36px; height: 36px; border: 4px solid #dee2e6; border-top-color: #1971c2;
             border-radius: 50%; animation: spin 0.8s linear infinite; }
  .dot { width: 14px; height: 14px; background: #e03131; border-radius: 50%; margin: 20px;
         animation: pulse 1.2s ease-in-out infinite; }
</style>

<div class="spinner"></div>
<div class="dot"></div>

animation: name duration timing iteration-count;. infinite repeats forever.


4. A Fade-in on Load

Example

html
<style>
  @keyframes fade-up { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
  .card { animation: fade-up 0.6s ease both; padding: 16px; border: 1px solid #dee2e6; border-radius: 10px; margin-bottom: 10px; }
  .card:nth-child(2) { animation-delay: 0.15s; }
  .card:nth-child(3) { animation-delay: 0.3s; }
</style>

<div class="card">First</div>
<div class="card">Second</div>
<div class="card">Third</div>

Press Run again to replay it.


5. Reduced Motion

Some people feel sick from moving screens and turn on "reduce motion" in their phone or computer settings. Respect it:

css
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

Common Beginner Mistakes

1. Transition on the :hover rule only. Then it animates in but snaps back. Put it on the normal state.

2. transition: all. It animates everything, including changes you didn't mean to animate. List the properties.

3. Slow animations. UI changes should take 0.15–0.3s. Anything longer feels sluggish.

4. Animating layout properties. width, top and margin stutter; transform and opacity stay smooth.


Practice Questions

  1. Which property smooths the change between two states?
  2. Where should the transition property go: on .btn or .btn:hover?
  3. Write a transform that makes an element 10% bigger.
  4. What two things do you need for a keyframe animation?
  5. How do you make an animation repeat forever?
  6. Why add a prefers-reduced-motion rule?

Practice Answers

  1. transition
  2. On .btn, the normal state, so it animates both ways.
  3. transform: scale(1.1);
  4. A @keyframes rule, and the animation property on the element.
  5. Add infinite to the animation value.
  6. To turn off motion for visitors who have asked their device to reduce it.

Key Takeaways

  • transition smooths changes between states; put it on the normal state.
  • transform moves, scales and rotates; animate transform and opacity for smoothness.
  • @keyframes + animation run multi-step animations on their own.
  • Keep it quick, and respect prefers-reduced-motion.