JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicscssTransitions & Keyframe Animations
Next
css
intermediate
10 min read

Transitions & Keyframe Animations

animations
easing
gpu
keyframes
motion
performance
transitions
web-animations-api

CSS transitions animate between two states on property changes, while @keyframes animations define multi-step sequences with fine-grained control over timing, iteration, and direction — and only transform and opacity animations avoid costly layout recalculations.

Key Points

1Transitions vs Keyframes

Transitions animate between two states on a trigger (hover, class change). @keyframes define multi-step sequences that can loop, reverse, and run without a trigger.

2GPU-Friendly Properties

Only transform and opacity are composited on the GPU without triggering layout or paint — animating anything else (width, top, margin) causes expensive reflows.

3Timing and Easing

cubic-bezier() provides custom easing curves, steps() enables frame-by-frame animation, and animation-fill-mode controls what styles apply before and after playback.

4Choosing the Right Tool

Use transitions for simple hover/focus/toggle effects. Use @keyframes for loading spinners, complex sequences, and animations that need iteration or direction control.

What You'll Learn

  • Explain the difference between CSS transitions and @keyframes animations
  • Identify which CSS properties can be animated without triggering layout recalculation
  • Choose the appropriate animation approach for common UI patterns
  • Describe how timing functions and fill modes control animation behavior

Deep Dive

CSS provides two distinct animation mechanisms: transitions and @keyframes animations. Understanding both, knowing when to use each, and being aware of performance implications are common interview topics.

CSS Transitions

Transitions animate a property change from one value to another. They require a trigger (such as :hover, :focus, or a class toggle) and only animate between two states. You define them with transition-property, transition-duration, transition-timing-function, and transition-delay — or the shorthand transition: property duration timing delay.

Transitions are ideal for simple state changes: hover effects, focus styles, expanding panels, and toggling visibility. They are declarative and require no JavaScript for the animation itself.

@keyframes Animations

@keyframes let you define multi-step animation sequences with percentage-based waypoints (or from/to for two-step). You apply them with the animation shorthand, which accepts name, duration, timing function, delay, iteration count, direction, fill mode, and play state.

Key properties include animation-iteration-count (use infinite for looping), animation-direction (alternate for ping-pong effects), and animation-fill-mode (forwards keeps the final state, backwards applies the first keyframe during delay). Animations can run without a trigger and support complex multi-property choreography.

Performance Considerations

The browser rendering pipeline has four stages: Style, Layout, Paint, and Composite. Animating properties that trigger layout (like width, height, top, left, margin, or padding) forces the browser to recalculate geometry for the element and potentially its siblings. Animating properties that trigger paint (like background-color or box-shadow) is cheaper but still not ideal.

Only transform and opacity can be composited on the GPU without triggering layout or paint. This means animations using translateX() instead of left, or scale() instead of width, run at 60fps even on low-powered devices. The will-change property can hint the browser to promote an element to its own compositor layer, but overuse wastes memory.

Timing Functions

The cubic-bezier() function gives precise control over easing curves. Common presets are ease (default), ease-in, ease-out, ease-in-out, and linear. The steps() function creates frame-by-frame animations, useful for sprite sheets or typewriter effects.

When to Use Which

Use transitions for simple A-to-B state changes triggered by user interaction. Use @keyframes for complex multi-step animations, looping effects, animations that run on page load, or when you need directional control. In practice, most UI interactions use transitions, while loading spinners, skeleton screens, and attention-grabbing effects use keyframe animations.

The Web Animations API

For programmatic control, the Web Animations API (element.animate()) provides JavaScript access to the same engine that powers CSS animations. It returns an Animation object with methods like play(), pause(), reverse(), and finish(), plus a finished promise for sequencing.

Fun Fact

Early CSS had no animation support at all. WebKit (Safari's engine) first proposed CSS transitions in 2007, and @keyframes animations followed in 2009. Before that, all web animations required JavaScript or Flash.

Learn These First

CSS Selectors & Specificity

beginner

The CSS Box Model

beginner

Continue Learning

CSS Performance Optimization

advanced

CSS Custom Properties (Variables)

intermediate

Practice What You Learned

What is the difference between CSS transitions and animations?
mid
animations
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.
Next
The CSS Box Model
Next