KodingKaro
CSS · Intermediate · CSS Transitions and Animations

Keyframe animations and reduced motion

Keyframes describe multiple named points in an animation. They are useful for repeated or multi-step effects, but movement should never be required to use a page.

✓ What you will learn in this topic

  • Explain the key idea: Use keyframes for purposeful sequences and respect motion preferences.
  • Apply the concept: The card fades in and moves a short distance, unless the visitor has requested reduced motion.
  • Avoid this common mistake:

Concept explanation

@keyframes defines frames at percentages or from/to positions. The animation shorthand assigns a name, duration, timing, delay, iteration count, and fill behavior. Infinite animation is distracting unless it communicates an ongoing state that users can control.

For users who request reduced motion, remove or replace nonessential animation. When motion conveys important information, provide a non-motion cue too.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

@keyframes arrive {
  from { opacity: 0; transform: translateY(0.5rem); }
  to { opacity: 1; transform: translateY(0); }
}
.demo {
  animation: arrive 240ms ease-out both;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
@keyframes arrive {
  from { opacity: 0; transform: translateY(0.5rem); }
  to { opacity: 1; transform: translateY(0); }
}
.demo {
  animation: arrive 240ms ease-out both;
}

Expected result: The card fades in and moves a short distance, unless the visitor has requested reduced motion.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card {
  animation: arrive 240ms ease-out both;
}
@media (prefers-reduced-motion: reduce) {
  .card { animation: none; }
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Animating layout-heavy properties or essential content indefinitely can create jank and discomfort.

Best practice

Prefer transform and opacity for motion, keep durations purposeful, and honor reduced-motion preferences.

Practice exercise

Add a short hover and focus transition to a button, then disable it for users who request reduced motion. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Use keyframes for purposeful sequences and respect motion preferences.
  • Prefer transform and opacity for motion, keep durations purposeful, and honor reduced-motion preferences.

Knowledge check

What should a page do with nonessential animation for reduced-motion users?

Reveal the explanation

Respect the preference by removing or replacing motion that is not necessary.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
@keyframes arrive {
  from { opacity: 0; transform: translateY(0.5rem); }
  to { opacity: 1; transform: translateY(0); }
}
.demo {
  animation: arrive 240ms ease-out both;
}

Expected result:

The card fades in and moves a short distance, unless the visitor has requested reduced motion.

Hands-on practice task

Add a short hover and focus transition to a button, then disable it for users who request reduced motion.

In this section

Other lessons in CSS Transitions and Animations

Broaden your knowledge

Related learning topics