CSS Transitions
1. Introduction to CSS Transitions
A transition creates a smooth animation when the value of a CSS property changes, such as color, width, height, opacity, or transform.
Advantages of CSS Transitions
- Create smooth animations.
- Improve user experience.
- Require very little code.
- Do not require JavaScript.
- Increase website interactivity.
Without Transition
Button → Color changes instantly
With Transition
Button → Color changes smoothly
2. Transition Property
The transition property specifies which CSS property should animate when its value changes.
button{
background:blue;
transition:background;
}
button:hover{
background:red;
}
| Property | Description |
|---|---|
| transition | Specifies which property will animate. |
| all | Animates all changeable properties. |
3. Transition Duration
The transition-duration property specifies how long the transition should take to complete.
.box{
width:120px;
background:orange;
transition-duration:1s;
}
.box:hover{
width:250px;
}
| Value | Meaning |
|---|---|
| 0.5s | Half a second. |
| 1s | One second. |
| 2s | Two seconds. |
4. Transition Timing Function
The transition-timing-function property controls the speed curve of the transition.
.box{
transition-property:width;
transition-duration:2s;
transition-timing-function:ease;
}
| Value | Description |
|---|---|
| ease | Starts slow, speeds up, then slows down. |
| linear | Constant speed. |
| ease-in | Starts slowly. |
| ease-out | Ends slowly. |
| ease-in-out | Slow at both beginning and end. |
5. Transition Delay
The transition-delay property specifies how long to wait before starting a transition.
.box{
transition-property:background;
transition-duration:1s;
transition-delay:2s;
}
.box:hover{
background:red;
}
| Value | Effect |
|---|---|
| 0s | Starts immediately. |
| 1s | Starts after one second. |
| 2s | Starts after two seconds. |
6. Transition Shorthand Property
Instead of writing multiple transition properties separately, you can use the transition shorthand property to define them in a single line.
button{
transition:background 0.5s ease;
}
button:hover{
background:green;
}
| Part | Description |
|---|---|
| background | Property to animate. |
| 0.5s | Duration of the transition. |
| ease | Timing function. |
7. Hover Effects with Transitions
CSS Transitions are commonly used with the :hover pseudo-class to create attractive button, image, and card effects.
.button{
background:#0d6efd;
color:white;
transition:0.4s;
}
.button:hover{
background:#198754;
}
.image{
transition:transform 0.5s;
}
.image:hover{
transform:scale(1.1);
}
8. Multiple Property Transitions
You can animate multiple CSS properties at the same time using a single transition declaration.
.box{
width:150px;
background:steelblue;
color:white;
transition:
width 0.5s,
background 0.5s,
border-radius 0.5s;
}
.box:hover{
width:250px;
background:tomato;
border-radius:20px;
}
| Property | Transition |
|---|---|
| Width | ✔ Yes |
| Background | ✔ Yes |
| Border Radius | ✔ Yes |
9. Practical Transition Examples
Example 1: Button Animation
button{
transition:background 0.4s;
}
button:hover{
background:green;
}
Example 2: Image Zoom
img{
transition:transform 0.5s;
}
img:hover{
transform:scale(1.1);
}
Example 3: Card Shadow
.card{
transition:box-shadow 0.4s;
}
.card:hover{
box-shadow:0 10px 20px
rgba(0,0,0,0.3);
}
10. CSS Transition Comparison Table
| Property | Purpose |
|---|---|
| transition-property | Selects which property will animate. |
| transition-duration | Specifies animation time. |
| transition-timing-function | Controls animation speed. |
| transition-delay | Delays the animation. |
| transition | Shorthand property. |
.box{
transition:
all 0.5s ease;
}
.box:hover{
transform:rotate(10deg);
}
11. CSS Transition Best Practices
Following best practices helps you create smooth, professional, and user-friendly transition effects.
- Use transitions only where they improve user experience.
- Keep transition durations between 0.2s and 0.5s for UI elements.
- Animate lightweight properties like
opacityandtransformwhenever possible. - Avoid excessive animations that distract users.
- Use consistent transition durations throughout the website.
- Combine transitions with
:hovereffects. - Test animations on different devices and browsers.
- Use the shorthand
transitionproperty to keep code clean.
12. Common CSS Transition Mistakes
| Mistake | Correct Practice |
|---|---|
| Using very long durations. | Keep transitions short and smooth. |
| Animating every property unnecessarily. | Animate only required properties. |
| Using transitions without user interaction. | Use with hover, focus, or active states. |
| Using inconsistent timing values. | Maintain a consistent animation style. |
| Forgetting the hover state. | Always define the property change. |
❌ Incorrect
.box{
transition:5s;
}
✔ Correct
.box{
transition:0.3s ease;
}
13. Browser Support
CSS Transitions are supported by all modern browsers and are widely used in modern web development.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Transition Interview Questions
- What is a CSS Transition?
- Why are CSS Transitions used?
- What is the difference between transitions and animations?
- Which property specifies the animation duration?
- What does
transition-delaydo? - What is the purpose of
transition-timing-function? - How do you animate multiple properties?
- What is the shorthand syntax for transitions?
- Which pseudo-class is commonly used with transitions?
- Why are transitions useful for user interfaces?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Transitions.
- ✔ Transition Property.
- ✔ Transition Duration.
- ✔ Transition Timing Function.
- ✔ Transition Delay.
- ✔ Transition Shorthand Property.
- ✔ Hover Effects.
- ✔ Multiple Property Transitions.
- ✔ Practical Transition Examples.
- ✔ Best Practices and Common Mistakes.
Quick Quiz
Which CSS property is used to create smooth changes between property values?