CSS Transitions

CSS Transitions allow CSS property values to change smoothly over a specified period instead of changing instantly. They help create attractive and interactive web pages without using JavaScript.

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
CSS Transitions make websites look more professional and interactive.

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.
The transition property determines which CSS property changes smoothly.

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.
A duration of 0s means no transition effect.

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.
Choosing the right timing function makes animations feel more natural.

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.
Use transition delays carefully so users do not feel the interface is slow.

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.
The shorthand property makes your CSS shorter and easier to read.

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);

}
Hover transitions improve the user experience by providing visual feedback.

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
Multiple transitions create rich and engaging UI effects.

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);

}
Small transition effects can greatly improve the appearance of a website.

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);

}
The shorthand transition property is the preferred way to define transitions.

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 opacity and transform whenever possible.
  • Avoid excessive animations that distract users.
  • Use consistent transition durations throughout the website.
  • Combine transitions with :hover effects.
  • Test animations on different devices and browsers.
  • Use the shorthand transition property to keep code clean.
Simple and smooth transitions create a better user experience.

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;

}
Long or unnecessary transitions can make a website feel slow.

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
CSS Transitions work reliably across all modern browsers.

14. CSS Transition Interview Questions

  1. What is a CSS Transition?
  2. Why are CSS Transitions used?
  3. What is the difference between transitions and animations?
  4. Which property specifies the animation duration?
  5. What does transition-delay do?
  6. What is the purpose of transition-timing-function?
  7. How do you animate multiple properties?
  8. What is the shorthand syntax for transitions?
  9. Which pseudo-class is commonly used with transitions?
  10. Why are transitions useful for user interfaces?
These questions are frequently asked in Front-End Developer interviews.

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.
Congratulations! You have successfully completed the CSS Transitions lesson. You can now create smooth and visually appealing effects for buttons, images, cards, menus, and many other webpage elements.

Quick Quiz

Which CSS property is used to create smooth changes between property values?