Creating Stunning Animations with CSS

Mark HaverbekeMark Haverbeke
02 Jun, 2025
Creating Stunning Animations with CSS

TABLE OF CONTENTS

Introduction to CSS Animations and Transitions

Using transition, transform, and animation

Keyframes and @keyframes Syntax

Hover, Click, and Scroll-Based Effects

Chaining Animations and Easing Functions

Performance Considerations

Real-World Animation Examples

Conclusion

Introduction to CSS Animations and Transitions

CSS animations allow elements to gradually change from one style to another. Instead of making abrupt style changes (like color or position), you can animate these properties over time using CSS.

There are two main types:

  • Transitions – Triggered by user actions (like hover or click).
  • Animations – Run automatically or based on keyframes.

Both are defined in your stylesheets, no JavaScript required.

Using transition, transform, and animation

transition

The transition property enables smooth interpolation between CSS property changes.

button {
  transition: background-color 0.3s ease-in-out;
}

transform

The transform property allows movement, rotation, scaling, and skewing of elements.

button:hover {
  transform: scale(1.1);
}

animation

The animation property gives you complete control using keyframes.

div {
  animation: fadeIn 2s ease-in;
}

Keyframes and @keyframes Syntax

To create more complex animations, define keyframes with @keyframes:

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

You can then assign this animation to any element:

.modal {
  animation: fadeIn 0.5s ease-out forwards;
}

Hover, Click, and Scroll-Based Effects

Hover Effects

Simple hover transformations:

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

Click Effects

Use the :active pseudo-class for click animations:

.button:active {
  transform: scale(0.95);
}

Scroll Effects (with IntersectionObserver + CSS)

While CSS doesn’t support scroll triggers natively, you can combine it with JavaScript to add or remove classes on scroll, which then trigger CSS animations.

Chaining Animations and Easing Functions

Chaining Animations

Use animation-delay or transition timings to sequence multiple effects.

h1 {
  animation: slideIn 1s ease-out;
}
p {
  animation: fadeIn 1s ease-out;
  animation-delay: 0.5s;
}

Easing Functions

Define how the speed of the animation progresses:

  • ease-in: starts slow
  • ease-out: ends slow
  • ease-in-out: starts and ends slow
  • linear: consistent speed
box {
  transition: transform 0.5s ease-in-out;
}

Performance Considerations

While animations look great, overusing or misusing them can hurt performance. Keep in mind:

  • Prefer transform and opacity: These can be GPU-accelerated.
  • Avoid animating layout-affecting properties like width, height, or top.
  • Minimize excessive animations on mobile.
  • Use will-change to hint performance-critical animations.
.card {
  will-change: transform, opacity;
}

Real-World Animation Examples

Button Hover

.button {
  padding: 12px 24px;
  background: #007BFF;
  color: white;
  border: none;
  transition: background 0.3s ease, transform 0.2s;
}

.button:hover {
  background: #0056b3;
  transform: translateY(-3px);
}

Modal Fade-In

@keyframes modalFade {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.modal {
  animation: modalFade 0.4s ease-out;
}

Conclusion

CSS animations are a powerful and lightweight way to enhance your UI. Whether you're building interactive hover states or crafting elegant page transitions, CSS gives you the flexibility and control to do it all, without writing a single line of JavaScript.

By mastering properties like transform, transition, and @keyframes, and keeping performance in mind, you can craft modern interfaces that are responsive, smooth, and user-friendly.

Mark Haverbeke

Mark Haverbeke

Senior Frontend Engineer

Mark is a passionate software developer and author with expertise in JavaScript and Python. He enjoys simplifying complex programming concepts and sharing practical coding tips.