JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionscss
PrevNext

Learn the concept

Transitions & Keyframe Animations

css
mid
animations

What is the difference between CSS transitions and animations?

animations
transitions
keyframes
performance
motion
Quick Answer

Transitions animate between two states when a property changes (e.g., on hover), requiring a trigger. Animations use @keyframes to define multi-step sequences that can run automatically, loop infinitely, and control timing at each step independently.

Detailed Explanation

CSS Transitions:

  • Animate between two states (A → B)
  • Need a trigger (hover, focus, class change)
  • Properties: transition-property, transition-duration, transition-timing-function, transition-delay
  • Shorthand: transition: property duration easing delay

CSS Animations:

  • Define multi-step animations with @keyframes
  • Can run without triggers (on load)
  • Can loop, alternate, pause, and have complex timing
  • Properties: animation-name, animation-duration, animation-iteration-count, animation-direction, etc.

Performance tip: Only animate transform and opacity — they don't trigger layout recalculation (reflow). Avoid animating width, height, top, left, margin, padding.

When to use which:

  • Transitions: hover effects, toggle states, simple A↔B changes
  • Animations: loading spinners, attention seekers, entrance effects, complex sequences

Code Examples

TransitionsCSS
/* Simple hover transition */
.button {
  background: #3b82f6;
  color: white;
  transform: scale(1);
  transition: all 0.2s ease;
}
.button:hover {
  background: #2563eb;
  transform: scale(1.05);
}

/* Multiple properties with different timing */
.card {
  opacity: 0.8;
  transform: translateY(0);
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  transition:
    opacity 0.2s ease,
    transform 0.3s ease,
    box-shadow 0.3s ease;
}
.card:hover {
  opacity: 1;
  transform: translateY(-4px);
  box-shadow: 0 10px 30px rgba(0,0,0,0.15);
}

Real-World Applications

Use Cases

Implementing interactive UI elements

Such as buttons that smoothly change color or size on hover, or dropdown menus that gracefully slide open and close.

Creating engaging loading experiences

By designing custom loading spinners or progress bars that animate continuously while content is being fetched.

Building an animated onboarding flow

Where elements fade in, slide, or scale at different stages to guide the user through a new feature or application.

Mini Projects

Interactive Navigation Bar with Hover Effects

beginner

Develop a navigation bar where menu items have smooth transitions on hover and active states.

Animated Gallery with Image Captions

intermediate

Create an image gallery where captions animate in and out when images are hovered over, using CSS animations for complex effects.

Industry Examples

Apple

Apple's product websites extensively use CSS transitions and animations to create a polished and fluid user experience, with elements subtly animating into view or responding gracefully to user interactions.

Stripe

Stripe's payment forms and dashboard leverage subtle CSS animations and transitions to provide visual feedback to users, such as animated input fields on focus or loading indicators during form submission.

Resources

MDN - CSS Transitions

docs

MDN - CSS Animations

docs

Related Questions

How does CSS Grid work and how does it differ from Flexbox?

mid
grid
Previous
How does CSS Grid work and how does it differ from Flexbox?
Next
What are CSS custom properties (variables) and how do they differ from Sass variables?
PrevNext