What are CSS Transitions?
CSS transitions allow you to change property values smoothly over a specified duration. They are commonly used for hover effects, focus states, and interactive UI feedback without JavaScript.
Basic Syntax
Transition Properties
| Property | Description | Example |
|---|---|---|
| transition-property | Which CSS property to animate | background-color, width, transform |
| transition-duration | How long the transition takes | 0.5s, 300ms |
| transition-timing-function | Speed curve of the transition | ease, linear, ease-in-out |
| transition-delay | Delay before transition starts | 0.2s |
Timing Functions
Shorthand Syntax
Common Transition Examples
Which Properties Can Be Animated?
- Colors – color, background-color, border-color
- Box Model – width, height, margin, padding, border-width
- Positioning – top, right, bottom, left
- Transforms – scale, rotate, translate, skew
- Opacity & Visibility – opacity, visibility
- Shadows & Filters – box-shadow, text-shadow, filter
Transition vs Animation
| Feature | Transition | Animation |
|---|---|---|
| Trigger | State change (hover, focus) | Can run automatically |
| Iterations | Once per state change | Infinite possible |
| Keyframes | No | Yes (@keyframes) |
| Complexity | Simple property changes | Multi-step sequences |
| Use case | Hover effects, interactive feedback | Loaders, continuous motion |
Practical Card Example
Best Practices
- Use transforms for position changes – Better performance than animating top/left
- Prefer opacity over visibility – Smoother and GPU-accelerated
- Keep durations short – 0.2s–0.5s for most UI interactions
- Avoid animating layout properties – width, height, margin trigger reflows
- Test on mobile devices – Ensure 60fps performance
Common Mistakes
- Animating all properties – Use
transition: allsparingly (performance) - Too long durations – Annoying for users (avoid > 1s for UI)
- Inconsistent delays – Creates disjointed experiences
- Forgetting to set initial state – Transition needs start and end values
Conclusion
CSS transitions provide an easy way to add polish and interactivity to your websites. They improve user experience through subtle motion feedback and are widely supported across all modern browsers.