CSS Transitions
CSS Transitions provide smooth animated changes between CSS property values over a specified duration, allowing elements to gradually change from one state to another with customizable timing functions.
How do CSS transitions work?
A transition animates a property smoothly when its value changes, for example on :hover. Declare it on the element's normal state: transition: background-color 0.2s ease;. Then any change to that property, from a pseudo-class, a class toggle or JavaScript, animates instead of jumping.
.button {
background: #3b82f6;
transition: background-color .2s ease, transform .2s ease;
}
.button:hover {
background: #2563eb;
transform: translateY(-2px);
}Interactive CSS Transitions Demo
Code Editor
<button class="btn">Hover me</button>
<div class="card">Hover the card</div>Preview
Transition Fundamentals
CSS transitions create smooth animations between property changes. They're essential for modern web interfaces and provide visual feedback for user interactions.
Complete Transition Syntax
transition: property duration timing-function delay;
transition: background-color 0.3s ease 0.1s;
transition: all 0.3s ease; /* Shorthand for all properties */Property: Which CSS property to animate
Duration: How long the transition takes
Timing Function: Speed curve of the transition
Delay: When to start the transition
Timing Functions Explained
Timing functions control the acceleration and deceleration of transitions, creating different feels and personalities for your animations.
ease (default)
Slow start, fast middle, slow end
cubic-bezier(0.25, 0.1, 0.25, 1)linear
Constant speed throughout
cubic-bezier(0, 0, 1, 1)ease-in-out
Slow start and end, fast middle
cubic-bezier(0.42, 0, 0.58, 1)Create custom timing with cubic-bezier(). Tools like cubic-bezier.com help visualize and create custom easing curves for unique animation personalities.
Advanced Transition Patterns
Multiple Properties
Animate different properties with different timings for complex effects.
transition:
background 0.3s ease,
transform 0.5s ease-out,
box-shadow 0.2s ease;Transition Delays
Stagger animations by adding delays to create sequential effects.
transition:
opacity 0.3s ease 0.1s,
transform 0.3s ease 0.2s;Performance & Best Practices
Best Practices
- • Prefer transform and opacity for performance
- • Keep durations under 500ms for UI feedback
- • Use ease-out for exits, ease-in for entrances
- • Test on slower devices and connections
- • Provide fallbacks for older browsers
Performance Tips
- • Use will-change for better optimization
- • Avoid animating layout properties (width, height)
- • Prefer transform over position changes
- • Use transform3d() for hardware acceleration
- • Remove transitions when not needed
CSS Transitions — step by step
transition Shorthand
Use transition: property duration timing-function delay to smoothly animate changes on a property when triggered by state changes like hover.
🎯 Quick Check
Which timing function produces a constant speed throughout the transition?
CSS Transitions: summary
Key points
- The shorthand order is property, duration, timing function, delay:
transition: opacity .3s ease-in 0.1s. - List specific properties rather than
transition: all, which can animate things you didn't intend and costs performance. - Animate
transformandopacityfor smooth 60fps motion; animatingwidth,heightortoptriggers layout. height: autocan't be transitioned directly. Usegrid-template-rows: 0fr → 1frorinterpolate-size: allow-keywordswhere supported.- Respect
prefers-reduced-motionby shortening or removing transitions.
Common mistakes
- Putting the
transitiononly on:hover, so the effect animates in but snaps back out. - Transitioning
display: none. Useopacityandvisibility, ortransition-behavior: allow-discretein modern browsers.
Frequently asked questions
›What is the difference between transition and animation?
A transition animates between two states when a property changes, such as on hover. An animation uses @keyframes to run multi-step sequences that can start on their own, loop and alternate.
›Why is my CSS transition not working?
Check that the property is animatable (display and auto values aren't, by default), that the transition is on the base state, and that the value actually changes.
›What does ease-in-out mean?
It's a timing function: the change starts slowly, speeds up, then slows down at the end. ease-out (fast then slow) usually feels best for elements entering the screen.