Transition Properties
Transition Properties
Basic Syntax
.button {
transition: property duration timing-function delay;
}
transition-property
Which properties to animate:
.button {
transition-property: background-color;
transition-property: all; /* All properties */
transition-property: transform, opacity; /* Multiple */
}
transition-duration
How long the transition takes:
.button {
transition-duration: 0.3s;
transition-duration: 300ms; /* Milliseconds */
transition-duration: 0.3s 0.5s; /* Different for each property */
}
transition-delay
When to start the transition:
.button {
transition-delay: 0.1s;
transition-delay: 0s 0.2s; /* Different for each property */
}
Shorthand
.button {
transition: background-color 0.3s ease;
transition: all 0.3s ease 0.1s;
transition: transform 0.2s ease, opacity 0.3s ease;
}
Triggering Transitions
.button {
background: blue;
transition: background 0.3s ease;
}
.button:hover {
background: darkblue;
}
.button:active {
transform: scale(0.98);
}
Transition Events (JavaScript)
element.addEventListener('transitionend', () => {
console.log('Transition complete');
});
Timing Functions
Timing Functions
Built-in Timing Functions
.button {
transition-timing-function: ease; /* Default */
transition-timing-function: linear; /* Constant speed */
transition-timing-function: ease-in; /* Slow start */
transition-timing-function: ease-out; /* Slow end */
transition-timing-function: ease-in-out; /* Slow start and end */
}
cubic-bezier
Custom timing curves:
.button {
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
/* Material Design standard */
}
/* Snappy */
transition-timing-function: cubic-bezier(0.2, 0, 0, 1);
/* Bouncy */
transition-timing-function: cubic-bezier(0.68, -0.55, 0.27, 1.55);
steps()
Stepped animations:
.typewriter {
transition-timing-function: steps(10);
}
Visual Comparison
ease: -----------/
linear: -----------
ease-in: __________/
ease-out: -----------
ease-in-out: ___-------___
Best Practices
Best Practices
Performance
/* Good: GPU-accelerated properties */
.button {
transition: transform 0.3s ease, opacity 0.3s ease;
}
/* Bad: Causes layout/paint */
.button {
transition: width 0.3s ease, height 0.3s ease;
}
GPU-Accelerated Properties
transformopacityfilter
Respect User Preferences
@media (prefers-reduced-motion: reduce) {
* {
transition-duration: 0.01ms !important;
}
}
Common Patterns
/* Fade in */
.fade-in {
opacity: 0;
transition: opacity 0.3s ease;
}
.fade-in.visible {
opacity: 1;
}
/* Slide up */
.slide-up {
transform: translateY(20px);
opacity: 0;
transition: transform 0.3s ease, opacity 0.3s ease;
}
.slide-up.visible {
transform: translateY(0);
opacity: 1;
}
/* Scale on hover */
.card {
transition: transform 0.2s ease;
}
.card:hover {
transform: scale(1.02);
}
Transition vs Animation
| Feature | Transition | Animation |
|---|---|---|
| Trigger | State change | Keyframes |
| Direction | A to B | A to B to C... |
| Control | Limited | Full control |
| Iteration | Once | Infinite |
| Delay | One delay | Multiple delays |
Practice Problems
Create a reusable React component implementing CSS Transitions. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for CSS Transitions using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize CSS Transitions for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. What is the default transition timing function?
2. Which CSS property is GPU-accelerated?
3. How do you transition multiple properties?
4. What does transition-delay do?
5. Why should you use prefers-reduced-motion?
Flashcards
Question
What are the transition properties?
Click to reveal answer
Answer
transition-property, transition-duration, transition-timing-function, transition-delay.
Question
Which CSS properties are GPU-accelerated?
Click to reveal answer
Answer
transform, opacity, and filter are GPU-accelerated and perform well.
Question
What is the difference between ease and linear?
Click to reveal answer
Answer
ease starts slow, speeds up, then slows down. linear has constant speed throughout.
Question
How do you disable animations for accessibility?
Click to reveal answer
Answer
Use @media (prefers-reduced-motion: reduce) to disable or minimize animations.
Question
What triggers a CSS transition?
Click to reveal answer
Answer
A change in state, like :hover, :focus, :active, or adding/removing a class.
Revision Notes
Key Takeaways
- 1.Transitions animate between two states
- 2.Use GPU-accelerated properties (transform, opacity) for performance
- 3.Always respect prefers-reduced-motion for accessibility
- 4.Transition shorthand: property duration timing-function delay
- 5.Transitions are triggered by state changes
Interview Tips
- •Know which properties are GPU-accelerated
- •Explain the difference between transition and animation
- •Understand timing functions and cubic-bezier
- •Know how to implement accessible transitions
Cheat Sheet
CSS Transitions Cheat Sheet
Transition Properties
transition-property: background-color;
transition-duration: 0.3s;
transition-timing-function: ease;
transition-delay: 0.1s;
/* Shorthand */
transition: background-color 0.3s ease 0.1s;
Timing Functions
ease- Default, slow start/endlinear- Constant speedease-in- Slow startease-out- Slow endease-in-out- Slow start and endcubic-bezier(x, y, x, y)- Custom
Performance
/* Good: GPU-accelerated */
transition: transform 0.3s, opacity 0.3s;
/* Bad: Layout-triggering */
transition: width 0.3s, height 0.3s;
Accessibility
@media (prefers-reduced-motion: reduce) {
* { transition-duration: 0.01ms !important; }
}