โšก Advanced Techniques

CSS Animations and Transitions: Smooth Motion Without JavaScript

By The CodeCraft Teamยทยท9 min read
Animated card hover effect demonstrating CSS transitions

Transitions animate between two states (hover on/off). Animations run a sequence of keyframes you define. Same engine under the hood โ€” different tools for different jobs.

Transitions in 60 seconds

.btn {
  background: #3b82f6;
  transition: background 200ms ease, transform 200ms ease;
}
.btn:hover { background: #2563eb; transform: translateY(-2px); }

Animations with @keyframes

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.card { animation: fadeInUp 300ms ease-out both; }

Animate only cheap properties

  • โœ… transform โ€” moves on the GPU
  • โœ… opacity โ€” composited
  • โš ๏ธ background-color โ€” fine for small areas
  • โŒ width / height / top / left โ€” triggers layout

Easing curves that don't feel robotic

  • ease-out โ€” UI entrances feel snappy
  • ease-in โ€” exits and dismissals
  • cubic-bezier(0.16, 1, 0.3, 1) โ€” modern 'spring' feel

Respect prefers-reduced-motion

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

Common mistakes

  • Animating non-transform properties
  • Using too-long durations (200โ€“300ms is plenty)
  • Forgetting reduced-motion users
  • Stacking multiple animations on the same element

Frequently asked questions

Transition or animation โ€” which should I use?

Transition for state changes (hover, focus). Animation for anything that runs on its own (loading spinner, entrance).

What's a good default duration?

Between 150ms and 300ms for UI. Longer feels sluggish; shorter feels jumpy.

Enjoyed this article?

Share it with a fellow developer or explore more tutorials in our blog.

More articles