DEV Community

Cover image for CSS Transitions and Animations Basics: Bringing Your UI to Life
Sharique Siddiqui
Sharique Siddiqui

Posted on

CSS Transitions and Animations Basics: Bringing Your UI to Life

Static web pages are functional, but adding motion can make an interface feel smoother, more engaging, and intuitive. With CSS transitions and animations, you can create hover effects, loading spinners, sliding menus, or even playful interactions—all without relying on JavaScript.

Let’s explore how transitions and animations work, their key differences, and some simple but effective examples.

CSS Transitions

What Are Transitions?

A transition lets you smoothly change a CSS property’s value over time, usually triggered by user interaction (hover, focus, click, etc.).

css
.box {
  width: 100px;
  height: 100px;
  background: skyblue;
  transition: all 0.5s ease;
}

.box:hover {
  width: 150px;
  background: coral;
}
Enter fullscreen mode Exit fullscreen mode
Explanation:
  • transition: Defines what properties change and how.
  • 0.5s: Duration of the animation.
  • ease: Timing function for acceleration (others: linear, ease-in, ease-out).

When hovered, the box smoothly expands instead of changing abruptly.

Transition Properties

The transition shorthand can include:

css
transition: [property] [duration] [timing-function] [delay];
Enter fullscreen mode Exit fullscreen mode
Example:
css
transition: background-color 0.3s ease-in-out 0.1s;
Enter fullscreen mode Exit fullscreen mode
  • Property: What to animate (background-color, transform, etc.).
  • Duration: How long it runs.
  • Timing function: Controls acceleration (ease, linear, cubic-bezier).
  • Delay: Wait time before starting.

CSS Animations

What Are Animations?

While transitions depend on user interaction, animations use keyframes and can run automatically, repeatedly, or infinitely.

css
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-50px); }
}

.ball {
  width: 50px;
  height: 50px;
  background: tomato;
  border-radius: 50%;
  animation: bounce 1s ease-in-out infinite;
}
Enter fullscreen mode Exit fullscreen mode
Here:
  • @keyframes defines stages for the animation.
  • The ball moves up at 50% then back down at 100%.
  • infinite repeats endlessly.
Animation Properties
css
animation: name duration timing-function delay iteration-count direction fill-mode play-state;
Enter fullscreen mode Exit fullscreen mode
Example:
css
animation: slideIn 2s ease-out 0.5s 1 forwards;
Enter fullscreen mode Exit fullscreen mode
  • name: Keyframes name.
  • duration: Animation length.
  • iteration-count: infinite or a number.
  • direction: normal, reverse, alternate.
  • fill-mode: forwards lets the animation keep its final state.
  • play-state: Can pause/resume animations.

Transitions vs Animations

Feature Transitions Animations
Trigger User interaction/event Can run automatically
Control Start/End states only Multiple stages with keyframes
Complexity Simple effects (hover, fade) Advanced motion, loops, sequences

Use transitions for simple effects like hover changes.
Use animations for repeated or multi-step effects like loading spinners, bouncing balls, or complex UI motion.

Practical Examples

1. Button Hover with Transition
css
button {
  background: #0077cc;
  color: white;
  padding: 10px 20px;
  border: none;
  transition: background 0.3s, transform 0.3s;
}
button:hover {
  background: #005fa3;
  transform: scale(1.05);
}
Enter fullscreen mode Exit fullscreen mode
2. Loading Spinner with Animation
css
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.loader {
  width: 40px;
  height: 40px;
  border: 4px solid #ccc;
  border-top: 4px solid #0077cc;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Keep animations subtle—avoid overwhelming users.
  • Use hardware-accelerated properties (transform, opacity) for smoother performance.
  • Keep performance in mind; too many animations can slow down pages.
  • Always ensure animations don’t cause accessibility issues (e.g., allow users to disable motion if needed).

Final Thoughts

CSS transitions and animations make your interfaces more dynamic, engaging, and intuitive.

  • Start with transitions for simple hover/focus effects.
  • Use @keyframes animations when you need looping or multi-step motion.

Mastering these basics opens the door to more advanced microinteractions that can bring a site to life without extra JavaScript overhead.

Check out the YouTube Playlist for great CSS content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ...CodenCloud

Top comments (0)