Lesson 17 / Motion

CSS Animation

Time
55 min
Type
Reading + Interactive
Level
Intermediate

Use keyframes and animation properties to create smooth visual effects without JavaScript.

Teacher Notes / In-Class Use

Demo Live

  • Walk through the interactive demo before students start changing their own project files.
  • Connect the demo back to the first goal: Identify the key properties that control CSS animations

Try In Class

  • Build one fading animation with @keyframes.
  • Have students make one visible change, save, refresh, and explain what changed.

Submit Or Check

  • Ask students to show the work in the browser, not only in the editor.
  • Have students commit their progress with a clear message when the checkpoint is stable.

Watch For

  • Students copying code without checking file paths, spelling, or capitalization.
  • Visual changes that work locally but break when the project is published.

Learning Goals

  • Identify the key properties that control CSS animations
  • Create animations with @keyframes
  • Apply animations to elements and trigger them through interaction

Interactive Demo

How to use this demo.

Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.

What To Try

  • Switch between animation names and adjust duration, iteration count, and direction.
  • Replay the animation after changing one setting at a time.

What Changes

  • The target element follows a different keyframe sequence.
  • The code sample shows how animation settings connect to @keyframes.

What To Notice

  • Keyframes define what changes over time.
  • Animation settings control how often, how long, and in what direction the keyframes run.

Apply It

  • Create one small animation that supports meaning, feedback, or attention in your project.

Interactive Demo

Animation Keyframe Lab

Pick an animation, adjust its timing, and restart it. CSS animations use keyframes to define a reusable sequence of visual states.

Animate

fade / 900ms / once / normal

@keyframes fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

.element {
  animation: fade 900ms ease-in-out 1 normal both;
}

What to notice

  • Keyframes define the states of the animation.
  • The animation property applies the keyframes to an element.
  • Iteration and direction change how the sequence repeats.

Try this

  • Compare bounce with pulse.
  • Set iteration to infinite, then change direction to alternate.
  • Copy one @keyframes block into your own stylesheet.

This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source (opens in a new tab).

Introduction

CSS animations offer a powerful way to bring your web pages to life by adding dynamic and engaging effects.

By gradually changing the CSS properties of elements over time, you can create visually appealing and interactive experiences.

This guide explores the fundamentals of CSS animations, providing essential knowledge and tools to enhance your web projects.

Key Components of Animations

  • Animation name: Unique identifier for the animation.
  • Animation duration: Total time the animation takes to complete.
  • Animation timing function: Defines the speed curve, such as linear or ease-in-out.
  • Animation delay: Wait time before the animation starts.
  • Animation iteration count: Number of times the animation repeats.
  • Animation direction: Direction of animation play, such as normal, reverse, or alternate.
  • Animation fill mode: Styles applied before and after the animation.

Creating Animations with @keyframes

The @keyframes rule defines the animation sequence. Set the percentage marks and apply style changes for each keyframe.

@keyframes fadeIn {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}

Applying Animations to Elements

Use the animation shorthand or its individual properties to attach animations to HTML elements.

.animated-element {
  animation: fadeIn 2s ease-in-out infinite;
}

Interactive Examples

  • Fading element
  • Bouncing ball
  • Bouncing text with rotation
  • Progress bar
  • Loading spinner

Fading Element

Use Case: Reveal hidden content or subtle transitions.

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

.fade-in {
  animation: fadeIn 600ms ease both;
}

Bouncing Ball

Use Case: Simulate physical motion or draw attention.

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-40px);
  }
}

.ball {
  animation: bounce 700ms ease-in-out infinite;
}

Bouncing Text with Rotation

@keyframes bounceRotate {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }

  50% {
    transform: translateY(-1rem) rotate(8deg);
  }
}

.animated-text {
  animation: bounceRotate 900ms ease-in-out infinite;
}

Progress Bar

@keyframes progress {
  from {
    transform: scaleX(0);
  }

  to {
    transform: scaleX(1);
  }
}

.progress-bar {
  transform-origin: left;
  animation: progress 2s ease forwards;
}

Loading Spinner

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.spinner {
  inline-size: 2rem;
  block-size: 2rem;
  border: 4px solid #ddd;
  border-top-color: #2f9e44;
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

Advanced Techniques

CSS animations can be combined with transforms, 3D space, and JavaScript to create more interactive designs.

3D Transformations and Animations

By combining 3D transforms with animations, you can create immersive and visually striking effects.

Example: 3D Card Flip

This setup enables a smooth card flipping effect when hovered, simulating a 3D transformation using perspective and preserve-3d.

.card-scene {
  perspective: 800px;
}

.card {
  transform-style: preserve-3d;
  transition: transform 400ms ease;
}

.card:hover {
  transform: rotateY(180deg);
}

.card-face {
  backface-visibility: hidden;
}

Keyframe Animations with JavaScript

JavaScript can be used to dynamically control CSS animations, allowing for more interactive designs.

Example: Dynamically Triggering Animations

This method allows for animations that are triggered by user interactions, adding an extra layer of interactivity to your site.

const button = document.querySelector('.animation-button');
const box = document.querySelector('.animated-box');

button.addEventListener('click', () => {
  box.classList.remove('is-animating');

  requestAnimationFrame(() => {
    box.classList.add('is-animating');
  });
});

Conclusion

CSS animations allow developers to create smooth, interactive visual effects without using JavaScript.

Mastering animations opens up a wide range of creative possibilities in user interface design.

Checkpoint

Before moving on, make sure these feel true.

  • I can explain the main concept in my own words.
  • I can apply this lesson to my current project.
  • I can verify the result in the browser.
  • I can commit the change with a clear message.

Project Connection

This lesson supports current class projects.

Practice

  • Build one fading animation with @keyframes.
  • Create one looping animation such as a spinner or bouncing object.
  • Trigger an animation with a class change.
  • Explain what user feedback the animation provides.